fromJson method

  1. @override
O fromJson(
  1. Object? json, {
  2. JsonDecoder? jsonDecoder,
  3. bool duplicatedEntitiesAsID = true,
  4. bool? autoResetEntityCache,
})
override

Returns a class instance from json.

Implementation

@override
O fromJson(Object? json,
    {JsonDecoder? jsonDecoder,
    bool duplicatedEntitiesAsID = true,
    bool? autoResetEntityCache}) {
  if (json == null) {
    throw StateError("Null JSON for class: $className");
  }

  if (json is Map) {
    var map = json is Map<String, Object?>
        ? json
        : json.map((k, v) => MapEntry(k.toString(), v));

    jsonDecoder ??= JsonDecoder.defaultDecoder;

    return jsonDecoder.fromJsonMap<O>(map,
        type: classType,
        duplicatedEntitiesAsID: duplicatedEntitiesAsID,
        autoResetEntityCache: autoResetEntityCache);
  } else {
    throw StateError(
        "JSON needs to be a Map to decode a class (`$className`) object. JSON type: `${json.runtimeType}`");
  }
}