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

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

Issue 11820032: Make input/output formats pluggable, adapt to new libraries (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changes from review comments Created 7 years, 11 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 * Setup 10 * Setup
11 * ===== 11 * =====
12 * A simple example of usage is 12 * A simple example of usage is
13 * 13 *
14 * var address = new Address(); 14 * var address = new Address();
15 * address.street = 'N 34th'; 15 * address.street = 'N 34th';
16 * address.city = 'Seattle'; 16 * address.city = 'Seattle';
17 * var serialization = new Serialization() 17 * var serialization = new Serialization()
18 * ..addRuleFor(address); 18 * ..addRuleFor(address);
19 * String output = serialization.write(address); 19 * String output = serialization.write(address);
20 * 20 *
21 * This creates a new serialization and adds a rule for address objects. Right 21 * This creates a new serialization and adds a rule for address objects. Right
22 * now it has to be passed an address instance because we can't write Address 22 * now it has to be passed an address instance because of limitations using
23 * as a literal. Then we ask the Serialization to write the address and we get 23 * Address as a literal. Then we ask the Serialization to write the address
24 * back a String which is a [JSON] representation of the state of it and related 24 * and we get back a String which is a [JSON] representation of the state of
25 * objects. 25 * it and related objects.
26 * 26 *
27 * The version above used reflection to automatically identify the public 27 * The version above used reflection to automatically identify the public
28 * fields of the address object. We can also specify those fields explicitly. 28 * fields of the address object. We can also specify those fields explicitly.
29 * 29 *
30 * var serialization = new Serialization() 30 * var serialization = new Serialization()
31 * ..addRuleFor(address, 31 * ..addRuleFor(address,
32 * constructor: "create", 32 * constructor: "create",
33 * constructorFields: ["number", "street"], 33 * constructorFields: ["number", "street"],
34 * fields: ["city"]); 34 * fields: ["city"]);
35 * 35 *
36 * This rule still uses reflection to access the fields, but does not try to 36 * This rule still uses reflection to access the fields, but does not try to
37 * identify which fields to use, but instead uses only the "number" and "street" 37 * identify which fields to use, but instead uses only the "number" and "street"
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 * Writing Rules
48 * =============
47 * We can also use a completely non-reflective rule to serialize and 49 * We can also use a completely non-reflective rule to serialize and
48 * de-serialize objects. This can be more cumbersome, but it does work in 50 * de-serialize objects. This can be more work, but it does work in
49 * dart2js, where mirrors are not yet implemented. 51 * dart2js, where mirrors are not yet implemented. We can specify this in two
52 * ways. First, we can write our own SerializationRule class that has methods
53 * for our Address class.
54 *
55 * class AddressRule extends CustomRule {
56 * bool appliesTo(instance, Writer w) => instance.runtimeType == Address;
57 * getState(instance) => [instance.street, instance.city];
58 * create(state) => new Address();
59 * setState(Address a, List state) {
60 * a.street = state[0];
61 * a.city = state[1];
62 * }
63 * }
64 *
65 * The class needs four different methods. The [appliesTo] method tells us if
66 * the rule should be used to write an object. In this example we use a test
67 * based on runtimeType. We could also use an "is Address" test, but if Address
68 * has subclasses that would find those as well, and we want a separate rule
69 * for each. The [getState] method should
70 * return all the state of the object that we want to recreate,
71 * and should be either a Map or a List. If you want to write to human-readable
72 * formats where it's useful to be able to look at the data as a map from
73 * field names to values, then it's better to return it as a map. Otherwise it's
74 * more efficient to return it as a list. You just need to be sure that the
75 * [create] and [setState] methods interpret the same way as [getState] does.
76 *
77 * The [create] method will create the new object and return it. While it's
78 * possible to create the object and set all its state in this one method, that
79 * increases the likelihood of problems with cycles. So it's better to use the
80 * minimum necessary information in [create] and do more of the work in
81 * [setState].
82 *
83 * The other way to do this is not creating a subclass, but by using a
84 * [ClosureRule] and giving it functions for how to create
85 * the address.
50 * 86 *
51 * addressToMap(a) => {"number" : a.number, "street" : a.street, 87 * addressToMap(a) => {"number" : a.number, "street" : a.street,
52 * "city" : a.city}; 88 * "city" : a.city};
53 * createAddress(Map m) => new Address.create(m["number"], m["street"]); 89 * createAddress(Map m) => new Address.create(m["number"], m["street"]);
54 * fillInAddress(Map m, Address a) => a.city = m["city"]; 90 * fillInAddress(Map m, Address a) => a.city = m["city"];
55 * var serialization = new Serialization() 91 * var serialization = new Serialization()
56 * ..addRule( 92 * ..addRule(
57 * new ClosureToMapRule(anAddress.runtimeType, 93 * new ClosureRule(anAddress.runtimeType,
58 * addressToMap, createAddress, fillInAddress); 94 * addressToMap, createAddress, fillInAddress);
59 * 95 *
60 * Note that there are three different functions provided. The addressToMap 96 * In this case we have created standalone functions rather than
61 * function takes the fields we want serialized from the Address and puts them 97 * methods in a subclass and we pass them to the constructor of
62 * into a map. The createAddress function creates a new address using a map like 98 * [ClosureRule]. In this case we've also had them use maps rather than
63 * the one returned by the first function. And the fillInAddress function fills 99 * lists for the state, but either would work as long as the rule is
64 * in any remaining state in the created object. 100 * consistent with the representation it uses. We pass it the runtimeType
101 * of the object, and functions equivalent to the methods on [CustomRule]
65 * 102 *
66 * At the moment, however, using this rule increases the probability of problems 103 * Constant Values
67 * with cycles. The problem is that before passing values to the user-supplied 104 * ===============
68 * functions it has to inflate any references to be the real objects. Since it
69 * doesn't know which ones the creation function uses it has to inflate all of
70 * them. For example, consider a Node class with parent, leftChild, and
71 * rightChild, and the parent field was final and set in the constructor. When
72 * we inflate all of the values we will end up with a cycle and can't
73 * de-serialize. If we know which fields are used by the constructor we can
74 * inflate only those, which is what BasicRule does. We expect to make a richer
75 * API for rules not using reflection, but there's a tension between providing
76 * the serialization process with enough information and making it more work
77 * to specify.
78 *
79 * There are cases where the constructor needs values that we can't easily get 105 * There are cases where the constructor needs values that we can't easily get
80 * the serialized object. For example, we may just want to pass null, or a 106 * from the serialized object. For example, we may just want to pass null, or a
81 * constant value. To support this, we can specify as constructor fields 107 * constant value. To support this, we can specify as constructor fields
82 * values that aren't field names. If any value isn't a String, it will be 108 * values that aren't field names. If any value isn't a String, it will be
83 * treated as a constant and passed unaltered to the constructor. 109 * treated as a constant and passed unaltered to the constructor.
84 * 110 *
85 * In some cases a non-constructor field should not be set using field 111 * In some cases a non-constructor field should not be set using field
86 * access or a setter, but should be done by calling a method. For example, it 112 * access or a setter, but should be done by calling a method. For example, it
87 * may not be possible to set a List field "foo", and you need to call an 113 * may not be possible to set a List field "foo", and you need to call an
88 * addFoo() method for each entry in the list. In these cases, if you are using 114 * addFoo() method for each entry in the list. In these cases, if you are using
89 * a BasicRule for the object you can call the setFieldWith() method. 115 * a BasicRule for the object you can call the setFieldWith() method.
90 * 116 *
91 * s..addRuleFor(fooHolderInstance).setFieldWith("foo", 117 * s..addRuleFor(fooHolderInstance).setFieldWith("foo",
92 * (parent, value) => for (var each in value) parent.addFoo(value)); 118 * (parent, value) => for (var each in value) parent.addFoo(value));
93 * 119 *
94 * Writing 120 * Writing
95 * ======= 121 * =======
96 * To write objects, we use the write() methods. There are two variations. 122 * To write objects, we use the write() method.
97 * 123 *
98 * String output = serialization.write(someObject); 124 * var output = serialization.write(someObject);
99 * List output = serialization.writeFlat(someObject);
100 * 125 *
101 * The first uses a representation in which objects are represented as maps 126 * By default this uses a representation in which objects are represented as
102 * keyed by field name, but in which references between objects have been 127 * maps keyed by field name, but in which references between objects have been
103 * converted into Reference objects. This is then encoded as a [JSON] string. 128 * converted into Reference objects. This is then encoded as a [json] string.
104 * 129 *
105 * The second representation holds all the objects as a List of simple types. 130 * We can write objects in different formats by passing a [Format] object to
106 * For practical use you may want to convert that to a [JSON] or other encoded 131 * the [write] method or by getting a [Writer] object. The available formats
107 * representation as well. 132 * include the default, a simple "flat" format that doesn't include field names,
133 * and a simple JSON format that produces output more suitable for talking to
134 * services that expect JSON in a predefined format. Examples of these are
108 * 135 *
109 * Both representations are primarily intended as proofs of concept for 136 * String output = serialization.write(address, new SimpleMapFormat());
110 * different types of representation, and we expect to generalize that to a 137 * List output = serialization.write(address, new SimpleFlatFormat());
111 * pluggable mechanism for different representations. 138 * Map output = serialization.write(address, new SimpleJsonFormat());
139 * Or, using a [Writer] explicitly
140 * var writer = serialization.newWriter(new SimpleFlatFormat());
141 * var output = writer.write(address);
142 *
143 * These representations are not yet considered stable.
112 * 144 *
113 * Reading 145 * Reading
114 * ======= 146 * =======
115 * To read objects, the corresponding methods are [read] and [readFlat]. 147 * To read objects, the corresponding [read] method can be used.
116 * 148 *
117 * List input = serialization.read(aString); 149 * Address input = serialization.read(aString);
118 * List input = serialization.readFlat(aList);
119 *
120 * There is also a convenience method for the case of reading a single object.
121 *
122 * Object result = serialization.readOne(aString);
123 * Object result = serialization.readOneFlat(aString);
124 * 150 *
125 * When reading, the serialization instance doing the reading must be configured 151 * When reading, the serialization instance doing the reading must be configured
126 * with compatible rules to the one doing the writing. It's possible for the 152 * with compatible rules to the one doing the writing. It's possible for the
127 * rules to be different, but they need to be able to read the same 153 * rules to be different, but they need to be able to read the same
128 * representation. For most practical purposes right now they should be the 154 * representation. For most practical purposes right now they should be the
129 * same. The simplest way to achieve this is by having the serialization 155 * same. The simplest way to achieve this is by having the serialization
130 * variable [selfDescribing] be true. In that case the rules themselves are also 156 * variable [selfDescribing] be true. In that case the rules themselves are also
131 * stored along with the serialized data, and can be read back on the receiving 157 * stored along with the serialized data, and can be read back on the receiving
132 * end. Note that this does not yet work for [ClosureToMapRule]. The 158 * end. Note that this may not work for all rules or all formats. The
133 * [selfDescribing] variable is true by default. 159 * [selfDescribing] variable is true by default, but the [SimpleJsonFormat] does
160 * not support it, since the point is to provide a representation in a form
161 * other services might expect. Using CustomRule or ClosureRule also does not
162 * yet work with the [selfDescribing] variable.
134 * 163 *
164 * Named Objects
165 * =============
135 * When reading, some object references should not be serialized, but should be 166 * When reading, some object references should not be serialized, but should be
136 * connected up to other instances on the receiving side. A notable example of 167 * connected up to other instances on the receiving side. A notable example of
137 * this is when serialization rules have been stored. Instances of BasicRule 168 * this is when serialization rules have been stored. Instances of BasicRule
138 * take a [ClassMirror] in their constructor, and we cannot serialize those. So 169 * take a [ClassMirror] in their constructor, and we cannot serialize those. So
139 * when we read the rules, we must provide a Map<String, Object> which maps from 170 * when we read the rules, we must provide a Map<String, Object> which maps from
140 * the simple name of classes we are interested in to a [ClassMirror]. This can 171 * the simple name of classes we are interested in to a [ClassMirror]. This can
141 * be provided either in the [externalObjects] variable of the Serialization, 172 * be provided either in the [namedObjects] variable of the Serialization,
142 * or as an additional parameter to the reading methods. 173 * or as an additional parameter to the reading and writing methods on the
174 * [Reader] or [Writer] respectively.
143 * 175 *
144 * new Serialization() 176 * new Serialization()
145 * ..addRuleFor(new Person(), constructorFields: ["name"]) 177 * ..addRuleFor(new Person(), constructorFields: ["name"])
146 * ..externalObjects['Person'] = reflect(new Person()).type; 178 * ..namedObjects['Person'] = reflect(new Person()).type;
147 */ 179 */
148 library serialization; 180 library serialization;
149 181
150 import 'src/mirrors_helpers.dart'; 182 import 'src/mirrors_helpers.dart';
151 import 'src/serialization_helpers.dart'; 183 import 'src/serialization_helpers.dart';
152 import 'dart:async'; 184 import 'dart:async';
153 import 'dart:json' as json; 185 import 'dart:json' as json;
154 186
155 part 'src/reader_writer.dart'; 187 part 'src/reader_writer.dart';
156 part 'src/serialization_rule.dart'; 188 part 'src/serialization_rule.dart';
157 part 'src/basic_rule.dart'; 189 part 'src/basic_rule.dart';
190 part 'src/format.dart';
158 191
159 /** 192 /**
160 * This class defines a particular serialization scheme, in terms of 193 * This class defines a particular serialization scheme, in terms of
161 * [SerializationRule] instances, and supports reading and writing them. 194 * [SerializationRule] instances, and supports reading and writing them.
162 * See library comment for examples of usage. 195 * See library comment for examples of usage.
163 */ 196 */
164 class Serialization { 197 class Serialization {
165 198
166 /** 199 /**
167 * The serialization is controlled by the list of Serialization rules. These 200 * The serialization is controlled by the list of Serialization rules. These
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 * handle most simple cases, but for adding an arbitrary rule, including 317 * handle most simple cases, but for adding an arbitrary rule, including
285 * a SerializationRule subclass which you have created, you can use this 318 * a SerializationRule subclass which you have created, you can use this
286 * method. 319 * method.
287 */ 320 */
288 void addRule(SerializationRule rule) { 321 void addRule(SerializationRule rule) {
289 rule.number = _rules.length; 322 rule.number = _rules.length;
290 _rules.add(rule); 323 _rules.add(rule);
291 } 324 }
292 325
293 /** 326 /**
294 * This is the basic method to write out an object graph rooted at 327 * This writes out an object graph rooted at [object] and returns the result.
295 * [object] and return the result. Right now this is hard-coded to return 328 * The [format] parameter determines the form of the result. The default
296 * a String from a custom [JSON] format, but that is likely to change to be 329 * format returns a String in [json] format.
297 * more pluggable in the near future.
298 */ 330 */
299 String write(Object object) { 331 write(Object object, [Format format]) {
300 return newWriter().write(object); 332 return newWriter(format).write(object);
301 } 333 }
302 334
303 /** 335 /**
304 * Return a new [Writer] object for this serialization. This is useful if you 336 * Return a new [Writer] object for this serialization. This is useful if you
305 * want to do something more complex with the writer than just returning 337 * want to do something more complex with the writer than just returning
306 * the final result. 338 * the final result.
307 */ 339 */
308 Writer newWriter() => new Writer(this); 340 Writer newWriter([Format format]) =>
309 341 new Writer(this, format);
310 /**
311 * Write out the tree in a custom flat format, returning a list containing
312 * only "simple" types: num, String, and bool.
313 */
314 List writeFlat(Object object) {
315 return newWriter().writeFlat(object);
316 }
317 342
318 /** 343 /**
319 * Read the serialized data from [input] and return the root object 344 * Read the serialized data from [input] and return the root object
320 * from the result. If there are objects that need to be resolved 345 * from the result. If there are objects that need to be resolved
321 * in the current context, they should be provided in [externals] as a 346 * in the current context, they should be provided in [externals] as a
322 * Map from names to values. In particular, in the current implementation 347 * Map from names to values. In particular, in the current implementation
323 * any class mirrors needed should be provided in [externals] using the 348 * any class mirrors needed should be provided in [externals] using the
324 * class name as a key. In addition to the [externals] map provided here, 349 * class name as a key. In addition to the [externals] map provided here,
325 * values will be looked up in the [externalObjects] map. 350 * values will be looked up in the [externalObjects] map.
326 */ 351 */
327 read(String input, [Map externals = const {}]) { 352 read(String input, [Map externals = const {}]) {
328 return newReader().read(input, externals); 353 return newReader().read(input, externals);
329 } 354 }
330 355
331 /** 356 /**
332 * Return a new [Reader] object for this serialization. This is useful if 357 * Return a new [Reader] object for this serialization. This is useful if
333 * you want to do something more complex with the reader than just returning 358 * you want to do something more complex with the reader than just returning
334 * the final result. 359 * the final result.
335 */ 360 */
336 Reader newReader() => new Reader(this); 361 Reader newReader([Format format]) => new Reader(this, format);
337 362
338 /** 363 /**
339 * Return the list of SerializationRule that apply to [object]. For 364 * Return the list of SerializationRule that apply to [object]. For
340 * internal use, but public because it's used in testing. 365 * internal use, but public because it's used in testing.
341 */ 366 */
342 List<SerializationRule> rulesFor(object, Writer w) { 367 Iterable<SerializationRule> rulesFor(object, Writer w) {
343 // This has a couple of edge cases. 368 // This has a couple of edge cases.
344 // 1) The owning object may have indicated we should use a different 369 // 1) The owning object may have indicated we should use a different
345 // rule than the default. 370 // rule than the default.
346 // 2) We may not have a rule, in which case we lazily create a BasicRule. 371 // 2) We may not have a rule, in which case we lazily create a BasicRule.
347 // 3) Rules are allowed to say mustBePrimary, meaning that they can be used 372 // 3) Rules are allowed to say mustBePrimary, meaning that they can be used
348 // iff no other rule was chosen first. 373 // iff no other rule was chosen first.
349 // TODO(alanknight): Can the mustBePrimary mechanism be removed or changed. 374 // TODO(alanknight): Can the mustBePrimary mechanism be removed or changed.
350 // It adds an order dependency to the rules, and is messy. Reconsider in the 375 // It adds an order dependency to the rules, and is messy. Reconsider in the
351 // light of a more general mechanism for multiple rules per object. 376 // light of a more general mechanism for multiple rules per object.
352 // TODO(alanknight): Finding which rules apply seems likely to be a 377 // TODO(alanknight): Finding which rules apply seems likely to be a
353 // bottleneck, particularly with the current reflective implementation. 378 // bottleneck, particularly with the current reflective implementation.
354 // Consider how to improve it. e.g. cache the list of rules by class. But 379 // Consider how to improve it. e.g. cache the list of rules by class. But
355 // be careful of issues like rules which have arbitrary predicates. Or 380 // be careful of issues like rules which have arbitrary predicates. Or
356 // consider having the arbitrary predicates be secondary to an initial 381 // consider having the arbitrary predicates be secondary to an initial
357 // class-based lookup mechanism. 382 // class-based lookup mechanism.
358 var target, candidateRules; 383 var target, candidateRules;
359 if (object is DesignatedRuleForObject) { 384 if (object is DesignatedRuleForObject) {
360 target = object.target; 385 target = object.target;
361 candidateRules = object.possibleRules(_rules); 386 candidateRules = object.possibleRules(_rules);
362 } else { 387 } else {
363 target = object; 388 target = object;
364 candidateRules = _rules; 389 candidateRules = _rules;
365 } 390 }
366 List applicable = 391 Iterable applicable = candidateRules.where(
367 candidateRules.where((each) => each.appliesTo(target, w)).toList(); 392 (each) => each.appliesTo(target, w));
368 393
369 if (applicable.isEmpty) { 394 if (applicable.isEmpty) {
370 return [addRuleFor(target)]; 395 return [addRuleFor(target)];
371 } 396 }
372 397
373 if (applicable.length == 1) return applicable; 398 if (applicable.length == 1) return applicable;
374 var first = applicable[0]; 399 var first = applicable.first;
375 var finalRules = applicable.where( 400 var finalRules = applicable.where(
376 (x) => !x.mustBePrimary || (x == first)).toList(); 401 (x) => !x.mustBePrimary || (x == first));
377 402
378 if (finalRules.isEmpty) throw new SerializationException( 403 if (finalRules.isEmpty) throw new SerializationException(
379 'No valid rule found for object $object'); 404 'No valid rule found for object $object');
380 return finalRules; 405 return finalRules;
381 } 406 }
382 407
383 /** 408 /**
384 * Create a Serialization for serializing SerializationRules. This is used 409 * Create a Serialization for serializing SerializationRules. This is used
385 * to save the rules in a self-describing format along with the data. 410 * to save the rules in a self-describing format along with the data.
386 * If there are new rule classes created, they will need to be described 411 * If there are new rule classes created, they will need to be described
387 * here. 412 * here.
388 */ 413 */
389 Serialization _ruleSerialization() { 414 Serialization ruleSerialization() {
390 // TODO(alanknight): There's an extensibility issue here with new rules. 415 // TODO(alanknight): There's an extensibility issue here with new rules.
391 // TODO(alanknight): How to handle rules with closures? They have to 416 // TODO(alanknight): How to handle rules with closures? They have to
392 // exist on the other side, but we might be able to hook them up by name, 417 // exist on the other side, but we might be able to hook them up by name,
393 // or we might just be able to validate that they're correctly set up 418 // or we might just be able to validate that they're correctly set up
394 // on the other side. 419 // on the other side.
395 420
396 // Make some bogus rule instances so we have something to feed rule creation 421 // Make some bogus rule instances so we have something to feed rule creation
397 // and get their types. If only we had class literals implemented... 422 // and get their types. If only we had class literals implemented...
398 var basicRule = new BasicRule(reflect(null).type, '', [], [], []); 423 var basicRule = new BasicRule(reflect(null).type, '', [], [], []);
399 424
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 } 456 }
432 } 457 }
433 458
434 /** 459 /**
435 * An exception class for errors during serialization. 460 * An exception class for errors during serialization.
436 */ 461 */
437 class SerializationException implements Exception { 462 class SerializationException implements Exception {
438 final String message; 463 final String message;
439 const SerializationException([this.message]); 464 const SerializationException([this.message]);
440 } 465 }
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