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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 * new Serialization() | 176 * new Serialization() |
177 * ..addRuleFor(new Person(), constructorFields: ["name"]) | 177 * ..addRuleFor(new Person(), constructorFields: ["name"]) |
178 * ..namedObjects['Person'] = reflect(new Person()).type; | 178 * ..namedObjects['Person'] = reflect(new Person()).type; |
179 */ | 179 */ |
180 library serialization; | 180 library serialization; |
181 | 181 |
182 import 'src/mirrors_helpers.dart'; | 182 import 'src/mirrors_helpers.dart'; |
183 import 'src/serialization_helpers.dart'; | 183 import 'src/serialization_helpers.dart'; |
184 import 'dart:async'; | 184 import 'dart:async'; |
185 import 'dart:json' as json; | 185 import 'dart:json' as json; |
186 import 'dart:collection' show Queue; | 186 import 'dart:collection'; |
| 187 // TODO(alanknight): Ugh. Remove this if we can resolve bug 7982/7940. |
| 188 import 'dart:collection-dev'; |
187 | 189 |
188 part 'src/reader_writer.dart'; | 190 part 'src/reader_writer.dart'; |
189 part 'src/serialization_rule.dart'; | 191 part 'src/serialization_rule.dart'; |
190 part 'src/basic_rule.dart'; | 192 part 'src/basic_rule.dart'; |
191 part 'src/format.dart'; | 193 part 'src/format.dart'; |
192 | 194 |
193 /** | 195 /** |
194 * This class defines a particular serialization scheme, in terms of | 196 * This class defines a particular serialization scheme, in terms of |
195 * [SerializationRule] instances, and supports reading and writing them. | 197 * [SerializationRule] instances, and supports reading and writing them. |
196 * See library comment for examples of usage. | 198 * See library comment for examples of usage. |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 return rule; | 306 return rule; |
305 } | 307 } |
306 | 308 |
307 /** Set up the default rules, for lists and primitives. */ | 309 /** Set up the default rules, for lists and primitives. */ |
308 void addDefaultRules() { | 310 void addDefaultRules() { |
309 addRule(new PrimitiveRule()); | 311 addRule(new PrimitiveRule()); |
310 addRule(new ListRule()); | 312 addRule(new ListRule()); |
311 // Both these rules apply to lists, so unless otherwise indicated, | 313 // Both these rules apply to lists, so unless otherwise indicated, |
312 // it will always find the first one. | 314 // it will always find the first one. |
313 addRule(new ListRuleEssential()); | 315 addRule(new ListRuleEssential()); |
| 316 addRule(new MapRule()); |
314 } | 317 } |
315 | 318 |
316 /** | 319 /** |
317 * Add a new SerializationRule [rule]. The addRuleFor method will probably | 320 * Add a new SerializationRule [rule]. The addRuleFor method will probably |
318 * handle most simple cases, but for adding an arbitrary rule, including | 321 * handle most simple cases, but for adding an arbitrary rule, including |
319 * a SerializationRule subclass which you have created, you can use this | 322 * a SerializationRule subclass which you have created, you can use this |
320 * method. | 323 * method. |
321 */ | 324 */ |
322 void addRule(SerializationRule rule) { | 325 void addRule(SerializationRule rule) { |
323 rule.number = _rules.length; | 326 rule.number = _rules.length; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 // or we might just be able to validate that they're correctly set up | 422 // or we might just be able to validate that they're correctly set up |
420 // on the other side. | 423 // on the other side. |
421 | 424 |
422 // Make some bogus rule instances so we have something to feed rule creation | 425 // Make some bogus rule instances so we have something to feed rule creation |
423 // and get their types. If only we had class literals implemented... | 426 // and get their types. If only we had class literals implemented... |
424 var basicRule = new BasicRule(reflect(null).type, '', [], [], []); | 427 var basicRule = new BasicRule(reflect(null).type, '', [], [], []); |
425 | 428 |
426 var meta = new Serialization() | 429 var meta = new Serialization() |
427 ..selfDescribing = false | 430 ..selfDescribing = false |
428 ..addRuleFor(new ListRule()) | 431 ..addRuleFor(new ListRule()) |
| 432 ..addRuleFor(new MapRule()) |
429 ..addRuleFor(new PrimitiveRule()) | 433 ..addRuleFor(new PrimitiveRule()) |
430 ..addRuleFor(new ListRuleEssential()) | 434 ..addRuleFor(new ListRuleEssential()) |
431 ..addRuleFor(basicRule, | 435 ..addRuleFor(basicRule, |
432 constructorFields: ['type', | 436 constructorFields: ['type', |
433 'constructorName', | 437 'constructorName', |
434 'constructorFields', 'regularFields', []], | 438 'constructorFields', 'regularFields', []], |
435 fields: []) | 439 fields: []) |
436 ..addRule(new NamedObjectRule()) | 440 ..addRule(new NamedObjectRule()) |
437 ..addRule(new MirrorRule()); | 441 ..addRule(new MirrorRule()); |
438 meta.namedObjects = namedObjects; | 442 meta.namedObjects = namedObjects; |
(...skipping 17 matching lines...) Expand all Loading... |
456 return ifAbsent == null ? null : ifAbsent(); | 460 return ifAbsent == null ? null : ifAbsent(); |
457 } | 461 } |
458 } | 462 } |
459 | 463 |
460 /** | 464 /** |
461 * An exception class for errors during serialization. | 465 * An exception class for errors during serialization. |
462 */ | 466 */ |
463 class SerializationException implements Exception { | 467 class SerializationException implements Exception { |
464 final String message; | 468 final String message; |
465 const SerializationException([this.message]); | 469 const SerializationException([this.message]); |
| 470 toString() => "SerializationException($message)"; |
466 } | 471 } |
OLD | NEW |