jaguar_json 1.3.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 0

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<ModelType> 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).

@Api(path: '/api/book')
class BookRoutes {
  static json.Decode decoder(_) => new json.Decode(bookSerializer);

  static json.Encode<Book> encoder(_) => new json.Encode<Book>(bookSerializer);

  @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<Book> 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.

final JsonRepo repo = new JsonRepo(
    serializers: [personSerializer, bookSerializer], withType: true);

json.CodecRepo codec(_) => new json.CodecRepo(repo);

@Api(path: '/api/book')
class BookRoutes {
  Book get(Context ctx) => new Book.fromNum(5);

  Book post(Context ctx) {
    final book = ctx.getInterceptorResult<Book>(json.CodecRepo);
    return book;

JsonRoutes utility class

Apis can extend JsonRoutes class to get access to toJson and fromJson methods that using JsonRepo to make encoding and decoding JSON easier.

@Api(path: '/api/book')
class BookRoutes extends Object with json.JsonRoutes {
  JsonRepo get repo => models.repo;

  Response<String> get(Context ctx) => toJson(new Book.fromNum(5));

  Future<Response<String>> post(Context ctx) async =>
      toJson(await fromJson(ctx));

Utility functions #

Global functions serialize and deserialize helps in encoding and decoding JSON using Serializer.

@Api(path: '/api/book')
class BookRoutes {
  Response<String> get(Context ctx) =>
      json.serialize(bookSerializer, new Book.fromNum(5));

  Future<Response<String>> post(Context ctx) async => json.serialize(
      bookSerializer, await json.deserialize(bookSerializer, ctx));

Changelog #

1.2.1 #

  • Jaguar 1.2.x style interceptors

0.2.0 #

  • Moved to jaguar_serializer 0.5.x

0.1.4 #

  • jaguar_serializer 0.4.x

0.1.2 #

  • Jaguar 0.6.x

0.1.0 #

  • Jaguar 0.5.x

0.0.1 #

  • Initial version, created by Stagehand


// Copyright (c) 2017, teja. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:jaguar_json/jaguar_json.dart';

main() {}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  jaguar_json: ^1.3.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:jaguar_json/jaguar_json.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0