JsonConverter

The JsonConverter class allows you to convert arbitrary values (e.g. a JSON object) from and to JSON (JavaScript Object Notation) strings.

Description

The JsonConverter class allows you to convert arbitrary values (e.g. a JSON object) from and to JSON (JavaScript Object Notation) strings.

Static methods

fromJson

Converts a JSON string into a value of the type specified by a TypeCode.

static T? fromJson<T>(type: TypeCode?, value: String)

  • type: TypeCode? - TypeCode for the data type into which ‘value’ is to be converted.
  • value: String - JSON string to convert.
  • returns: T? - converted object value or null when value is null.

toJson

Converts value into JSON string.

static String? toJson(value)

  • value: dynamic - the value to convert.
  • returns: String? - JSON string or null when value is null.

toMap

Converts JSON string into a map object or returns an empty map when the conversion is not possible. See LongConverter.toNullableLong

static Map<String, dynamic>? toMap(String? value)

  • value: String? - JSON string to convert.
  • returns: Map<String, dynamic>? - Map object value or empty object when the conversion is not supported.

toMapWithDefault

Converts a JSON string into a map object or returns a default value when the conversion is not possible.

static Map<String, dynamic> toMapWithDefault(String value, Map<String, dynamic> defaultValue)

  • value: String - JSON string to convert.
  • defaultValue: Map<String, dynamic> - default value.
  • returns: Map<String, dynamic> - Map object or given default when the conversion is not supported.

toNullableMap

Converts a JSON string into a map object or returns null when the conversion is not possible.
See MapConverter.toNullableMap

static Map<String, dynamic>? toNullableMap(String? value)

  • value: String? - JSON string to convert.
  • returns: Map<String, dynamic>? - Map object or null when the conversion is not supported.

Examples

var value1 = JsonConverter.fromJson('{\'key\':123}'); // Result: { 'key': 123 }
var value2 = JsonConverter.toMap({ 'key': 123 }); // Result: '{ 'key': 123 }'