ofDynamic static method

LDValue ofDynamic(
  1. dynamic json
)

Given a dynamic produce an LDValue.

For maximum type safety use the specifically typed methods and builders.

The types that can be stored in an LDValue are limited to those that can be represented by JSON. User-specified types cannot be converted to an LDValue using this function.

The dynamic value should be a num, bool, string, List<dynamic> or Map<String, dynamic>. For lists and maps the inner dynamics have the same constraints.

This method makes a best effort at converting the value into an LDValue. If the dynamic is a list or map and a specific element cannot be converted, then that item will be replaced with ofNull. If the entire object cannot be converted, then ofNull will be returned from this function. The keys of a map must be strings, if they are not then the map cannot be converted and will instead be replaced by ofNull.

Example:

    final converted = LDValue.ofDynamic(
        {'string': 'test', 'bool': true, 17: 'dummy', 'int': 42});
//In this case `converted` will be a null LDValue (LDValue.ofNull()).

An integer type can be converted, but if accessed using toDynamic, either as a single value or as a part of an object/array, then it will be converted to a double.

If a cycle is detected attempting to convert the object, then the value at the point in which the cycle is detected will be replaced with ofNull. Avoid cycles in objects that will need to be converted to LDValue objects.

Example of a cycle in an array.

final a = <dynamic>[];
 final b = [a];
 a.add(b);
 final converter = LDValue.ofDynamic(a);

The converted LDValue will be equivalent to:

[[null]]
final a = <String, dynamic>{};
final b = {'a': a};
a['b'] = b;

final converter = LDValue.ofDynamic(a);

The converted LDValue will be equivalent to:

{"b": {"a": null}}

The implementation may be recursive, so it is possible for there to be a stack overflow if the object to be converted contains an extreme amount of nesting.

Implementation

static LDValue ofDynamic(dynamic json) {
  return _ofDynamic(json, []);
}