El Atlas Device SDK para Flutter admite la serialización y deserialización de JSON extendido (EJSON) hacia y desde objetos Realm estáticos.
Tipos de datos admitidos para la serialización
El SDK de Flutter actualmente soporta la serialización de los siguientes tipos de datos soportados:
All Dart-language data types
All Realm-specific data types, except Decimal128 and RealmValue
La siguiente tabla ilustra cómo los tipos de datos específicos de Realm del SDK se serializan con ejemplos de salida:
Tipo de Realm | Serializa a |
|---|---|
Fecha y hora | fecha
|
RealmList | Arreglo
|
RealmMap | Arreglo
|
RealmSet | Arreglo
|
ObjectId | ObjectId
|
UUID | Binario
|
Uint8List | Binario
|
Objeto | Documento
|
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.
Serializar objetos de Realm
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; }
Serializar a EJSON
Para objetos basados en RealmObject clases, puede serializarlos en EJSON usando toEjson() método de las dos maneras siguientes:
// 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
Deserialice desde EJSON usando el método fromEjson(). El método toma EJSON para un tipo de objeto Realm específico como entrada y emite una instancia deserializada del tipo de objeto Realm específico.
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
Para clases que no sean de Realm, se puede usar la anotación @ejson en el constructor de la clase para generar un decodificador y un codificador:
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.