Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: pkg/serialization/lib/serialization.dart

Issue 14793016: pkg/serialization types cleanup (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: const is better, right? Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/serialization/lib/src/basic_rule.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 * This class defines a particular serialization scheme, in terms of 220 * This class defines a particular serialization scheme, in terms of
221 * [SerializationRule] instances, and supports reading and writing them. 221 * [SerializationRule] instances, and supports reading and writing them.
222 * See library comment for examples of usage. 222 * See library comment for examples of usage.
223 */ 223 */
224 class Serialization { 224 class Serialization {
225 225
226 /** 226 /**
227 * The serialization is controlled by the list of Serialization rules. These 227 * The serialization is controlled by the list of Serialization rules. These
228 * are most commonly added via [addRuleFor]. 228 * are most commonly added via [addRuleFor].
229 */ 229 */
230 List _rules = []; 230 final List<SerializationRule> rules = new List<SerializationRule>();
231
232 /**
233 * The serialization is controlled by the list of Serialization rules. These
234 * are most commonly added via [addRuleFor].
235 */
236 List get rules => _rules;
237 231
238 /** 232 /**
239 * When reading, we may need to resolve references to existing objects in 233 * When reading, we may need to resolve references to existing objects in
240 * the system. The right action may not be to create a new instance of 234 * the system. The right action may not be to create a new instance of
241 * something, but rather to find an existing instance and connect to it. 235 * something, but rather to find an existing instance and connect to it.
242 * For example, if we have are serializing an Email message and it has a 236 * For example, if we have are serializing an Email message and it has a
243 * link to the owning account, it may not be appropriate to try and serialize 237 * link to the owning account, it may not be appropriate to try and serialize
244 * the account. Instead we should just connect the de-serialized message 238 * the account. Instead we should just connect the de-serialized message
245 * object to the account object that already exists there. 239 * object to the account object that already exists there.
246 */ 240 */
(...skipping 10 matching lines...) Expand all
257 /** 251 /**
258 * When we write out data using this serialization, should we also write 252 * When we write out data using this serialization, should we also write
259 * out a description of the rules. This is on by default unless using 253 * out a description of the rules. This is on by default unless using
260 * CustomRule subclasses, in which case it requires additional setup and 254 * CustomRule subclasses, in which case it requires additional setup and
261 * is off by default. 255 * is off by default.
262 */ 256 */
263 bool get selfDescribing { 257 bool get selfDescribing {
264 // TODO(alanknight): Should this be moved to the format? 258 // TODO(alanknight): Should this be moved to the format?
265 // TODO(alanknight): Allow self-describing in the presence of CustomRule. 259 // TODO(alanknight): Allow self-describing in the presence of CustomRule.
266 if (_selfDescribing != null) return _selfDescribing; 260 if (_selfDescribing != null) return _selfDescribing;
267 return !_rules.any((x) => x is CustomRule); 261 return !rules.any((x) => x is CustomRule);
268 } 262 }
269 263
270 /** 264 /**
271 * When we write out data using this serialization, should we also write 265 * When we write out data using this serialization, should we also write
272 * out a description of the rules. This is on by default unless using 266 * out a description of the rules. This is on by default unless using
273 * CustomRule subclasses, in which case it requires additional setup and 267 * CustomRule subclasses, in which case it requires additional setup and
274 * is off by default. 268 * is off by default.
275 */ 269 */
276 set selfDescribing(x) => _selfDescribing = x; 270 void set selfDescribing(bool value) => _selfDescribing = value;
277 271
278 /** 272 /**
279 * Creates a new serialization with a default set of rules for primitives 273 * Creates a new serialization with a default set of rules for primitives
280 * and lists. 274 * and lists.
281 */ 275 */
282 Serialization() { 276 Serialization() {
283 addDefaultRules(); 277 addDefaultRules();
284 } 278 }
285 279
286 /** 280 /**
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 addRule(new SymbolRule()); 337 addRule(new SymbolRule());
344 } 338 }
345 339
346 /** 340 /**
347 * Add a new SerializationRule [rule]. The addRuleFor method will probably 341 * Add a new SerializationRule [rule]. The addRuleFor method will probably
348 * handle most simple cases, but for adding an arbitrary rule, including 342 * handle most simple cases, but for adding an arbitrary rule, including
349 * a SerializationRule subclass which you have created, you can use this 343 * a SerializationRule subclass which you have created, you can use this
350 * method. 344 * method.
351 */ 345 */
352 void addRule(SerializationRule rule) { 346 void addRule(SerializationRule rule) {
353 rule.number = _rules.length; 347 rule.number = rules.length;
354 _rules.add(rule); 348 rules.add(rule);
355 } 349 }
356 350
357 /** 351 /**
358 * This writes out an object graph rooted at [object] and returns the result. 352 * This writes out an object graph rooted at [object] and returns the result.
359 * The [format] parameter determines the form of the result. The default 353 * The [format] parameter determines the form of the result. The default
360 * format returns a String in [json] format. 354 * format returns a String in [json] format.
361 */ 355 */
362 write(Object object, [Format format]) { 356 write(Object object, [Format format]) {
363 return newWriter(format).write(object); 357 return newWriter(format).write(object);
364 } 358 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 // light of a more general mechanism for multiple rules per object. 403 // light of a more general mechanism for multiple rules per object.
410 // TODO(alanknight): Finding which rules apply seems likely to be a 404 // TODO(alanknight): Finding which rules apply seems likely to be a
411 // bottleneck, particularly with the current reflective implementation. 405 // bottleneck, particularly with the current reflective implementation.
412 // Consider how to improve it. e.g. cache the list of rules by class. But 406 // Consider how to improve it. e.g. cache the list of rules by class. But
413 // be careful of issues like rules which have arbitrary predicates. Or 407 // be careful of issues like rules which have arbitrary predicates. Or
414 // consider having the arbitrary predicates be secondary to an initial 408 // consider having the arbitrary predicates be secondary to an initial
415 // class-based lookup mechanism. 409 // class-based lookup mechanism.
416 var target, candidateRules; 410 var target, candidateRules;
417 if (object is DesignatedRuleForObject) { 411 if (object is DesignatedRuleForObject) {
418 target = object.target; 412 target = object.target;
419 candidateRules = object.possibleRules(_rules); 413 candidateRules = object.possibleRules(rules);
420 } else { 414 } else {
421 target = object; 415 target = object;
422 candidateRules = _rules; 416 candidateRules = rules;
423 } 417 }
424 Iterable applicable = candidateRules.where( 418 Iterable applicable = candidateRules.where(
425 (each) => each.appliesTo(target, w)); 419 (each) => each.appliesTo(target, w));
426 420
427 if (applicable.isEmpty) { 421 if (applicable.isEmpty) {
428 return [addRuleFor(target)]; 422 return [addRuleFor(target)];
429 } 423 }
430 424
431 if (applicable.length == 1) return applicable; 425 if (applicable.length == 1) return applicable;
432 var first = applicable.first; 426 var first = applicable.first;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 } 486 }
493 487
494 /** 488 /**
495 * An exception class for errors during serialization. 489 * An exception class for errors during serialization.
496 */ 490 */
497 class SerializationException implements Exception { 491 class SerializationException implements Exception {
498 final String message; 492 final String message;
499 const SerializationException([this.message]); 493 const SerializationException([this.message]);
500 toString() => "SerializationException($message)"; 494 toString() => "SerializationException($message)";
501 } 495 }
OLDNEW
« no previous file with comments | « no previous file | pkg/serialization/lib/src/basic_rule.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698