jaguar_serializer 0.4.2

Build Status

jaguar_serializer

Format agnostic Serializer library that can be used in server and client for JSON, mongodb, postgresql, etc

Getting Started

Install

pub global activate jaguar_serializer_cli

Add it to your project

dependencies:
    jaguar_serializer: <last-version>

Simple serializer

Create a file for your model.

library example.user;

import 'package:jaguar_serializer/jaguar_serializer.dart';

part 'user.g.dart';

Create you model.

/// User model
class User {
  String name;
  int age;
}

Declare a Serializer for your model

@GenSerializer()
class UserJsonSerializer extends Serializer<User> with _$UserJsonSerializer {
  User createModel() => new User();
}

Generate Serializer

Configuration file (Optional since 0.3.5)

Jaguar Serializer need a configuration file to know which files have possible Serializer.

On your root directory, declare the serializer.yaml file with the following informations.

serializers:
- lib/model/user.dart
...

You can use serializer init command to generate it.

If no files are provided, jaguar_serializer will take all dart files under lib, bin, example and test folders.

Build

No you can build you serializer running the command

serializer build

or

pub run jaguar_serializer:serializer build

This command will create 'user.g.dart' file with the generated Serializer inside.

Watch

You can trigger the rebuild each time you do a change in you file by using the watch command.

serializer watch

Use Serializer

A Serializer will convert an instance of object to a Map<String, dynamic>, that can be used to apply conversion to JSON, YAML ...

You can directly use the generated Serializer and apply the conversion.

import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'model/user.dart';

void main() {
  UserSerializer userSerializer = new UserSerializer();
  
  User user = userSerializer.fromMap({
        'name': 'John',
        'age': 25
      });
  
  print(userSerializer.toMap(user));
}

You can also use a JSON repository or implement one.

import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'model/user.dart';

void main() {
  SerializerRepo serializer = new JsonRepo()..add(new UserSerializer());
  
  User user = serializer.deserialize("{'name':'John','age': 25}", type: User);
  
  print(serializer.serialize(user));
}

Changelog

0.4.2

  • Better support of ddc

0.4.1

  • Added docs
  • Fixed README

0.4.0

Breaking Changes

  • Generator is now in a separate package
  • Angular style annotations, only one annotation
@GenSerializer(
  fields: const {
    'field': const EncodeOnly('f')
  }, 
  ignore: const [
    'ignoreField'
  ], 
  processors: const {
   'dates': const DateTimeProcessor(),
  }, 
  serializers: const [
   FieldSerializer,
])

0.3.16

  • to, from, serialize, deserialize methods now handle null values correctly

0.3.15

  • serialize and deserialize methods now handle null values correctly

0.3.14

  • SerializerRepo handles when type is dynamic

0.3.12

  • Added from and to methods to SerializerRepo

0.3.11

  • SerializerRepo now serializes and de-serializes built-in types at the root like Map, List<List>, etc
  • added addAll method to SerializerRepo to add multiple serializers
  • Validation for RawData custom field processor

0.3.7

  • Add builtin RawData Processor
  • Optimize CustomFieldProcessor

0.3.6

  • Update build package to >=0.9.3 <0.10.0

0.3.5

  • serializers.yaml is now optional, Jaguar Serializer will take all dart files under lib, bin, example and test folders. Be careful on big project, watch can be slow or take a lot of resources.

0.3.4

  • Fixed ProvideSerializer for Map fields

0.3.3

  • Better failure messages when property is an Object or dynamic

0.3.2

  • Added "exclude by default" feature

0.3.1

  • Update build (>=0.7.0 <0.8.0)
  • serializer init command, generate serializer.yaml
  • fix overrided field in double inside the generated code

0.3.0

New

  • Repository system (Json, Yaml)
  • embeded type => see typeKey
  • Optional parameters for GenSerializer :
    • bool typeKey (default:true) if you want to include the type
    • String modelName value that will be include as a type.

0.1.2

First release.

Contains:

  1. Serializer for dart builtin types
  2. Custom serializers for fields
  3. Nested Map and List serializers

0.1.3

  • Fixed renaming encode field bug

0.1.6

  • Null nested objects are not serialized properly

1. Depend on it

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

dependencies:
  jaguar_serializer: "^0.4.2"

2. Install it

You can install packages from the command line:

$ 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_serializer/jaguar_serializer.dart';

Platforms

About

Platform and format agnostic serializer built using source_gen

Author

Email tejainece@gmail.com Ravi Teja Gudapati
Email segaud.kevin@gmail.com Kevin Segaud
Email hadrien.lejard@gmail.com Hadrien Lejard

Homepage

github.com/Jaguar-dart/jaguar_serializer

Documentation

www.dartdocs.org/documentation/jaguar_serializer/0.4.2/

Source code (hyperlinked)

www.crossdart.info/p/jaguar_serializer/0.4.2/

Uploader

tejainece@gmail.com
segaud.kevin@gmail.com
hadrien.lejard@gmail.com

License

BSD

Published

Sep 12, 2017

Share