toJson<T> static method

T toJson<T>(
  1. Object? data, {
  2. Object? reviver(
    1. Object?,
    2. Object?
    )?,
})

Converts a JSON-encoded string to a Dart object represented as a Map.

The input data is a JSON-encoded string. Returns a Map representing the Dart object.

Implementation

static T toJson<T>(Object? data,
    {Object? Function(Object?, Object?)? reviver}) {
  if (data is! String) {
    if (data is! T) {
      throw ArgumentException(
          "Invalid data encountered during JSON conversion.",
          details: {"data": data});
    }
    return data;
  }
  final decode = jsonDecode(data, reviver: reviver);
  if (decode is! T) {
    throw ArgumentException(
        "Invalid json casting. excepted: $T got: ${decode.runtimeType}");
  }
  return decode;
}