jaguar_json 1.3.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 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);

  @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<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 {
  @Get()
  @WrapOne(codec)
  Book get(Context ctx) => new Book.fromNum(5);

  @Post()
  @WrapOne(codec)
  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;

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

  @Post()
  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 {
  @Get()
  Response<String> get(Context ctx) =>
      json.serialize(bookSerializer, new Book.fromNum(5));

  @Post()
  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

example/jaguar_json_example.dart

// 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:


dependencies:
  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';
  
Version Uploaded Documentation Archive
1.3.1 Feb 2, 2018 Go to the documentation of jaguar_json 1.3.1 Download jaguar_json 1.3.1 archive
1.2.2 Jan 13, 2018 Go to the documentation of jaguar_json 1.2.2 Download jaguar_json 1.2.2 archive
1.2.1 Jan 13, 2018 Go to the documentation of jaguar_json 1.2.1 Download jaguar_json 1.2.1 archive
1.1.5 Jan 11, 2018 Go to the documentation of jaguar_json 1.1.5 Download jaguar_json 1.1.5 archive
0.2.0 Nov 3, 2017 Go to the documentation of jaguar_json 0.2.0 Download jaguar_json 0.2.0 archive
0.1.4 Oct 5, 2017 Go to the documentation of jaguar_json 0.1.4 Download jaguar_json 0.1.4 archive
0.1.3 Jul 21, 2017 Go to the documentation of jaguar_json 0.1.3 Download jaguar_json 0.1.3 archive
0.1.2 Jul 9, 2017 Go to the documentation of jaguar_json 0.1.2 Download jaguar_json 0.1.2 archive
0.1.1 Jul 9, 2017 Go to the documentation of jaguar_json 0.1.1 Download jaguar_json 0.1.1 archive
0.1.0 Jul 8, 2017 Go to the documentation of jaguar_json 0.1.0 Download jaguar_json 0.1.0 archive

All 11 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
0
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 https://www.dartlang.org/dart-2#migration.

Dependencies

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