| 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 27 matching lines...) Expand all Loading... |
| 38 * fields that we specified. We may also want to tell it to identify the | 38 * fields that we specified. We may also want to tell it to identify the |
| 39 * fields, but to specifically omit certain fields that we don't want | 39 * fields, but to specifically omit certain fields that we don't want |
| 40 * serialized. | 40 * serialized. |
| 41 * | 41 * |
| 42 * var serialization = new Serialization() | 42 * var serialization = new Serialization() |
| 43 * ..addRuleFor(address, | 43 * ..addRuleFor(address, |
| 44 * constructor: "", | 44 * constructor: "", |
| 45 * excludeFields: ["other", "stuff"]); | 45 * excludeFields: ["other", "stuff"]); |
| 46 * | 46 * |
| 47 * We can also use a completely non-reflective rule to serialize and | 47 * We can also use a completely non-reflective rule to serialize and |
| 48 * de-serialize objects. | 48 * de-serialize objects. This can be more cumbersome, but it does work in |
| 49 * dart2js, where mirrors are not yet implemented. |
| 49 * | 50 * |
| 50 * addressToMap(a) => {"number" : a.number, "street" : a.street, | 51 * addressToMap(a) => {"number" : a.number, "street" : a.street, |
| 51 * "city" : a.city}; | 52 * "city" : a.city}; |
| 52 * createAddress(Map m) => new Address.create(m["number"], m["street"]); | 53 * createAddress(Map m) => new Address.create(m["number"], m["street"]); |
| 53 * fillInAddress(Map m, Address a) => a.city = m["city"]; | 54 * fillInAddress(Map m, Address a) => a.city = m["city"]; |
| 54 * var serialization = new Serialization() | 55 * var serialization = new Serialization() |
| 55 * ..addRule( | 56 * ..addRule( |
| 56 * new ClosureToMapRule(anAddress.runtimeType, | 57 * new ClosureToMapRule(anAddress.runtimeType, |
| 57 * addressToMap, createAddress, fillInAddress); | 58 * addressToMap, createAddress, fillInAddress); |
| 58 * | 59 * |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 * use for this is if we are reading self-describing serialized data and | 225 * use for this is if we are reading self-describing serialized data and |
| 225 * will populate the rules from that data. | 226 * will populate the rules from that data. |
| 226 */ | 227 */ |
| 227 Serialization.blank() { } | 228 Serialization.blank() { } |
| 228 | 229 |
| 229 /** | 230 /** |
| 230 * Create a [BasicRule] rule for the type of | 231 * Create a [BasicRule] rule for the type of |
| 231 * [instanceOfType]. Optionally | 232 * [instanceOfType]. Optionally |
| 232 * allows specifying a [constructor] name, the list of [constructorFields], | 233 * allows specifying a [constructor] name, the list of [constructorFields], |
| 233 * and the list of [fields] not used in the constructor. Returns the new | 234 * and the list of [fields] not used in the constructor. Returns the new |
| 234 * rule. | 235 * rule. Note that [BasicRule] uses reflection, and so will not work with the |
| 236 * current state of dartj2s. If you need to run there, consider using |
| 237 * [CustomRule] instead. |
| 235 * | 238 * |
| 236 * If the optional parameters aren't specified, the default constructor will | 239 * If the optional parameters aren't specified, the default constructor will |
| 237 * be used, and the list of fields will be computed. Alternatively, you can | 240 * be used, and the list of fields will be computed. Alternatively, you can |
| 238 * omit [fields] and provide [excludeFields], which will then compute the | 241 * omit [fields] and provide [excludeFields], which will then compute the |
| 239 * list of fields specifically excluding those listed. | 242 * list of fields specifically excluding those listed. |
| 240 * | 243 * |
| 241 * The fields can be actual public fields, but can also be getter/setter | 244 * The fields can be actual public fields, but can also be getter/setter |
| 242 * pairs or getters whose value is provided in the constructor. For the | 245 * pairs or getters whose value is provided in the constructor. For the |
| 243 * [constructorFields] they can also be arbitrary objects. Anything that is | 246 * [constructorFields] they can also be arbitrary objects. Anything that is |
| 244 * not a String will be treated as a constant value to be used in any | 247 * not a String will be treated as a constant value to be used in any |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 } | 430 } |
| 428 } | 431 } |
| 429 | 432 |
| 430 /** | 433 /** |
| 431 * An exception class for errors during serialization. | 434 * An exception class for errors during serialization. |
| 432 */ | 435 */ |
| 433 class SerializationException implements Exception { | 436 class SerializationException implements Exception { |
| 434 final String message; | 437 final String message; |
| 435 const SerializationException([this.message]); | 438 const SerializationException([this.message]); |
| 436 } | 439 } |
| OLD | NEW |