| 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 * ## Installing ## | 10 * ## Installing ## |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 * [pub]: http://pub.dartlang.org | 203 * [pub]: http://pub.dartlang.org |
| 204 * [pkg]: http://pub.dartlang.org/packages/serialization | 204 * [pkg]: http://pub.dartlang.org/packages/serialization |
| 205 */ | 205 */ |
| 206 library serialization; | 206 library serialization; |
| 207 | 207 |
| 208 import 'src/mirrors_helpers.dart'; | 208 import 'src/mirrors_helpers.dart'; |
| 209 import 'src/serialization_helpers.dart'; | 209 import 'src/serialization_helpers.dart'; |
| 210 import 'dart:async'; | 210 import 'dart:async'; |
| 211 import 'dart:json' as json; | 211 import 'dart:json' as json; |
| 212 import 'dart:collection'; | 212 import 'dart:collection'; |
| 213 import 'dart:uri'; |
| 213 | 214 |
| 214 part 'src/reader_writer.dart'; | 215 part 'src/reader_writer.dart'; |
| 215 part 'src/serialization_rule.dart'; | 216 part 'src/serialization_rule.dart'; |
| 216 part 'src/basic_rule.dart'; | 217 part 'src/basic_rule.dart'; |
| 217 part 'src/format.dart'; | 218 part 'src/format.dart'; |
| 218 | 219 |
| 219 /** | 220 /** |
| 220 * This class defines a particular serialization scheme, in terms of | 221 * This class defines a particular serialization scheme, in terms of |
| 221 * [SerializationRule] instances, and supports reading and writing them. | 222 * [SerializationRule] instances, and supports reading and writing them. |
| 222 * See library comment for examples of usage. | 223 * See library comment for examples of usage. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 251 /** | 252 /** |
| 252 * When we write out data using this serialization, should we also write | 253 * When we write out data using this serialization, should we also write |
| 253 * out a description of the rules. This is on by default unless using | 254 * out a description of the rules. This is on by default unless using |
| 254 * CustomRule subclasses, in which case it requires additional setup and | 255 * CustomRule subclasses, in which case it requires additional setup and |
| 255 * is off by default. | 256 * is off by default. |
| 256 */ | 257 */ |
| 257 bool get selfDescribing { | 258 bool get selfDescribing { |
| 258 // TODO(alanknight): Should this be moved to the format? | 259 // TODO(alanknight): Should this be moved to the format? |
| 259 // TODO(alanknight): Allow self-describing in the presence of CustomRule. | 260 // TODO(alanknight): Allow self-describing in the presence of CustomRule. |
| 260 if (_selfDescribing != null) return _selfDescribing; | 261 if (_selfDescribing != null) return _selfDescribing; |
| 261 return !rules.any((x) => x is CustomRule); | 262 _selfDescribing = !rules.any((x) => x is CustomRule && x is! SymbolRule); |
| 263 return _selfDescribing; |
| 262 } | 264 } |
| 263 | 265 |
| 264 /** | 266 /** |
| 265 * When we write out data using this serialization, should we also write | 267 * When we write out data using this serialization, should we also write |
| 266 * out a description of the rules. This is on by default unless using | 268 * out a description of the rules. This is on by default unless using |
| 267 * CustomRule subclasses, in which case it requires additional setup and | 269 * CustomRule subclasses, in which case it requires additional setup and |
| 268 * is off by default. | 270 * is off by default. |
| 269 */ | 271 */ |
| 270 void set selfDescribing(bool value) { | 272 void set selfDescribing(bool value) { |
| 271 _selfDescribing = value; | 273 _selfDescribing = value; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 * subclass. | 314 * subclass. |
| 313 */ | 315 */ |
| 314 // TODO(alanknight): Take a type rather than an instance. Issue 6282 and 6433. | 316 // TODO(alanknight): Take a type rather than an instance. Issue 6282 and 6433. |
| 315 BasicRule addRuleFor( | 317 BasicRule addRuleFor( |
| 316 instanceOfType, | 318 instanceOfType, |
| 317 {String constructor, | 319 {String constructor, |
| 318 List constructorFields, | 320 List constructorFields, |
| 319 List<String> fields, | 321 List<String> fields, |
| 320 List<String> excludeFields}) { | 322 List<String> excludeFields}) { |
| 321 | 323 |
| 322 var rule; | 324 var rule = new BasicRule( |
| 323 rule = new BasicRule( | |
| 324 turnInstanceIntoSomethingWeCanUse( | 325 turnInstanceIntoSomethingWeCanUse( |
| 325 instanceOfType), | 326 instanceOfType), |
| 326 constructor, constructorFields, fields, excludeFields); | 327 constructor, constructorFields, fields, excludeFields); |
| 327 addRule(rule); | 328 addRule(rule); |
| 328 return rule; | 329 return rule; |
| 329 } | 330 } |
| 330 | 331 |
| 331 /** Set up the default rules, for lists and primitives. */ | 332 /** Set up the default rules, for lists and primitives. */ |
| 332 void addDefaultRules() { | 333 void addDefaultRules() { |
| 333 addRule(new PrimitiveRule()); | 334 addRule(new PrimitiveRule()); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 ..addRuleFor(new MapRule()) | 458 ..addRuleFor(new MapRule()) |
| 458 ..addRuleFor(new PrimitiveRule()) | 459 ..addRuleFor(new PrimitiveRule()) |
| 459 ..addRuleFor(new ListRuleEssential()) | 460 ..addRuleFor(new ListRuleEssential()) |
| 460 ..addRuleFor(basicRule, | 461 ..addRuleFor(basicRule, |
| 461 constructorFields: ['type', | 462 constructorFields: ['type', |
| 462 'constructorName', | 463 'constructorName', |
| 463 'constructorFields', 'regularFields', []], | 464 'constructorFields', 'regularFields', []], |
| 464 fields: []) | 465 fields: []) |
| 465 ..addRule(new NamedObjectRule()) | 466 ..addRule(new NamedObjectRule()) |
| 466 ..addRule(new MirrorRule()) | 467 ..addRule(new MirrorRule()) |
| 467 ..addRule(new SymbolRule()); | 468 ..addRuleFor(new MirrorRule()) |
| 469 ..addRuleFor(new SymbolRule()); |
| 468 meta.namedObjects = namedObjects; | 470 meta.namedObjects = namedObjects; |
| 469 return meta; | 471 return meta; |
| 470 } | 472 } |
| 471 | 473 |
| 472 /** Return true if our [namedObjects] collection has an entry for [object].*/ | 474 /** Return true if our [namedObjects] collection has an entry for [object].*/ |
| 473 bool _hasNameFor(object) { | 475 bool _hasNameFor(object) { |
| 474 var sentinel = const _Sentinel(); | 476 var sentinel = const _Sentinel(); |
| 475 return _nameFor(object, () => sentinel) != sentinel; | 477 return _nameFor(object, () => sentinel) != sentinel; |
| 476 } | 478 } |
| 477 | 479 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 488 } | 490 } |
| 489 | 491 |
| 490 /** | 492 /** |
| 491 * An exception class for errors during serialization. | 493 * An exception class for errors during serialization. |
| 492 */ | 494 */ |
| 493 class SerializationException implements Exception { | 495 class SerializationException implements Exception { |
| 494 final String message; | 496 final String message; |
| 495 const SerializationException([this.message]); | 497 const SerializationException([this.message]); |
| 496 toString() => "SerializationException($message)"; | 498 toString() => "SerializationException($message)"; |
| 497 } | 499 } |
| OLD | NEW |