jaguar_serializer 0.4.1 copy "jaguar_serializer: ^0.4.1" to clipboard
jaguar_serializer: ^0.4.1 copied to clipboard

outdatedDart 1 only

Platform and format agnostic serializer built using source_gen

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: ^0.3.0

Simple serializer #

Create a file for your model.

library example.user;

import 'package:jaguar_serializer/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/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/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));
}
7
likes
0
pub points
8%
popularity

Publisher

unverified uploader

Platform and format agnostic serializer built using source_gen

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

intl, yaml, yamlicious

More

Packages that depend on jaguar_serializer