Skip to content

Type Conversion ​

Template Engine will try to automatically convert the input data value to the expected data type.

Conversion Rules ​

For some data types, values can be automatically converted following the rules:

Numbers ​

If Number is expected, values of other data types can be converted as follows:

SourceNumber Representative
Null Reference (null)0
Undefined0
Boolean1 for true, 0 for false
StringWill try to parse the number in the string
DateTimeWill convert to timestamp

Boolean ​

If Boolean is expected, values of other data types can be converted as follows:

SourceBoolean Representative
Null Reference (null)false
Undefinedfalse
Stringfalse if empty, true otherwise
List or Mapfalse if empty, true otherwise
Numberfalse for 0, true otherwise

String ​

If String is expected, values of other data types will be converted to string to the same form as it would print it using output code island.

DateTime ​

If DateTime is expected, values of other data types can be converted as follows:

SourceDateTime Representative
String
  • Will try to parse the ISO 8601 string: yyyy-MM-dd'T'HH:mm:ss.SSSZ
  • If string is now, will be converted to current datetime.
NumberWill convert to date using this number as Unix timestamp

Where it works? ​

Automatic data type conversion is available for:

Data Type Conversion in Methods Parameters

  • Doesn't work for some list and map methods.
  • If a method parameter is documented with multiple data types (so-called overloads), the template engine will attempt to convert them in the order specified there.

Comparison Operations ​

Binary Comparison Operators (>, >=, <, <=, in, ==, !=) use the following algorithm:

  1. If both operands can be converted to a numbers, they are converted and compared as numbers;
  2. If at least one of the operands cannot be converted to a number, both operands are converted to strings and compared as strings.

This algorithm is universal, but in some cases the order of operations should be taken into account. For example:

JuniperBot Template
{{ 'true' == 1 == true }}
{{ true == 1 == 'true' }}

It would seem that both expressions should print true, but the first one prints false.

First expression:

  1. 'true' == 1 evaluates as false, because string 'true' cannot be converted to a number, so both operands are converted to strings and compared as 'true' and '1' that gives false in result;
  2. Second operation becomes false == true that gives false as the final result of the whole expression.

Second expression:

  1. true == 1 evaluates as true, because boolean true can be converted to number 1, so both operands are compared as numbers (they are equal);
  2. Second operation becomes true == 'true' that gives true as the final result of the whole expression, but only because second operand 'true' is string that cannot be converted to a number, so both operands are converted to strings and compared as 'true' and 'true' (they are equal).

All rights sniffed.