fromJson static method

Constant fromJson(
  1. Map<String, Object?> value,
  2. List<Constant> constants
)

Creates a Constant object from its JSON representation.

constants needs to be passed, as the Constants are normalized and stored separately in the JSON.

Implementation

static Constant fromJson(
  Map<String, Object?> value,
  List<Constant> constants,
) => switch (value[_typeKey] as String) {
  NullConstant._type => const NullConstant(),
  BoolConstant._type => BoolConstant(value[_valueKey] as bool),
  IntConstant._type => IntConstant(value[_valueKey] as int),
  StringConstant._type => StringConstant(value[_valueKey] as String),
  ListConstant._type => ListConstant(
    (value[_valueKey] as List<dynamic>)
        .map((value) => value as int)
        .map((value) => constants[value])
        .toList(),
  ),
  MapConstant._type => MapConstant(
    (value[_valueKey] as Map<String, Object?>).map(
      (key, value) => MapEntry(key, constants[value as int]),
    ),
  ),
  InstanceConstant._type => InstanceConstant(
    fields: (value[_valueKey] as Map<String, Object?>? ?? {}).map(
      (key, value) => MapEntry(key, constants[value as int]),
    ),
  ),
  String() =>
    throw UnimplementedError('This type is not a supported constant'),
};