RealmJson class

JSON <-> RealmObject serialization helpers with automatic nested object support.

Serialization Strategy (in order of preference):

  1. Generated toEJson() - Best option, automatically handles all nested RealmObjects
  2. Schema introspection - Auto-detects properties when toEJson() unavailable
  3. Explicit property lists - Manual specification for edge cases

Deserialization Strategy:

  1. fromEJsonMap<T>() - Recommended, uses generated fromEJson() for automatic nested object handling
  2. fromJsonWith<T>() - Manual deserialization with explicit property lists

Round-Trip Serialization:

// Initialize schemas
ChatUser.schema;
ChatRoom.schema;

// Serialize
final json = RealmJson.toJsonWith(chatRoom, null);

// Deserialize (round-trip)
final restored = RealmJson.fromEJsonMap<ChatRoom>(json);

Supported types:

  • Primitives: String, int, double, bool
  • DateTime (UTC converted to/from ISO-8601)
  • ObjectId, Uuid (string representation)
  • Decimal128 (string representation)
  • Uint8List (base64 encoding)
  • RealmValue (mixed type with type metadata)
  • Collections: RealmList, RealmSet, RealmMap
  • Relationships: Embedded objects, to-one, to-many (fully recursive)
  • Backlinks are automatically skipped during serialization

Constructors

RealmJson()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

fromEJsonMap<T extends dynamic>(Map<String, dynamic> json, {T create()?, List<String>? propertyNames, Map<String, dynamic Function(Map<String, dynamic>)>? embeddedCreators}) → T
Deserialize a RealmObject from JSON using generated fromEJson() method.
fromJsonWith<T extends dynamic>(Map<String, dynamic> json, T create(), List<String> propertyNames, {Map<String, dynamic Function(Map<String, dynamic>)>? embeddedCreators}) → T
Hydrate a RealmObject from JSON using explicit property names.
toJsonWith(dynamic obj, List<String>? propertyNames, {Map<String, List<String>>? embeddedProperties}) Map<String, dynamic>
Serialize a RealmObject into JSON using the provided property names.