json method

Future json({
  1. Object? reviver(
    1. Object?,
    2. Object?
    )?,
  2. Encoding? encoding,
})

Returns the JSON object of the request body.

The optional reviver function is called once for each object or list property that has been parsed during decoding. The key argument is either the integer list index for a list property, the string map key for object properties, or null for the final result.

The default reviver (when not provided) is the identity function.

Implementation

Future<dynamic> json({
  Object? Function(Object?, Object?)? reviver,
  Encoding? encoding,
}) async {
  // If the JSON object has already been parsed, return it.
  if (context.contains(key)) return context[key];

  final source = (await text(encoding: encoding)).trim();
  try {
    return context[key] = jsonDecode(source, reviver: reviver);
  } catch (e, stackTrace) {
    throw SpryHttpException.internalServerError(
      message: "Failed to parse json body: ${e.toString()}",
      stackTrace: stackTrace,
      uri: uri,
    );
  }
}