bodyAsJson<T, F> method

Future<T> bodyAsJson <T, F>(
  1. {Encoding encoding: conv.utf8,
  2. Converter<T, F> convert,
  3. Type type}
)

Decodes JSON body of the request

Example: final server = new Jaguar(); server.post('/api/book', (Context ctx) async { // Decode request body as JSON Map final json = await ctx.req.bodyAsJson(); // ... }); await server.serve();

Implementation

Future<T> bodyAsJson<T, F>(
    {conv.Encoding encoding = conv.utf8,
    Converter<T, F> convert,
    Type type}) async {
  final String text = await bodyAsText(encoding);
  final dec = conv.json.decode(text);
  if (convert != null) return convert(dec);
  {
    final repo = _serializers[MimeTypes.json];
    if (repo != null) {
      final ser = _serializers[MimeTypes.json].getByType<T>(type ?? T);
      if (ser != null && dec is Map) return ser.fromMap(dec);
    }
  }
  return dec;
}