cereal 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 48

Cereal, a serialization library for Dart #

Cereal offers no-boilerplate JSON serialization for Dart objects. It does this with Dart's new extension methods & code generation.

Example #

import 'package:cereal/cereal.dart';

// The code generator will create this file,
// but you do have to type this file name in first.
part 'my_file.g.dart';

@cereal
class Struct {
    final int foo;
    final String bar;

    Struct({this.foo, this.bar});
}

The code generator will generate a Struct.toJson, as well as a json.decodeStruct method on the json object in dart:convert.

import 'dart:convert';

import 'src/my_file.dart';

void main() {
    json.encodeStruct(Struct(
        foo: 0, 
        bar: 'hello world',
    ));

    final struct = json.decodeStruct(
        '{"foo": 0, "bar": "hello world"}',
    );
}

Whenever your Struct class is in-scope, the appropriate serializer and deserializer methods will also be available. This is made possible by generated Dart class extension methods.

Adding to your project #

To add Cereal to your project, change your pubspec.yaml like so:

# You need at least Dart 2.6.0, which launched extension methods.
environment:
  sdk: '>=2.6.0 <3.0.0'

dependencies:
  # Depend on some version of cereal.
  cereal: ^0.0.2

dev_dependencies:
  # Depend on some version of cereal_generator
  cereal_generator: ^0.0.2
  # build_runner will run the code generator.
  build_runner: ^1.0.0

From there, you can run pub get or flutter pub get and start annotating data classes as @cereal.

To generate the json serializers, run the following:

pub run build_runner build

or

flutter pub run build_runner build

0.0.3 #

  • Properly type the generated encoder.

0.0.2 #

  • Improve support for serializing dynamic types
  • Add a @skip annotation to skip fields not intended for serialization.

0.0.1 #

  • Add support for serializing primitive types, collections, and classes annotated with @cereal.

example/lib/main.dart

import 'dart:convert';

import 'src/more_entities.dart';
import 'src/sample.dart';

void main() {
  final unencodedJson = Foo(
    bar: 0,
    bazes: {
      Baz(
        value: 'words',
        extraStuff: {
          'key': 'value',
          'typed': 20.0,
        },
      )
    },
    doubleToBop: {
      0.0: Bop(
        numberOfPeopleWithName: {'Bob': 5, 'Yuqian': 20},
      ),
      0.5: Bop(
        numberOfPeopleWithName: {'Bob': 20, 'Yuqian': 5},
      ),
    },
    names: ['words1', 'words2'],
  ).toJson();

  print(
    JsonEncoder.withIndent('  ').convert(unencodedJson),
  );
  final encoded = '''
{
  "bar": 0,
  "bazes": [
    {
      "value": "words"
    }
  ],
  "doubleToBop": [
    {
      "key": 0.0,
      "value": {
        "numberOfPeopleWithName": [
          {
            "key": "Bob",
            "value": 5
          },
          {
            "key": "Yuqian",
            "value": 20
          }
        ]
      }
    },
    {
      "key": 0.5,
      "value": {
        "numberOfPeopleWithName": [
          {
            "key": "Bob",
            "value": 20
          },
          {
            "key": "Yuqian",
            "value": 5
          }
        ]
      }
    }
  ],
  "names": [
    "words1",
    "words2"
  ]
}
''';

  print(json.decodeFoo(encoded).toJson());
}

Use this package as a library

1. Depend on it

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


dependencies:
  cereal: ^0.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:cereal/cereal.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
48
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
meta ^1.1.0 1.2.2 1.3.0-nullsafety