| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library serialization_test; | 5 library serialization_test; |
| 6 | 6 |
| 7 import 'dart:json' as json; | 7 import 'dart:json' as json; |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 import 'package:serialization/serialization.dart'; | 9 import 'package:serialization/serialization.dart'; |
| 10 import 'package:serialization/src/serialization_helpers.dart'; | 10 import 'package:serialization/src/serialization_helpers.dart'; |
| (...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 states(object, Serialization s) { | 741 states(object, Serialization s) { |
| 742 var rules = s.rulesFor(object, null); | 742 var rules = s.rulesFor(object, null); |
| 743 return rules.map((x) => x.extractState(object, doNothing, null)).toList(); | 743 return rules.map((x) => x.extractState(object, doNothing, null)).toList(); |
| 744 } | 744 } |
| 745 | 745 |
| 746 /** A hard-coded rule for serializing Node instances. */ | 746 /** A hard-coded rule for serializing Node instances. */ |
| 747 class NodeRule extends CustomRule { | 747 class NodeRule extends CustomRule { |
| 748 bool appliesTo(instance, _) => instance.runtimeType == Node; | 748 bool appliesTo(instance, _) => instance.runtimeType == Node; |
| 749 getState(instance) => [instance.parent, instance.name, instance.children]; | 749 getState(instance) => [instance.parent, instance.name, instance.children]; |
| 750 create(state) => new Node(state[1]); | 750 create(state) => new Node(state[1]); |
| 751 setState(Node node, state) { | 751 void setState(Node node, state) { |
| 752 node.parent = state[0]; | 752 node.parent = state[0]; |
| 753 node.children = state[2]; | 753 node.children = state[2]; |
| 754 } | 754 } |
| 755 } | 755 } |
| 756 | 756 |
| 757 /** | 757 /** |
| 758 * This is a rather silly rule which stores the address data in a map, | 758 * This is a rather silly rule which stores the address data in a map, |
| 759 * but inverts the keys and values, so we look up values and find the | 759 * but inverts the keys and values, so we look up values and find the |
| 760 * corresponding key. This will lead to maps that aren't allowed in JSON, | 760 * corresponding key. This will lead to maps that aren't allowed in JSON, |
| 761 * and which have keys that need to be dereferenced. | 761 * and which have keys that need to be dereferenced. |
| 762 */ | 762 */ |
| 763 class PersonRuleReturningMapWithNonStringKey extends CustomRule { | 763 class PersonRuleReturningMapWithNonStringKey extends CustomRule { |
| 764 appliesTo(instance, _) => instance is Person; | 764 appliesTo(instance, _) => instance is Person; |
| 765 getState(instance) { | 765 getState(instance) { |
| 766 return new Map() | 766 return new Map() |
| 767 ..[instance.name] = "name" | 767 ..[instance.name] = "name" |
| 768 ..[instance.address] = "address"; | 768 ..[instance.address] = "address"; |
| 769 } | 769 } |
| 770 create(state) => new Person(); | 770 create(state) => new Person(); |
| 771 setState(Person a, state) { | 771 void setState(Person a, state) { |
| 772 a.name = findValue("name", state); | 772 a.name = findValue("name", state); |
| 773 a.address = findValue("address", state); | 773 a.address = findValue("address", state); |
| 774 } | 774 } |
| 775 findValue(String key, Map state) { | 775 findValue(String key, Map state) { |
| 776 var answer; | 776 var answer; |
| 777 for (var each in state.keys) { | 777 for (var each in state.keys) { |
| 778 var value = state[each]; | 778 var value = state[each]; |
| 779 if (value == key) return each; | 779 if (value == key) return each; |
| 780 } | 780 } |
| 781 return null; | 781 return null; |
| 782 } | 782 } |
| 783 } | 783 } |
| OLD | NEW |