From e3b1dc86ba6de41cb051b87bfbb3dad0faa37341 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 2 Mar 2018 13:55:03 -0500 Subject: [PATCH] Updated README further --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index 41b9c181..27c5ecf9 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ the time you spend writing boilerplate serialization code for your models. * [Models](#models) * [Field Aliases](#aliases) * [Excluding Keys](#excluding-keys) + * [Serialization](#serializaition) * [Nesting](#nesting) * [ID and Date Fields](#id-and-dates) @@ -79,6 +80,44 @@ abstract class _Book extends Model { The following files will be generated: * `book.g.dart` * `book.serializer.g.dart` + +# Serialization + +You can use the generated files as follows: + +```dart +myFunction() { + var warAndPeace = new Book( + author: 'Leo Tolstoy', + title: 'War and Peace', + description: 'You will cry after reading this.', + pageCount: 1225 + ); + + // Easily serialize models into Maps + var map = BookSerializer.toMap(warAndPeace); + + // Also deserialize from Maps + var book = BookSerialize.fromMap(map); + print(book.title); // 'War and Peace' + + // For compatibility with `JSON.encode`, a `toJson` method + // is included that forwards to `BookSerializer.toMap`: + expect(book.toJson(), map); +} +``` +## Customizing Serialization +Currently, these serialization methods are supported: + * to `Map` + * to JSON + +You can customize these by means of `serializers`: + +```dart +@Serializable(serializers: const [Serializers.map, Serializers.json]) +class _MyClass extends Model {} +``` + ## Aliases Whereas Dart fields conventionally are camelCased, most database columns