cereal_generator 0.0.3

  • Readme
  • Changelog
  • Installing
  • 46

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.

Use this package as a library

1. Depend on it

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


dependencies:
  cereal_generator: ^0.0.3

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:cereal_generator/cereal_generator.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]
80
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13

Analysis suggestions

Package not compatible with SDK flutter

Because it is not compatible with any of the supported runtimes: flutter-native, flutter-web

Package not compatible with runtime flutter-native on android

Because of the import of dart:mirrors via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/type_checker.dartdart:mirrors

Package not compatible with runtime flutter-native on ios

Because of the import of dart:mirrors via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/type_checker.dartdart:mirrors

Package not compatible with runtime flutter-native on linux

Because of the import of dart:mirrors via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/type_checker.dartdart:mirrors

Package not compatible with runtime flutter-native on macos

Because of the import of dart:mirrors via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/type_checker.dartdart:mirrors

Package not compatible with runtime flutter-native on windows

Because of the import of dart:mirrors via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/type_checker.dartdart:mirrors

Package not compatible with runtime flutter-web on web

Because of the import of dart:io via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/utils.dartpackage:build/build.dartpackage:build/src/generate/run_post_process_builder.dartpackage:build/src/builder/post_process_builder.dartpackage:build/src/builder/builder.dartpackage:build/src/builder/build_step.dartpackage:build/src/asset/reader.dartpackage:glob/glob.dartpackage:glob/src/list_tree.dartpackage:glob/src/io.dartpackage:glob/src/io_export.dartdart:io

Package not compatible with runtime native-aot

Because of the import of dart:mirrors via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/type_checker.dartdart:mirrors

Package not compatible with runtime web

Because of the import of dart:io via the import chain package:cereal_generator/cereal_generator.dartpackage:source_gen/source_gen.dartpackage:source_gen/src/utils.dartpackage:build/build.dartpackage:build/src/generate/run_post_process_builder.dartpackage:build/src/builder/post_process_builder.dartpackage:build/src/builder/builder.dartpackage:build/src/builder/build_step.dartpackage:build/src/asset/reader.dartpackage:glob/glob.dartpackage:glob/src/list_tree.dartpackage:glob/src/io.dartpackage:glob/src/io_export.dartdart:io

Health suggestions

Fix lib/cereal_generator.dart. (-0.50 points)

Analysis of lib/cereal_generator.dart reported 1 hint:

line 115 col 10: This function has a return type of 'String', but doesn't end with a return statement.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and cereal_generator.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

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
analyzer ^0.39.0 0.39.11
build ^1.0.0 1.3.0
build_config >=0.3.1 <0.5.0 0.4.2
source_gen ^0.9.0 0.9.5
Transitive dependencies
_fe_analyzer_shared 5.0.0
args 1.6.0
async 2.4.1
charcode 1.1.3
checked_yaml 1.0.2
collection 1.14.13
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
dart_style 1.3.6
glob 1.2.0
html 0.14.0+3
js 0.6.2
json_annotation 3.0.1
logging 0.11.4
meta 1.1.8
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pedantic 1.9.0 1.9.1
pub_semver 1.4.4
pubspec_parse 0.1.5
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
build_runner ^1.0.0