| 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 /** | 5 /** |
| 6 * This provides a general-purpose serialization facility for Dart objects. A | 6 * This provides a general-purpose serialization facility for Dart objects. A |
| 7 * [Serialization] is defined in terms of [SerializationRule]s and supports | 7 * [Serialization] is defined in terms of [SerializationRule]s and supports |
| 8 * reading and writing to different formats. | 8 * reading and writing to different formats. |
| 9 * | 9 * |
| 10 * Setup | 10 * Setup |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 } | 312 } |
| 313 | 313 |
| 314 /** Set up the default rules, for lists and primitives. */ | 314 /** Set up the default rules, for lists and primitives. */ |
| 315 void addDefaultRules() { | 315 void addDefaultRules() { |
| 316 addRule(new PrimitiveRule()); | 316 addRule(new PrimitiveRule()); |
| 317 addRule(new ListRule()); | 317 addRule(new ListRule()); |
| 318 // Both these rules apply to lists, so unless otherwise indicated, | 318 // Both these rules apply to lists, so unless otherwise indicated, |
| 319 // it will always find the first one. | 319 // it will always find the first one. |
| 320 addRule(new ListRuleEssential()); | 320 addRule(new ListRuleEssential()); |
| 321 addRule(new MapRule()); | 321 addRule(new MapRule()); |
| 322 addRule(new SymbolRule()); |
| 322 } | 323 } |
| 323 | 324 |
| 324 /** | 325 /** |
| 325 * Add a new SerializationRule [rule]. The addRuleFor method will probably | 326 * Add a new SerializationRule [rule]. The addRuleFor method will probably |
| 326 * handle most simple cases, but for adding an arbitrary rule, including | 327 * handle most simple cases, but for adding an arbitrary rule, including |
| 327 * a SerializationRule subclass which you have created, you can use this | 328 * a SerializationRule subclass which you have created, you can use this |
| 328 * method. | 329 * method. |
| 329 */ | 330 */ |
| 330 void addRule(SerializationRule rule) { | 331 void addRule(SerializationRule rule) { |
| 331 rule.number = _rules.length; | 332 rule.number = _rules.length; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 ..addRuleFor(new ListRule()) | 439 ..addRuleFor(new ListRule()) |
| 439 ..addRuleFor(new MapRule()) | 440 ..addRuleFor(new MapRule()) |
| 440 ..addRuleFor(new PrimitiveRule()) | 441 ..addRuleFor(new PrimitiveRule()) |
| 441 ..addRuleFor(new ListRuleEssential()) | 442 ..addRuleFor(new ListRuleEssential()) |
| 442 ..addRuleFor(basicRule, | 443 ..addRuleFor(basicRule, |
| 443 constructorFields: ['type', | 444 constructorFields: ['type', |
| 444 'constructorName', | 445 'constructorName', |
| 445 'constructorFields', 'regularFields', []], | 446 'constructorFields', 'regularFields', []], |
| 446 fields: []) | 447 fields: []) |
| 447 ..addRule(new NamedObjectRule()) | 448 ..addRule(new NamedObjectRule()) |
| 448 ..addRule(new MirrorRule()); | 449 ..addRule(new MirrorRule()) |
| 450 ..addRule(new SymbolRule()); |
| 449 meta.namedObjects = namedObjects; | 451 meta.namedObjects = namedObjects; |
| 450 return meta; | 452 return meta; |
| 451 } | 453 } |
| 452 | 454 |
| 453 /** Return true if our [namedObjects] collection has an entry for [object].*/ | 455 /** Return true if our [namedObjects] collection has an entry for [object].*/ |
| 454 bool _hasNameFor(object) { | 456 bool _hasNameFor(object) { |
| 455 var sentinel = const _Sentinel(); | 457 var sentinel = const _Sentinel(); |
| 456 return _nameFor(object, () => sentinel) != sentinel; | 458 return _nameFor(object, () => sentinel) != sentinel; |
| 457 } | 459 } |
| 458 | 460 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 469 } | 471 } |
| 470 | 472 |
| 471 /** | 473 /** |
| 472 * An exception class for errors during serialization. | 474 * An exception class for errors during serialization. |
| 473 */ | 475 */ |
| 474 class SerializationException implements Exception { | 476 class SerializationException implements Exception { |
| 475 final String message; | 477 final String message; |
| 476 const SerializationException([this.message]); | 478 const SerializationException([this.message]); |
| 477 toString() => "SerializationException($message)"; | 479 toString() => "SerializationException($message)"; |
| 478 } | 480 } |
| OLD | NEW |