Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Unified Diff: pkg/serialization/test/serialization_test.dart

Issue 12210151: Fix modification of map while iterating in SimpleJsonFormat (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changes from review comments Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/serialization/lib/src/format.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/serialization/test/serialization_test.dart
diff --git a/pkg/serialization/test/serialization_test.dart b/pkg/serialization/test/serialization_test.dart
index d45f562ded99ed1e0558d3b2b1d0ec375bdfbc5b..5a9cc491c6b54e1f706df9c92d007b9c32a6621c 100644
--- a/pkg/serialization/test/serialization_test.dart
+++ b/pkg/serialization/test/serialization_test.dart
@@ -386,6 +386,20 @@ main() {
expect(a2.city, "Seattle");
});
+ test("Straight JSON format, non-string key", () {
+ // This tests what happens if we have a key that's not a string. That's
+ // not allowed by json, so we don't actually turn it into a json string,
+ // but someone might reasonably convert to a json-able structure without
+ // going through the string representation.
+ var p1 = new Person()..name = 'Alice'..address = a1;
+ var s = new Serialization()
+ ..addRule(new PersonRuleReturningMapWithNonStringKey());
+ var p2 = writeAndReadBack(s,
+ new SimpleJsonFormat(storeRoundTripInfo: true), p1);
+ expect(p2.name, "Alice");
+ expect(p2.address.street, "N 34th");
+ });
+
test("Root is a Map", () {
// Note that we can't use the usual round-trip test because it has cycles.
var p1 = new Person()..name = 'Alice'..address = a1;
@@ -704,3 +718,31 @@ class NodeRule extends CustomRule {
node.children = state[2];
}
}
+
+/**
+ * This is a rather silly rule which stores the address data in a map,
+ * but inverts the keys and values, so we look up values and find the
+ * corresponding key. This will lead to maps that aren't allowed in JSON,
+ * and which have keys that need to be dereferenced.
+ */
+class PersonRuleReturningMapWithNonStringKey extends CustomRule {
+ appliesTo(instance, _) => instance is Person;
+ getState(instance) {
+ return new Map()
+ ..[instance.name] = "name"
+ ..[instance.address] = "address";
+ }
+ create(state) => new Person();
+ setState(Person a, state) {
+ a.name = findValue("name", state);
+ a.address = findValue("address", state);
+ }
+ findValue(String key, Map state) {
+ var answer;
+ for (var each in state.keys) {
+ var value = state[each];
+ if (value == key) return each;
+ }
+ return null;
+ }
+}
« no previous file with comments | « pkg/serialization/lib/src/format.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698