Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jaguar-dart/jaguar_json
jaguar_serializer based JSON interceptors for Jaguar
https://github.com/jaguar-dart/jaguar_json
dartlang http jaguar json marshall rest-api serialize serializer server
Last synced: 2 months ago
JSON representation
jaguar_serializer based JSON interceptors for Jaguar
- Host: GitHub
- URL: https://github.com/jaguar-dart/jaguar_json
- Owner: Jaguar-dart
- License: bsd-3-clause
- Created: 2017-05-24T14:25:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-02T16:26:29.000Z (almost 7 years ago)
- Last Synced: 2024-10-13T08:42:36.848Z (3 months ago)
- Topics: dartlang, http, jaguar, json, marshall, rest-api, serialize, serializer, server
- Language: Dart
- Size: 38.1 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# jaguar_json
This package provides jaguar_serializer based interceptors, utitity methods and utility classes for Jaguar to make
encoding and decoding JSON easier.# Interceptors
jaguar_json exposes interceptors to encode and decode Dart objects to and from JSON using two methods:
1. Using `Serializer`
2. Using `JsonRepo`## Using `Serializer`
`Encode`, `Decode` and `Codec` interceptors are provided to encode and decode json. Both these interceptors
accept an instance of the `Serializer` class that it internally uses to serialize and
deserialize.`Encode` automatically serializes result returned from the route method. Care must be taken that the
route returns an object of type `ModelType`.`Decode` automatically de-serializes JSON body in the request to `ModelType`. The de-serialized dart
object can be obtained in the route method using `ctx.getInterceptorResult(Decode)`.```dart
@Api(path: '/api/book')
class BookRoutes {
static json.Decode decoder(_) => new json.Decode(bookSerializer);static json.Encode encoder(_) => new json.Encode(bookSerializer);
@Post()
@Wrap(const [decoder, encoder])
Book one(Context ctx) {
final Book book = ctx.getInterceptorResult(json.Decode);
return book;
}@Post(path: '/many')
@Wrap(const [decoder, encoder])
List list(Context ctx) => ctx.getInterceptorResult(json.Decode);
}
```## Using `JsonRepo`
Using `JsonRepo` simplifies the encoding and decoding. First task is to create a `JsonRepo` object
and add all the required serializers to it.`EncodeRepo`, `DecodeRepo` and `CodecRepo` interceptors are provided to encode and decode json using repo. Both these
interceptors accept an instance of `JsonRepo` that it internally uses to find the appropriate serializer
to serialize and deserialize. For the decoder to find the right serializer, the incoming JSON body must have a type
field. The type field can be set in the `DecodeRepo` using `typeKey` argument.```dart
final JsonRepo repo = new JsonRepo(
serializers: [personSerializer, bookSerializer], withType: true);json.CodecRepo codec(_) => new json.CodecRepo(repo);
@Api(path: '/api/book')
class BookRoutes {
@Get()
@WrapOne(codec)
Book get(Context ctx) => new Book.fromNum(5);@Post()
@WrapOne(codec)
Book post(Context ctx) {
final book = ctx.getInterceptorResult(json.CodecRepo);
return book;
}
}
```# `JsonRoutes` utility class
`Api`s can extend `JsonRoutes` class to get access to `toJson` and `fromJson` methods that using `JsonRepo` to make
encoding and decoding JSON easier.```dart
@Api(path: '/api/book')
class BookRoutes extends Object with json.JsonRoutes {
JsonRepo get repo => models.repo;@Get()
Response get(Context ctx) => toJson(new Book.fromNum(5));@Post()
Future> post(Context ctx) async =>
toJson(await fromJson(ctx));
}
```# Utility functions
Global functions `serialize` and `deserialize` helps in encoding and decoding JSON using `Serializer`.
```dart
@Api(path: '/api/book')
class BookRoutes {
@Get()
Response get(Context ctx) =>
json.serialize(bookSerializer, new Book.fromNum(5));@Post()
Future> post(Context ctx) async => json.serialize(
bookSerializer, await json.deserialize(bookSerializer, ctx));
}
```