-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOCSP-29928 Serialization for Dart (#3235)
## Pull Request Info Jira ticket: https://jira.mongodb.org/browse/DOCSP-29928 - [Serialization](https://preview-mongodblindseymoore.gatsbyjs.io/realm/DOCSP-29928/sdk/flutter/realm-database/serialization/): New page documenting serialization ### Reminder Checklist Before merging your PR, make sure to check a few things. - [x] Did you tag pages appropriately? - genre - meta.keywords - meta.description - [x] Describe your PR's changes in the Release Notes section - [ ] Create a Jira ticket for related docs-app-services work, if any ### Release Notes <!-- - **Kotlin** SDK - Realm/Manage Realm Files/Encrypt a Realm: Add information on encryption for local and synced realms. --> **Flutter SDK** - Serialization: Add new page for serialization of objects to and from EJSON. ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
- Loading branch information
1 parent
1e41ff2
commit 674381d
Showing
11 changed files
with
787 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:realm_dart/realm.dart'; | ||
|
||
// :snippet-start: ejson-annotation | ||
class Person { | ||
final String name; | ||
final DateTime birthDate; | ||
|
||
final int? age; | ||
final double income; | ||
final Person? spouse; | ||
|
||
@ejson // annotate constructor to generate decoder and encoder | ||
Person(this.name, this.birthDate, this.income, {this.spouse, this.age}); | ||
} | ||
// :snippet-end: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// :snippet-start: serialize-object-model | ||
import 'package:realm_dart/realm.dart'; | ||
|
||
part 'pet.realm.dart'; | ||
|
||
@RealmModel() | ||
class _Pet { | ||
late String type; | ||
late int numberOfLegs; | ||
late DateTime birthDate; | ||
|
||
late double? price; | ||
} | ||
// :snippet-end: |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import 'package:test/test.dart'; | ||
import 'package:realm_dart/realm.dart'; | ||
import 'utils.dart'; | ||
import 'dart:typed_data'; | ||
|
||
import './pet.dart'; | ||
part 'serialization_test.realm.dart'; | ||
|
||
@RealmModel(ObjectType.embeddedObject) | ||
class _Address { | ||
late String street; | ||
late String city; | ||
late String state; | ||
late String country; | ||
} | ||
|
||
@RealmModel() | ||
class _SerializeModel { | ||
@PrimaryKey() | ||
// ObjectID | ||
late ObjectId id; | ||
|
||
// UUID | ||
late Uuid myId; | ||
|
||
// Array | ||
late List<String> listOfStrings; | ||
late Set<int> setOfInts; | ||
late Map<String, int> mapOfMixedAnyValues; | ||
|
||
// Binary | ||
late Uint8List requiredBinaryProperty; | ||
|
||
// Boolean | ||
bool isElectric = false; | ||
|
||
// Date | ||
late DateTime dateLastServiced; | ||
|
||
// Document | ||
late _Address? address; | ||
|
||
// Double | ||
double milesDriven = 126.0; | ||
|
||
// Int32 and Int64 | ||
int number32 = 900; | ||
int number64 = 922393736854775807; | ||
|
||
// String | ||
late String licensePlate; | ||
|
||
// Null | ||
int? a; | ||
|
||
// Missing encoders for RealmValue and Decimal 128, engineering ticket number: | ||
// RDART-1063 (https://jira.mongodb.org/browse/RDART-1063) | ||
} | ||
|
||
main() { | ||
test('data types', () { | ||
|
||
final config = Configuration.local([SerializeModel.schema, Address.schema]); | ||
final realm = Realm(config); | ||
|
||
final example = SerializeModel( | ||
ObjectId(), Uuid.v4(), Uint8List.fromList([1, 2]), DateTime.utc(2024, 4, 10), | ||
'90ZXWYZL', listOfStrings: ['food', 'water'], setOfInts: {0, 1, 2, 3}, | ||
mapOfMixedAnyValues: {'first': 123 , 'second': 567}, | ||
address: Address("500 Dean Street", "Brooklyn", "NY", "USA")); | ||
|
||
realm.write(() { | ||
realm.add(example); | ||
}); | ||
|
||
print(example); | ||
|
||
EJsonValue exSerialize = toEJson(example); | ||
|
||
print(exSerialize); | ||
|
||
cleanUpRealm(realm); | ||
|
||
}); | ||
|
||
test('serialize', () { | ||
|
||
final config = Configuration.local([Pet.schema]); | ||
final realm = Realm(config); | ||
|
||
// create pet spider object | ||
final spider = Pet('Jumping Spider', 8, DateTime.utc(2024, 4, 10)); | ||
|
||
realm.write(() { | ||
realm.add(spider); | ||
}); | ||
|
||
// :snippet-start: serialize | ||
// Pass the object as a parameter to the method | ||
EJsonValue serializeByParam = toEJson(spider); | ||
|
||
// Call the method directly on the object | ||
EJsonValue serializeWithCall = spider.toEJson(); | ||
// :snippet-end: | ||
|
||
print(serializeByParam); | ||
|
||
final birthDate = DateTime.utc(2024, 4, 10); | ||
|
||
// make sure serialized value matches expected | ||
expect(serializeByParam, { | ||
'type': 'Jumping Spider', | ||
'numberOfLegs': {'\$numberInt': '8'}, | ||
'birthDate': { | ||
'\$date': {'\$numberLong': birthDate.millisecondsSinceEpoch.toString()} | ||
}, | ||
'price': null, | ||
}); | ||
|
||
// make sure two methods of serialization match | ||
expect(serializeByParam, serializeWithCall); | ||
|
||
cleanUpRealm(realm); | ||
}); | ||
|
||
test('deserialize', () { | ||
|
||
final config = Configuration.local([Pet.schema]); | ||
final realm = Realm(config); | ||
|
||
// create pet spider object | ||
final spider = Pet('Jumping Spider', 8, DateTime.utc(2024, 4, 10)); | ||
|
||
realm.write(() { | ||
realm.add(spider); | ||
}); | ||
|
||
// Pass the object as a parameter to the method | ||
EJsonValue serializeByParam = toEJson(spider); | ||
|
||
// :snippet-start: deserialize | ||
// Pass the serialized object to the method | ||
final deserializeFromEjsonWithExplicitType = fromEJson<Pet>(serializeByParam); | ||
|
||
// The method can also infer the object type | ||
Pet deserializeFromEjson = fromEJson(serializeByParam); | ||
// :snippet-end: | ||
|
||
print(deserializeFromEjson); | ||
|
||
// make sure deserialized returns proper instance of Pet object | ||
expect(deserializeFromEjson.type, spider.type); | ||
|
||
cleanUpRealm(realm); | ||
}); | ||
} |
Oops, something went wrong.