Dart: Add a middleware to parse to JSON work with shelf_rest

Issue

I’ve created a function that converts a model into JSON using mirror.

Map convertObjectToJson(Object obj);
dynamic convertJsonToObject(Map json, Type type);

I wish to use it so that my models/view-models are converted back and forth between request/response so that I don’t have to implement a toJson() method for every model/view-model class.

I’m planning to use shelf_rest. I’m looking at the example on this page: https://pub.dartlang.org/packages/shelf_rest

But I’m not sure how to hook up my methods above so that it will work with shelf_rest’s route annotations like the resource below …

class AccountResource {
  @Get('{accountId}')
  Account find(String accountId) => new Account.build(accountId: accountId);
}`enter code here`

… but will map the JSON to convert to object before/after the request/response form AccountResource.

Solution

Currently there is no support for plugging in your own JSON converters. It wouldn’t be very hard to add and something that will likely be added in the future as more options for JSON conversion become available.

Answered By – Anders

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.