| 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 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 * will populate the rules from that data. | 290 * will populate the rules from that data. |
| 291 */ | 291 */ |
| 292 factory Serialization.blank() | 292 factory Serialization.blank() |
| 293 => new Serialization._(new List<SerializationRule>()); | 293 => new Serialization._(new List<SerializationRule>()); |
| 294 | 294 |
| 295 Serialization._(List<SerializationRule> rules) : | 295 Serialization._(List<SerializationRule> rules) : |
| 296 this._rules = rules, | 296 this._rules = rules, |
| 297 this.rules = new UnmodifiableListView(rules); | 297 this.rules = new UnmodifiableListView(rules); |
| 298 | 298 |
| 299 /** | 299 /** |
| 300 * Create a [BasicRule] rule for the type of | 300 * Create a [BasicRule] rule for [instanceOrType]. Normally this will be |
| 301 * [instanceOfType]. Optionally | 301 * a type, but for backward compatibilty we also allow you to pass an |
| 302 * instance (except an instance of Type), and the rule will be created |
| 303 * for its runtimeType. Optionally |
| 302 * allows specifying a [constructor] name, the list of [constructorFields], | 304 * allows specifying a [constructor] name, the list of [constructorFields], |
| 303 * and the list of [fields] not used in the constructor. Returns the new | 305 * and the list of [fields] not used in the constructor. Returns the new |
| 304 * rule. Note that [BasicRule] uses reflection, and so will not work with the | 306 * rule. Note that [BasicRule] uses reflection, and so will not work with the |
| 305 * current state of dartj2s. If you need to run there, consider using | 307 * current state of dartj2s. If you need to run there, consider using |
| 306 * [CustomRule] instead. | 308 * [CustomRule] instead. |
| 307 * | 309 * |
| 308 * If the optional parameters aren't specified, the default constructor will | 310 * If the optional parameters aren't specified, the default constructor will |
| 309 * be used, and the list of fields will be computed. Alternatively, you can | 311 * be used, and the list of fields will be computed. Alternatively, you can |
| 310 * omit [fields] and provide [excludeFields], which will then compute the | 312 * omit [fields] and provide [excludeFields], which will then compute the |
| 311 * list of fields specifically excluding those listed. | 313 * list of fields specifically excluding those listed. |
| 312 * | 314 * |
| 313 * The fields can be actual public fields, but can also be getter/setter | 315 * The fields can be actual public fields, but can also be getter/setter |
| 314 * pairs or getters whose value is provided in the constructor. For the | 316 * pairs or getters whose value is provided in the constructor. For the |
| 315 * [constructorFields] they can also be arbitrary objects. Anything that is | 317 * [constructorFields] they can also be arbitrary objects. Anything that is |
| 316 * not a String will be treated as a constant value to be used in any | 318 * not a String will be treated as a constant value to be used in any |
| 317 * construction of these objects. | 319 * construction of these objects. |
| 318 * | 320 * |
| 319 * If the list of fields is computed, fields from the superclass will be | 321 * If the list of fields is computed, fields from the superclass will be |
| 320 * included. However, each subclass needs its own rule, since the constructors | 322 * included. However, each subclass needs its own rule, since the constructors |
| 321 * are not inherited, and so may need to be specified separately for each | 323 * are not inherited, and so may need to be specified separately for each |
| 322 * subclass. | 324 * subclass. |
| 323 */ | 325 */ |
| 324 // TODO(alanknight): Take a type rather than an instance. Issue 6282 and 6433. | |
| 325 BasicRule addRuleFor( | 326 BasicRule addRuleFor( |
| 326 instanceOfType, | 327 instanceOrType, |
| 327 {String constructor, | 328 {String constructor, |
| 328 List constructorFields, | 329 List constructorFields, |
| 329 List<String> fields, | 330 List<String> fields, |
| 330 List<String> excludeFields}) { | 331 List<String> excludeFields}) { |
| 331 | 332 |
| 332 var rule = new BasicRule( | 333 var rule = new BasicRule( |
| 333 turnInstanceIntoSomethingWeCanUse( | 334 turnInstanceIntoSomethingWeCanUse( |
| 334 instanceOfType), | 335 instanceOrType), |
| 335 constructor, constructorFields, fields, excludeFields); | 336 constructor, constructorFields, fields, excludeFields); |
| 336 addRule(rule); | 337 addRule(rule); |
| 337 return rule; | 338 return rule; |
| 338 } | 339 } |
| 339 | 340 |
| 340 /** Set up the default rules, for lists and primitives. */ | 341 /** Set up the default rules, for lists and primitives. */ |
| 341 void addDefaultRules() { | 342 void addDefaultRules() { |
| 342 addRule(new PrimitiveRule()); | 343 addRule(new PrimitiveRule()); |
| 343 addRule(new ListRule()); | 344 addRule(new ListRule()); |
| 344 // Both these rules apply to lists, so unless otherwise indicated, | 345 // Both these rules apply to lists, so unless otherwise indicated, |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 } | 502 } |
| 502 | 503 |
| 503 /** | 504 /** |
| 504 * An exception class for errors during serialization. | 505 * An exception class for errors during serialization. |
| 505 */ | 506 */ |
| 506 class SerializationException implements Exception { | 507 class SerializationException implements Exception { |
| 507 final String message; | 508 final String message; |
| 508 const SerializationException(this.message); | 509 const SerializationException(this.message); |
| 509 String toString() => "SerializationException($message)"; | 510 String toString() => "SerializationException($message)"; |
| 510 } | 511 } |
| OLD | NEW |