package:source_gen
to eliminate the time you spend writing boilerplate serialization code for your models. package:angel3_serialize
also powers package:angel3_orm
.pubspec.yaml
, you need to install the following dependencies:package:build_runner
, you can build models automatically, anywhere in your project structure, by running pub run build_runner build
.build
call with a call to watch
. They take the same parameters.@serializable
annotation to your model class to have it serialized, and a serializable model class's name should also start with a leading underscore.abstract
class to ensure immutability of models.angel3_serialize
does it for you. This means that the main class can have its constructors automatically generated, in addition into serialization functions.Book
model. Create a class named _Book
:book.g.dart
Book
: Extends or implements _Book
; may be const
-enabled.BookSerializer
: static functionality for serializing Book
models.BookFields
: The names of all fields from the Book
model, statically-available.BookEncoder
: Allows BookSerializer
to extend Codec<Book, Map>
.BookDecoder
: Also allows BookSerializer
to extend Codec<Book, Map>
.bookSerializer
: A top-level, const
instance of BookSerializer
.Book.toString
: Prints out all of a Book
instance's fields.2.0.2
, the generated output also includes information about the serialized names of keys on your model class.Map
serializers
:angel3_serialize
pulls in fields from parent classes, as well as implemented interfaces, so it is extremely easy to share attributes among model classes:angel3_serialize
will transform keys into snake case. Use alias
to provide a custom name, or pass autoSnakeCaseNames
: false
to the builder;autoSnakeCaseNames
per model:@exclude
:canSerialize
or canDeserialize
:@required
in the generated constructor, and serializers will check for its presence, throwing a FormatException
if it is missing.package:angel3_serialize
does not cover every known Dart data type; you can add support for your own. Provide serializer
and deserializer
arguments to @SerializableField()
as you see fit.deserializer
will always be dynamic
, while serializer
can receive the data type in question.serializesTo
argument. This lets the generator, as well as the ORM, apply the correct (de)serialization rules and validations.angel3_serialize
also supports a few types of nesting of @serializable
classes:Book myField
List
, ex. List<Book>
Map
, ex. Map<String, Book>
_Book
), or the generated class name (ex Book
):Author
) depends on a model defined in another file (Book
), then you will need to generate book.g.dart
before, author.g.dart
, in a separate build action. This way, the analyzer can resolve the Book
type.id
, createdAt
, and updatedAt
fields for you, in the style of an Angel3 Model
. This will automatically be generated, only for classes extending Model
.package:angel3_serialize
also handles Uint8List
fields, by means of serialization to and from base64
encoding.Serializers.typescript
to your @Serializable()
declaration:_Author
class will generate the following in author.d.ts
:@Exclude()
that specifies canSerialize: false
will not be present in the TypeScript definition. The rationale for this is that if a field (i.e. password
) will never be sent to the client, the client shouldn't even know the field exists.angel3_serialize
can forward custom constructor parameters.