Library in the Commons module that contains data conversion components.
ArrayConverter
Provides methods to create an array from a set of values.
BooleanConverter
Provides methods to convert a value into a Boolean.
DateTimeConverter
Provides methods to convert a value into a Date.
DoubleConverter
Provides methods to convert a value into a double.
FloatConverter
Provides methods to convert a value into a float.
IntegerConverter
Provides methods to convert a value into an integer.
LongConverter
Provides methods to convert a value into a long.
StringConverter
Provides methods to convert a value into a string.
JsonConverter
Provides methods to convert a value from/into a JSON string.
MapConverter
Provides methods to convert a value into a map.
RecursiveMapConverter
Provides methods to convert a value into a map in a recursive manner.
TypeConverter
Provides methods to convert a value into an object specified by a type code and to get the type code of an object.
Introduction
This tutorial will help you to understand the different conversion components available in the Pip.Services toolkit, Commons module, convert library. The tutorial consists of a brief explanation of each class and its methods followed by a set of examples that show how to use each of them.
Convertors
This section contains a brief description of each class and its methods.
1) ArrayConverter
This class provides methods to create an array from a set of values. Its methods are summarized in the table and examples below.
Method
Description
to_array
Converts a value into an array object with an empty array as default. Single values are converted into arrays with a single element.
to_array_with_default
Converts a value into an array object with a specified default. Single values are converted into arrays with a single element.
list_to_array
Converts a list into an array object, with an empty array as default. Strings with comma-delimited values are split into an array of strings.
to_nullable_array
Converts a value into an array object. Single values are converted into arrays with a single element.
This class can be used to convert different values to Boolean values using extended conversion rules. The rules and methods provided by this class are summarized in the tables and examples below.
Conversion rules
Number
<>0 are true, =0 are false.
String
"true", "yes", "T", "Y", "1" are true; "false", "no", "F", "N" are false.
DateTime
<>0 total milliseconds are true, =0 are false.
Method
Description
to_boolean
Converts a value into a boolean or returns false when the conversion is not possible.
to_boolean_with_default
Converts a value into a boolean or returns a default value when the conversion is not possible.
to_nullable_boolean
Converts value into a boolean or returns None when the conversion is not possible.
This class can be used to convert arbitrary values into Date values using extended conversion rules. Its methods and conversion rules are explained in the tables and examples below.
Conversion rules
String
Converts using the ISO time format.
Number
Converts using milliseconds since Unix epoch.
Method
Description
to_datetime
Converts a value into Date or returns the current date when the conversion is not possible.
to_date_time_with_default
Converts a value into Date or returns a default date when the conversion is not possible.
to_nullable_date_time
Converts a value into Date or returns None when the conversion is not possible.
// Date time converter
import{DateTimeConverter}from"pip-services3-commons-nodex";letvalue0=DateTimeConverter.toDateTime("2021-11-09T17:24:50.750Z");// Returns 2021-11-09 17:24:50.75
letvalue1=DateTimeConverter.toNullableDateTime("ABC");// Returns null
letvalue2=DateTimeConverter.toNullableDateTime("2021-11-09T17:24:50.750Z");// Returns 2021-11-09 17:24:50.75
letvalue3=DateTimeConverter.toNullableDateTime(12345657755000);// Returns 2361-03-21 16:22:35
letvalue4=DateTimeConverter.toDateTimeWithDefault("ABC",newDate());// Returns current datetime
letvalue5=DateTimeConverter.toDateTimeWithDefault("2021-11-09T17:24:50.750Z",newDate());// Returns 2021-11-09 17:24:50.75
// Date time converter
usingPipServices3.Commons.Convert;varvalue0=DateTimeConverter.ToDateTime("2021-11-09T17:24:50.750Z");// Returns 2021-11-09 17:24:50.75
varvalue1=DateTimeConverter.ToNullableDateTime("ABC");// Returns null
varvalue2=DateTimeConverter.ToNullableDateTime("2021-11-09T17:24:50.750Z");// Returns 2021-11-09 17:24:50.75
varvalue3=DateTimeConverter.ToNullableDateTime(1234565775500);// Returns 0001-01-02 10:17:36
varvalue4=DateTimeConverter.ToDateTimeWithDefault("ABC",DateTime.Now);// Returns current datetime
varvalue5=DateTimeConverter.ToDateTimeWithDefault("2021-11-09T17:24:50.750Z",DateTime.Now);// Returns 2021-11-09 17:24:50.75
// Date time converter
// Returns 2021-11-09 17:24:50.75
value0:=convert.DateTimeConverter.ToDateTime("2021-11-09T17:24:50.750Z")// Returns nil
value1:=convert.DateTimeConverter.ToNullableDateTime("ABC")// Returns 2021-11-09 17:24:50.75
value2:=convert.DateTimeConverter.ToNullableDateTime("2021-11-09T17:24:50.750Z")// Returns 2361-03-21 16:22:35
value3:=convert.DateTimeConverter.ToNullableDateTime(12345657755000)// Returns current datetime
value4:=convert.DateTimeConverter.ToDateTimeWithDefault("ABC",time.Now())// Returns 2021-11-09 17:24:50.75
value5:=convert.DateTimeConverter.ToDateTimeWithDefault("2021-11-09T17:24:50.750Z",time.Now())
// Date time converter
import'package:pip_services3_commons/pip_services3_commons.dart';varvalue0=DateTimeConverter.toDateTime('2021-11-09T17:24:50.750Z');// Returns 2021-11-09 17:24:50.75
varvalue1=DateTimeConverter.toNullableDateTime('ABC');// Returns null
varvalue2=DateTimeConverter.toNullableDateTime('2021-11-09T17:24:50.750Z');// Returns 2021-11-09 17:24:50.75
varvalue3=DateTimeConverter.toNullableDateTime(12345657755000);// Returns 2361-03-21 16:22:35
varvalue4=DateTimeConverter.toDateTimeWithDefault('ABC',DateTime.now());// Returns current datetime
varvalue5=DateTimeConverter.toDateTimeWithDefault('2021-11-09T17:24:50.750Z',DateTime.now());// Returns 2021-11-09 17:24:50.75
# Date time converterfrompip_services3_commons.convertimportDateTimeConverterfromdatetimeimportdatevalue0=DateTimeConverter.to_datetime("2021-11-09T17:24:50.750Z")# Returns 2021-11-09 17:24:50.75value1=DateTimeConverter.to_nullable_datetime("ABC")# Returns Nonevalue2=DateTimeConverter.to_nullable_datetime("2021-11-09T17:24:50.750Z")# Returns 2021-11-09 17:24:50.75value3=DateTimeConverter.to_nullable_datetime(12345657755000)# Returns 2361-03-21 16:22:35value4=DateTimeConverter.to_datetime_with_default("ABC",date.today())# Returns current datetimevalue4=DateTimeConverter.to_datetime_with_default("2021-11-09T17:24:50.750Z",date.today())# Returns 2021-11-09 17:24:50.75
Not available
4) DoubleConverter
This class is used to convert arbitrary values into doubles using extended conversion rules. The rules and available methods are explained in the following tables and examples.
Conversion rules
String
Converts to a double value.
DateTime
Total number of milliseconds since Unix epoch.
Boolean
1 for true and 0 for false.
Method
Description
to_double
Converts a value into a double or returns 0 when the conversion is not possible.
to_double_with_default
Converts a value into a double or returns a default value when the conversion is not possible.
to_nullable_double
Converts a value into a double or returns None when the conversion is not possible.
This class can be used to convert arbitrary values into floats using extended conversion rules. Its methods and conversion rules are explained in the tables and examples below.
Conversion rules
String
Converts to a float value.
DateTime
Total number of milliseconds since Unix epoch.
Boolean
1 for true and 0 for false.
Method
Description
to_float
Converts a value into a float or returns 0 when the conversion is not possible.
to_float_with_default
Converts a value into a float or returns a default value when the conversion is not possible.
to_nullable_float
Converts a value into a float or returns None when the conversion is not possible.
This class is used to convert arbitrary values into integers using extended conversion rules. Its methods and rules are explained in the following tables and examples.
Conversion rules
String
Converts to float, then to integer.
DateTime
Total number of milliseconds since Unix epoch.
Boolean
1 for true and 0 for false.
Method
Description
to_integer
Converts a value into an integer or returns 0 when the conversion is not possible.
to_integer_with_default
Converts a value into an integer or returns a default value when the conversion is not possible.
to_nullable_integer
Converts a value into an integer or returns None when the conversion is not possible.
This class is used to convert arbitrary values into longs using extended conversion rules. Its methods and rules are explained in the following tables and examples.
Conversion rules
String
Converts to float, then to long.
DateTime
Total number of milliseconds since Unix epoch.
Boolean
1 for true and 0 for false.
Method
Description
to_long
Converts a value into a long or returns 0 when the conversion is not possible.
to_long_with_default
Converts a value into a long or returns a default value when the conversion is not possible.
to_nullable_long
Converts a value into a long or returns None when the conversion is not possible.
This class is used to convert arbitrary values (e.g. a JSON object) from and to JSON (JavaScript Object Notation) strings. Its methods are explained in the table and examples below.
Method
Description
from_json
Converts a JSON string into a value of the type specified by a TypeCode.
to_json
Converts a value into a JSON string.
to_map
Converts a JSON string into a map object or returns an empty map when the conversion is not possible.
to_map_with_default
Converts a JSON string into a map object or returns a default value when the conversion is not possible.
to_nullable_map
Converts a JSON string into a map object or returns None when the conversion is not possible.
Both classes are used to convert arbitrary values into map objects using extended conversion rules. The difference between them is that RecursiveMapConverter uses recursion to convert all values stored in objects and arrays, and MapConverter doesn’t. The tables and examples below explain their methods and conversion rules.
Conversion rules
Objects
Property names as keys, property values as values.
Arrays
Element indexes as keys, elements as values.
MapConverter
Method
Description
to_map
Converts a value into a map object or returns an empty map when the conversion is not possible.
to_map_with_default
Converts a value into a map object or returns a default value when the conversion is not possible.
to_nullable_map
Converts a value into a map object or returns None when the conversion is not possible.
This class is used to convert arbitrary values into objects specified by a TypeCode and to get the TypeCode of an object. Its methods are explained in the table and examples below.
Note: Code types are defined in the TypeCode class available in the same package.
Method
Description
to_nullable_type
Converts a value into an object type specified by a TypeCode or returns None when the conversion is not possible.
to_string
Converts a TypeCode into its string name.
to_type
Converts a value into an object type specified by a TypeCode
to_type_code
Gets the TypeCode for a specific value.
to_type_with_default
Converts a value into an object type specified by TypeCode or returns a default value when the conversion is not possible.
In this tutorial, we have seen several components that provide methods with conversion mechanisms for arrays, Booleans, dates, doubles, floats, integers, longs, strings, JSON objects, maps and types defined by a type code.