Skip to main content

json_factory_runtime_generator 0.3.2 copy "json_factory_runtime_generator: ^0.3.2" to clipboard
json_factory_runtime_generator: ^0.3.2 copied to clipboard

Build-time JSON factory generator for json_serializable classes. Decodes JSON to the right class from a key in the JSON, with no base class, mixin or toJson changes.

example/README.md

Example #

A small project that uses json_factory_runtime_generator.

  • lib/models/: User and Address in the default JsonFactoryMixin mixin (Address also accepts its old key 'location'), Order, ApiUser and ApiEvent (key from an enum @JsonValue) in ApiMixin.
  • build.yaml: writes the mixins to lib/src/factories/json_factory_runtime.g.dart.
  • lib/factories.dart: classes that mix them in. The generated JsonFactoryMapper and ApiMapper classes can be called directly instead.
  • test/factory_test.dart: round-trips JSON through the factories.
@JsonSerializable()
class User {
  final String id;
  final String name;

  const User({required this.id, required this.name});

  @jsonFactoryKey()
  final String factoryKey = 'user';

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}
final json = const User(id: '1', name: 'Ada').toJson();
// {id: 1, name: Ada, __type__: user}

final user = JsonFactoryMapper.fromKeyedJsonAs<User>(json);

Run it:

dart pub get
dart run build_runner build
dart test
1
likes
160
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Build-time JSON factory generator for json_serializable classes. Decodes JSON to the right class from a key in the JSON, with no base class, mixin or toJson changes.

Repository (GitHub)
View/report issues

Topics

#json #serialization #codegen #build-runner #polymorphism

License

MIT (license)

Dependencies

analyzer, build, dart_style, glob, json_factory_runtime_annotation, source_gen

More

Packages that depend on json_factory_runtime_generator