JsonValueItem.fromJson constructor

JsonValueItem.fromJson(
  1. Object? json_
)

Returns a new instance from a JSON value. May throw if the value does not have the expected structure.

Implementation

factory JsonValueItem.fromJson(Object? json_) {
  final json = json_ is Map
      ? _spec.fields.map((f) => json_[f.label]).toList(growable: false)
      : json_;
  return switch (json) {
    [final item, final arrayReferences, final mapReferences] ||
    (final item, final arrayReferences, final mapReferences) =>
      JsonValueItem(
        item: JsonValue.fromJson(item),
        arrayReferences: (arrayReferences! as Iterable)
            .map((e) => (e! as Iterable).map(JsonValue.fromJson).toList())
            .toList(),
        mapReferences: (mapReferences! as Iterable)
            .map((e) => (e! as Iterable)
                .map((e) => (() {
                      final l = e is Map
                          ? List.generate(2, (i) => e[i.toString()],
                              growable: false)
                          : e;
                      return switch (l) {
                        [final v0, final v1] || (final v0, final v1) => (
                            v0 is String ? v0 : (v0! as ParsedString).value,
                            JsonValue.fromJson(v1),
                          ),
                        _ => throw Exception('Invalid JSON $e')
                      };
                    })())
                .toList())
            .toList(),
      ),
    _ => throw Exception('Invalid JSON $json_')
  };
}