JsonValue.fromJson constructor
JsonValue.fromJson(
- Object? json_
Returns a new instance from a JSON value. May throw if the value does not have the expected structure.
Implementation
factory JsonValue.fromJson(Object? json_) {
Object? json = json_;
if (json is Map) {
final MapEntry(:key, :value) =
json.entries.firstWhere((e) => e.key != 'runtimeType');
json = (
key is int ? key : _spec.cases.indexWhere((c) => c.label == key),
value,
);
}
return switch (json) {
(0, null) || [0, null] => const JsonValueNull(),
(1, null) || [1, null] => const JsonValueUndefined(),
(2, final value) || [2, final value] => JsonValueBoolean(value! as bool),
(3, final value) || [3, final value] => JsonValueNumber(value! as double),
(4, final value) ||
[4, final value] =>
JsonValueBigInt(bigIntFromJson(value)),
(5, final value) ||
[5, final value] =>
JsonValueStr(value is String ? value : (value! as ParsedString).value),
(6, final value) || [6, final value] => JsonValueBuffer(
(value is Uint8List
? value
: Uint8List.fromList((value! as List).cast()))),
(7, final value) ||
[7, final value] =>
JsonValueArray(JsonArrayRef.fromJson(value)),
(8, final value) ||
[8, final value] =>
JsonValueMap(JsonMapRef.fromJson(value)),
_ => throw Exception('Invalid JSON $json_'),
};
}