json method

void json(
  1. Object? object, {
  2. Object? toEncodable(
    1. Object? nonSerializable
    )?,
  3. Encoding? encoding,
})

Write a JSON object to the response body.

The optional toEncodable function is called for any non-primitive object that is to be encoded. If toEncodable is omitted, it defaults to a function that returns the result of calling .toJson() on the object.

Implementation

void json(
  Object? object, {
  Object? Function(Object? nonSerializable)? toEncodable,
  Encoding? encoding,
}) {
  contentType = ContentType.json;
  encoding ??= context.app.encoding;

  return raw(encoding.encode(jsonEncode(object, toEncodable: toEncodable)));
}