| 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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 * or as an additional parameter to the reading methods. | 142 * or as an additional parameter to the reading methods. |
| 143 * | 143 * |
| 144 * new Serialization() | 144 * new Serialization() |
| 145 * ..addRuleFor(new Person(), constructorFields: ["name"]) | 145 * ..addRuleFor(new Person(), constructorFields: ["name"]) |
| 146 * ..externalObjects['Person'] = reflect(new Person()).type; | 146 * ..externalObjects['Person'] = reflect(new Person()).type; |
| 147 */ | 147 */ |
| 148 library serialization; | 148 library serialization; |
| 149 | 149 |
| 150 import 'src/mirrors_helpers.dart'; | 150 import 'src/mirrors_helpers.dart'; |
| 151 import 'src/serialization_helpers.dart'; | 151 import 'src/serialization_helpers.dart'; |
| 152 import 'dart:json' show JSON; | 152 import 'dart:async'; |
| 153 import 'dart:json' as json; |
| 153 | 154 |
| 154 part 'src/reader_writer.dart'; | 155 part 'src/reader_writer.dart'; |
| 155 part 'src/serialization_rule.dart'; | 156 part 'src/serialization_rule.dart'; |
| 156 part 'src/basic_rule.dart'; | 157 part 'src/basic_rule.dart'; |
| 157 | 158 |
| 158 /** | 159 /** |
| 159 * This class defines a particular serialization scheme, in terms of | 160 * This class defines a particular serialization scheme, in terms of |
| 160 * [SerializationRule] instances, and supports reading and writing them. | 161 * [SerializationRule] instances, and supports reading and writing them. |
| 161 * See library comment for examples of usage. | 162 * See library comment for examples of usage. |
| 162 */ | 163 */ |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 bool _selfDescribing; | 195 bool _selfDescribing; |
| 195 | 196 |
| 196 /** | 197 /** |
| 197 * When we write out data using this serialization, should we also write | 198 * When we write out data using this serialization, should we also write |
| 198 * out a description of the rules. This is on by default unless using | 199 * out a description of the rules. This is on by default unless using |
| 199 * CustomRule subclasses, in which case it requires additional setup and | 200 * CustomRule subclasses, in which case it requires additional setup and |
| 200 * is off by default. | 201 * is off by default. |
| 201 */ | 202 */ |
| 202 bool get selfDescribing { | 203 bool get selfDescribing { |
| 203 if (_selfDescribing != null) return _selfDescribing; | 204 if (_selfDescribing != null) return _selfDescribing; |
| 204 return !_rules.some((x) => x is CustomRule); | 205 return !_rules.any((x) => x is CustomRule); |
| 205 } | 206 } |
| 206 | 207 |
| 207 /** | 208 /** |
| 208 * When we write out data using this serialization, should we also write | 209 * When we write out data using this serialization, should we also write |
| 209 * out a description of the rules. This is on by default unless using | 210 * out a description of the rules. This is on by default unless using |
| 210 * CustomRule subclasses, in which case it requires additional setup and | 211 * CustomRule subclasses, in which case it requires additional setup and |
| 211 * is off by default. | 212 * is off by default. |
| 212 */ | 213 */ |
| 213 set selfDescribing(x) => _selfDescribing = x; | 214 set selfDescribing(x) => _selfDescribing = x; |
| 214 | 215 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 // consider having the arbitrary predicates be secondary to an initial | 356 // consider having the arbitrary predicates be secondary to an initial |
| 356 // class-based lookup mechanism. | 357 // class-based lookup mechanism. |
| 357 var target, candidateRules; | 358 var target, candidateRules; |
| 358 if (object is DesignatedRuleForObject) { | 359 if (object is DesignatedRuleForObject) { |
| 359 target = object.target; | 360 target = object.target; |
| 360 candidateRules = object.possibleRules(_rules); | 361 candidateRules = object.possibleRules(_rules); |
| 361 } else { | 362 } else { |
| 362 target = object; | 363 target = object; |
| 363 candidateRules = _rules; | 364 candidateRules = _rules; |
| 364 } | 365 } |
| 365 List applicable = candidateRules.filter( | 366 List applicable = |
| 366 (each) => each.appliesTo(target, w)); | 367 candidateRules.where((each) => each.appliesTo(target, w)).toList(); |
| 367 | 368 |
| 368 if (applicable.isEmpty) { | 369 if (applicable.isEmpty) { |
| 369 return [addRuleFor(target)]; | 370 return [addRuleFor(target)]; |
| 370 } | 371 } |
| 371 | 372 |
| 372 if (applicable.length == 1) return applicable; | 373 if (applicable.length == 1) return applicable; |
| 373 var first = applicable[0]; | 374 var first = applicable[0]; |
| 374 var finalRules = applicable.filter( | 375 var finalRules = applicable.where( |
| 375 (x) => !x.mustBePrimary || (x == first)); | 376 (x) => !x.mustBePrimary || (x == first)).toList(); |
| 376 | 377 |
| 377 if (finalRules.isEmpty) throw new SerializationException( | 378 if (finalRules.isEmpty) throw new SerializationException( |
| 378 'No valid rule found for object $object'); | 379 'No valid rule found for object $object'); |
| 379 return finalRules; | 380 return finalRules; |
| 380 } | 381 } |
| 381 | 382 |
| 382 /** | 383 /** |
| 383 * Create a Serialization for serializing SerializationRules. This is used | 384 * Create a Serialization for serializing SerializationRules. This is used |
| 384 * to save the rules in a self-describing format along with the data. | 385 * to save the rules in a self-describing format along with the data. |
| 385 * If there are new rule classes created, they will need to be described | 386 * If there are new rule classes created, they will need to be described |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 return ifAbsent == null ? null : ifAbsent(); | 430 return ifAbsent == null ? null : ifAbsent(); |
| 430 } | 431 } |
| 431 } | 432 } |
| 432 | 433 |
| 433 /** | 434 /** |
| 434 * An exception class for errors during serialization. | 435 * An exception class for errors during serialization. |
| 435 */ | 436 */ |
| 436 class SerializationException implements Exception { | 437 class SerializationException implements Exception { |
| 437 final String message; | 438 final String message; |
| 438 const SerializationException([this.message]); | 439 const SerializationException([this.message]); |
| 439 } | 440 } |
| OLD | NEW |