Serialization - Flutter SDK
On this page
The Atlas Device SDK for Flutter supports serialization and deserialization of Extended JSON (EJSON) to and from static Realm objects.
Supported Data Types for Serialization
The Flutter SDK currently supports serialization of the following supported data types:
All Dart-language data types
All Realm-specific data types, except Decimal128 and RealmValue
The following table illustrates how the SDK's Realm-specific data types serialize with output examples:
Realm Type | Serializes To |
---|---|
DateTime | Date
|
RealmList | Array
|
RealmMap | Array
|
RealmSet | Array
|
ObjectId | ObjectId
|
UUID | Binary
|
Uint8List | Binary
|
Object | Document
|
For more information on the serialization of non-Realm specific types, see BSON Data Types and Associated Representations.
Flutter SDK serialization does not currently support the following BSON types: Code, CodeWScope, DBPointer, DBRef, Regular Expression, and Timestamp.
Serialize Realm Objects
The SDK's full-document encoder enables you to serialize and deserialize user-defined classes.
To use the encoder, create your object model as you normally would using
the @RealmModel()
annotation. The RealmObject
class
model created by your part
declaration provides the necessary methods
for serialization and deserialization.
The following Pet
object model will be used in the examples on this page:
import 'package:realm_dart/realm.dart'; part 'pet.realm.dart'; ()class _Pet { late String type; late int numberOfLegs; late DateTime birthDate; late double? price; }
Serialize to EJSON
For objects based on RealmObject
classes, you can serialize to EJSON using the
toEjson() method in the following two ways:
// Pass the object as a parameter to the method EJsonValue serializeByParam = toEJson(spider); // Call the method directly on the object EJsonValue serializeWithCall = spider.toEJson();
{ type: Jumping Spider, numberOfLegs: {$numberInt: 8}, birthDate: {$date: {$numberLong: 1712707200000}}, price: null }
Deserialize from EJSON
Deserialize from EJSON using the fromEjson() method. The method takes EJSON for a specified object type as input and outputs a deserialized instance of the specified object type.
The following example deserializes serializeByParam
from the previous example:
// Pass the serialized object to the method final deserializeFromEjsonWithExplicitType = fromEJson<Pet>(serializeByParam); // The method can also infer the object type Pet deserializeFromEjson = fromEJson(serializeByParam);
Serialize Non-Realm Objects
For non-Realm classes, you can use the @ejson
annotation on the class constructor
to generate a decoder and encoder:
class Person { final String name; final DateTime birthDate; final int? age; final double income; final Person? spouse; // annotate constructor to generate decoder and encoder Person(this.name, this.birthDate, this.income, {this.spouse, this.age}); }
Register Custom Codecs
The SDK also supports custom EJSON codecs. To use them in your app, register the custom EJSON encoder and decoder for the specified type.