Index: pkg/serialization/test/serialization_test.dart |
diff --git a/pkg/serialization/test/serialization_test.dart b/pkg/serialization/test/serialization_test.dart |
index 206651bca61c89222eb29440daf1e192540710e7..c7879eb8b2a2be0474bf9291ee7d3d4bbb3e40ea 100644 |
--- a/pkg/serialization/test/serialization_test.dart |
+++ b/pkg/serialization/test/serialization_test.dart |
@@ -4,6 +4,7 @@ |
library serialization_test; |
+import 'dart:json' as json; |
import 'package:unittest/unittest.dart'; |
import 'package:serialization/serialization.dart'; |
import 'package:serialization/src/serialization_helpers.dart'; |
@@ -236,6 +237,10 @@ main() { |
runRoundTripTest(nodeSerializerUsingMaps); |
}); |
+ test('round-trip, flat format, using maps', () { |
+ runRoundTripTestFlat(nodeSerializerUsingMaps); |
+ }); |
+ |
test('round-trip with Node CustomRule', () { |
runRoundTripTestFlat(nodeSerializerCustom); |
}); |
@@ -309,7 +314,7 @@ main() { |
constructor: 'withData', |
constructorFields: ["street", "Kirkland", "WA", "98103"], |
fields: []); |
- var out = s.write(a1); |
+ String out = s.write(a1); |
var newAddress = s.read(out); |
expect(newAddress.street, a1.street); |
expect(newAddress.city, "Kirkland"); |
@@ -317,6 +322,73 @@ main() { |
expect(newAddress.zip, "98103"); |
}); |
+ test("Straight JSON format", () { |
+ var s = new Serialization(); |
+ var writer = s.newWriter(new SimpleJsonFormat()); |
+ var out = writer.write(a1); |
+ var reconstituted = json.parse(out); |
+ expect(reconstituted.length, 4); |
+ expect(reconstituted[0], "Seattle"); |
+ }); |
+ |
+ test("Straight JSON format, nested objects", () { |
+ var p1 = new Person()..name = 'Alice'..address = a1; |
+ var s = new Serialization(); |
+ var addressRule = s.addRuleFor(a1)..configureForMaps(); |
+ var personRule = s.addRuleFor(p1)..configureForMaps(); |
+ var writer = s.newWriter(new SimpleJsonFormat(storeRoundTripInfo: true)); |
+ var out = writer.write(p1); |
+ var reconstituted = json.parse(out); |
+ var expected = { |
+ "name" : "Alice", |
+ "rank" : null, |
+ "serialNumber" : null, |
+ "__rule" : personRule.number, |
+ "address" : { |
+ "street" : "N 34th", |
+ "city" : "Seattle", |
+ "state" : null, |
+ "zip" : null, |
+ "__rule" : addressRule.number |
+ } |
+ }; |
+ expect(expected, reconstituted); |
+ }); |
+ |
+ test("Straight JSON format, round-trip", () { |
+ // Note that we can't use the usual round-trip test because it has cycles. |
+ var p1 = new Person()..name = 'Alice'..address = a1; |
+ // Use maps for one rule, lists for the other. |
+ var s = new Serialization() |
+ ..addRuleFor(a1) |
+ ..addRuleFor(p1).configureForMaps(); |
+ var writer = s.newWriter(new SimpleJsonFormat(storeRoundTripInfo: true)); |
+ var out = writer.write(p1); |
+ var reader = s.newReader(new SimpleJsonFormat(storeRoundTripInfo: true)); |
+ var p2 = reader.read(out); |
+ expect(p2.name, "Alice"); |
+ var a2 = p2.address; |
+ expect(a2.street, "N 34th"); |
+ expect(a2.city, "Seattle"); |
+ }); |
+ |
+ test("Straight JSON format, round-trip with named objects", () { |
+ // Note that we can't use the usual round-trip test because it has cycles. |
+ var p1 = new Person()..name = 'Alice'..address = a1; |
+ // Use maps for one rule, lists for the other. |
+ var s = new Serialization() |
+ ..addRule(new NamedObjectRule()) |
+ ..addRuleFor(a1) |
+ ..addRuleFor(p1).configureForMaps() |
+ ..namedObjects["foo"] = a1; |
+ var writer = s.newWriter(new SimpleJsonFormat(storeRoundTripInfo: true)); |
+ var out = writer.write(p1); |
+ var reader = s.newReader(new SimpleJsonFormat(storeRoundTripInfo: true)); |
+ var p2 = reader.read(out, {"foo" : 12}); |
+ expect(p2.name, "Alice"); |
+ var a2 = p2.address; |
+ expect(a2, 12); |
+ }); |
} |
/****************************************************************************** |
@@ -355,9 +427,9 @@ Serialization metaSerialization() { |
* reader. |
*/ |
readBackSimple(Serialization s, object, Reader reader) { |
- var rule = s.rulesFor(object, null)[0]; |
+ var rule = s.rulesFor(object, null).first; |
reader.inflateForRule(rule); |
- var list2 = reader.allObjectsForRule(rule)[0]; |
+ var list2 = reader.allObjectsForRule(rule).first; |
return list2; |
} |
@@ -470,11 +542,11 @@ runRoundTripTestFlat(serializerSetUp) { |
n2.parent = n1; |
n3.parent = n1; |
var s = serializerSetUp(n1); |
- var output = s.writeFlat(n2); |
+ var output = s.write(n2, new SimpleFlatFormat()); |
expect(output is List, isTrue); |
var s2 = serializerSetUp(n1); |
- var reader = new Reader(s2); |
- var m2 = reader.readFlat(output); |
+ var reader = new Reader(s2, new SimpleFlatFormat()); |
+ var m2 = reader.read(output); |
var m1 = m2.parent; |
expect(m1 is Node, isTrue); |
var children = m1.children; |
@@ -495,7 +567,7 @@ states(object, Serialization s) { |
/** A hard-coded rule for serializing Node instances. */ |
class NodeRule extends CustomRule { |
- bool appliesTo(instance, _) => instance is Node; |
+ bool appliesTo(instance, _) => instance.runtimeType == Node; |
getState(instance) => [instance.parent, instance.name, instance.children]; |
create(state) => new Node(state[1]); |
setState(Node node, state) { |