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

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

Issue 22375012: pkg/serialization updates (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: a bit more Created 7 years, 4 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 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 part 'src/serialization_rule.dart'; 213 part 'src/serialization_rule.dart';
214 part 'src/basic_rule.dart'; 214 part 'src/basic_rule.dart';
215 part 'src/format.dart'; 215 part 'src/format.dart';
216 216
217 /** 217 /**
218 * This class defines a particular serialization scheme, in terms of 218 * This class defines a particular serialization scheme, in terms of
219 * [SerializationRule] instances, and supports reading and writing them. 219 * [SerializationRule] instances, and supports reading and writing them.
220 * See library comment for examples of usage. 220 * See library comment for examples of usage.
221 */ 221 */
222 class Serialization { 222 class Serialization {
223 final List<SerializationRule> _rules;
223 224
224 /** 225 /**
225 * The serialization is controlled by the list of Serialization rules. These 226 * The serialization is controlled by the list of Serialization rules. These
226 * are most commonly added via [addRuleFor]. 227 * are most commonly added via [addRuleFor].
227 */ 228 */
228 final List<SerializationRule> rules = new List<SerializationRule>(); 229 final UnmodifiableListView<SerializationRule> rules;
229 230
230 /** 231 /**
231 * When reading, we may need to resolve references to existing objects in 232 * When reading, we may need to resolve references to existing objects in
232 * the system. The right action may not be to create a new instance of 233 * the system. The right action may not be to create a new instance of
233 * something, but rather to find an existing instance and connect to it. 234 * something, but rather to find an existing instance and connect to it.
234 * For example, if we have are serializing an Email message and it has a 235 * For example, if we have are serializing an Email message and it has a
235 * link to the owning account, it may not be appropriate to try and serialize 236 * link to the owning account, it may not be appropriate to try and serialize
236 * the account. Instead we should just connect the de-serialized message 237 * the account. Instead we should just connect the de-serialized message
237 * object to the account object that already exists there. 238 * object to the account object that already exists there.
238 */ 239 */
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 * is off by default. 273 * is off by default.
273 */ 274 */
274 void set selfDescribing(bool value) { 275 void set selfDescribing(bool value) {
275 _selfDescribing = value; 276 _selfDescribing = value;
276 } 277 }
277 278
278 /** 279 /**
279 * Creates a new serialization with a default set of rules for primitives 280 * Creates a new serialization with a default set of rules for primitives
280 * and lists. 281 * and lists.
281 */ 282 */
282 Serialization() { 283 factory Serialization() =>
283 addDefaultRules(); 284 new Serialization.blank()
284 } 285 ..addDefaultRules();
285 286
286 /** 287 /**
287 * Creates a new serialization with no default rules at all. The most common 288 * Creates a new serialization with no default rules at all. The most common
288 * use for this is if we are reading self-describing serialized data and 289 * use for this is if we are reading self-describing serialized data and
289 * will populate the rules from that data. 290 * will populate the rules from that data.
290 */ 291 */
291 Serialization.blank() { } 292 factory Serialization.blank()
293 => new Serialization._(new List<SerializationRule>());
294
295 Serialization._(List<SerializationRule> rules) :
296 this._rules = rules,
297 this.rules = new UnmodifiableListView(rules);
292 298
293 /** 299 /**
294 * Create a [BasicRule] rule for the type of 300 * Create a [BasicRule] rule for the type of
295 * [instanceOfType]. Optionally 301 * [instanceOfType]. Optionally
296 * allows specifying a [constructor] name, the list of [constructorFields], 302 * allows specifying a [constructor] name, the list of [constructorFields],
297 * and the list of [fields] not used in the constructor. Returns the new 303 * and the list of [fields] not used in the constructor. Returns the new
298 * rule. Note that [BasicRule] uses reflection, and so will not work with the 304 * rule. Note that [BasicRule] uses reflection, and so will not work with the
299 * current state of dartj2s. If you need to run there, consider using 305 * current state of dartj2s. If you need to run there, consider using
300 * [CustomRule] instead. 306 * [CustomRule] instead.
301 * 307 *
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 addRule(new DateTimeRule()); 349 addRule(new DateTimeRule());
344 } 350 }
345 351
346 /** 352 /**
347 * Add a new SerializationRule [rule]. The addRuleFor method will probably 353 * Add a new SerializationRule [rule]. The addRuleFor method will probably
348 * handle most simple cases, but for adding an arbitrary rule, including 354 * handle most simple cases, but for adding an arbitrary rule, including
349 * a SerializationRule subclass which you have created, you can use this 355 * a SerializationRule subclass which you have created, you can use this
350 * method. 356 * method.
351 */ 357 */
352 void addRule(SerializationRule rule) { 358 void addRule(SerializationRule rule) {
353 rule.number = rules.length; 359 rule.number = _rules.length;
354 rules.add(rule); 360 _rules.add(rule);
355 } 361 }
356 362
357 /** 363 /**
358 * This writes out an object graph rooted at [object] and returns the result. 364 * 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 365 * The [format] parameter determines the form of the result. The default
360 * format returns a String in [json] format. 366 * format returns a String in [json] format.
361 */ 367 */
362 write(Object object, {Format format}) { 368 write(Object object, {Format format}) {
363 return newWriter(format).write(object); 369 return newWriter(format).write(object);
364 } 370 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 } 501 }
496 502
497 /** 503 /**
498 * An exception class for errors during serialization. 504 * An exception class for errors during serialization.
499 */ 505 */
500 class SerializationException implements Exception { 506 class SerializationException implements Exception {
501 final String message; 507 final String message;
502 const SerializationException(this.message); 508 const SerializationException(this.message);
503 String toString() => "SerializationException($message)"; 509 String toString() => "SerializationException($message)";
504 } 510 }
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