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

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

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
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 part of serialization; 5 part of serialization;
6 6
7 /** 7 /**
8 * This writes out the state of the objects to an external format. It holds 8 * This writes out the state of the objects to an external format. It holds
9 * all of the intermediate state needed. The primary API for it is the 9 * all of the intermediate state needed. The primary API for it is the
10 * [write] method. 10 * [write] method.
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 int _objectNumberFor(object) { 219 int _objectNumberFor(object) {
220 var reference = references[object]; 220 var reference = references[object];
221 return (reference == null) ? -1 : reference.objectNumber; 221 return (reference == null) ? -1 : reference.objectNumber;
222 } 222 }
223 223
224 /** 224 /**
225 * Return the serialized data in string format. Currently hard-coded to 225 * Return the serialized data in string format. Currently hard-coded to
226 * our custom JSON format. 226 * our custom JSON format.
227 */ 227 */
228 String toStringFormat() { 228 String toStringFormat() {
229 return JSON.stringify(toMaps()); 229 return json.stringify(toMaps());
230 } 230 }
231 231
232 /** 232 /**
233 * Returns the full serialized structure as nested maps. The top-level 233 * Returns the full serialized structure as nested maps. The top-level
234 * has 3 fields, "rules" which may hold a definition of the rules used, 234 * has 3 fields, "rules" which may hold a definition of the rules used,
235 * "data" which holds the serialized data, and "roots", which holds 235 * "data" which holds the serialized data, and "roots", which holds
236 * [Reference] objects indicating the root objects. Note that roots are 236 * [Reference] objects indicating the root objects. Note that roots are
237 * necessary because the data is organized in the same way as the object 237 * necessary because the data is organized in the same way as the object
238 * structure, it's a list of lists holding self-contained maps which only 238 * structure, it's a list of lists holding self-contained maps which only
239 * refer to other parts via [Reference] objects. 239 * refer to other parts via [Reference] objects.
(...skipping 13 matching lines...) Expand all
253 result["data"] = states; 253 result["data"] = states;
254 result["roots"] = _rootReferences(trace.roots); 254 result["roots"] = _rootReferences(trace.roots);
255 return result; 255 return result;
256 } 256 }
257 257
258 /** 258 /**
259 * Return a list of [Reference] objects pointing to our roots. This will be 259 * Return a list of [Reference] objects pointing to our roots. This will be
260 * stored in the output under "roots" in the default format. 260 * stored in the output under "roots" in the default format.
261 */ 261 */
262 _rootReferences(roots) => 262 _rootReferences(roots) =>
263 roots.map(_referenceFor); 263 roots.mappedBy(_referenceFor).toList();
264 264
265 /** 265 /**
266 * Given an object, return a reference for it if one exists. If there's 266 * Given an object, return a reference for it if one exists. If there's
267 * no reference, return null. Once we have finished the tracing step, all 267 * no reference, return null. Once we have finished the tracing step, all
268 * objects that should have a reference (roughly speaking, non-primitives) 268 * objects that should have a reference (roughly speaking, non-primitives)
269 * can be relied on to have a reference. 269 * can be relied on to have a reference.
270 */ 270 */
271 _referenceFor(object) => references[object]; 271 _referenceFor(object) => references[object];
272 272
273 /** 273 /**
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 */ 362 */
363 List<SerializationRule> get rules => serialization._rules; 363 List<SerializationRule> get rules => serialization._rules;
364 364
365 /** 365 /**
366 * Internal use only, for testing purposes. Set the data for this reader 366 * Internal use only, for testing purposes. Set the data for this reader
367 * to a List of Lists whose size must match the number of rules. 367 * to a List of Lists whose size must match the number of rules.
368 */ 368 */
369 // When we set the data, initialize the object storage to a matching size. 369 // When we set the data, initialize the object storage to a matching size.
370 void set data(List<List> newData) { 370 void set data(List<List> newData) {
371 _data = newData; 371 _data = newData;
372 objects = _data.map((x) => new List(x.length)); 372 objects = _data.mappedBy((x) => new List(x.length)).toList();
373 } 373 }
374 374
375 /** 375 /**
376 * This is the primary method for a [Reader]. It takes the input data, 376 * This is the primary method for a [Reader]. It takes the input data,
377 * currently hard-coded to expect our custom JSON format, and returns 377 * currently hard-coded to expect our custom JSON format, and returns
378 * the root object. 378 * the root object.
379 */ 379 */
380 read(String input, [Map externals = const {}]) { 380 read(String input, [Map externals = const {}]) {
381 namedObjects = externals; 381 namedObjects = externals;
382 var topLevel = JSON.parse(input); 382 var topLevel = json.parse(input);
383 var ruleString = topLevel["rules"]; 383 var ruleString = topLevel["rules"];
384 readRules(ruleString, externals); 384 readRules(ruleString, externals);
385 data = topLevel["data"]; 385 data = topLevel["data"];
386 rules.forEach(inflateForRule); 386 rules.forEach(inflateForRule);
387 var roots = topLevel["roots"]; 387 var roots = topLevel["roots"];
388 return inflateReference(roots.first); 388 return inflateReference(roots.first);
389 } 389 }
390 390
391 /** 391 /**
392 * If the data we are reading from has rules written to it, read them back 392 * If the data we are reading from has rules written to it, read them back
(...skipping 15 matching lines...) Expand all
408 * of fixing and generalization. 408 * of fixing and generalization.
409 */ 409 */
410 readFlat(List input, [Map externals = const {}]) { 410 readFlat(List input, [Map externals = const {}]) {
411 // TODO(alanknight): Way too much code duplication with read. Numerous 411 // TODO(alanknight): Way too much code duplication with read. Numerous
412 // code smells. 412 // code smells.
413 namedObjects = externals; 413 namedObjects = externals;
414 var topLevel = input; 414 var topLevel = input;
415 var ruleString = topLevel[0]; 415 var ruleString = topLevel[0];
416 readRules(ruleString, externals); 416 readRules(ruleString, externals);
417 var flatData = topLevel[1]; 417 var flatData = topLevel[1];
418 var stream = flatData.iterator(); 418 var stream = flatData.iterator;
419 var tempData = new List(rules.length); 419 var tempData = new List(rules.length);
420 for (var eachRule in rules) { 420 for (var eachRule in rules) {
421 tempData[eachRule.number] = eachRule.pullStateFrom(stream); 421 tempData[eachRule.number] = eachRule.pullStateFrom(stream);
422 } 422 }
423 data = tempData; 423 data = tempData;
424 for (var eachRule in rules) { 424 for (var eachRule in rules) {
425 inflateForRule(eachRule); 425 inflateForRule(eachRule);
426 } 426 }
427 var rootsAsInts = topLevel[2]; 427 var rootsAsInts = topLevel[2];
428 var rootStream = rootsAsInts.iterator(); 428 var rootStream = rootsAsInts.iterator;
429 var roots = new List(); 429 var roots = new List();
430 while (rootStream.hasNext) { 430 while (rootStream.moveNext()) {
431 roots.add(new Reference(this, rootStream.next(), rootStream.next())); 431 var first = rootStream.current;
432 rootStream.moveNext();
433 var second = rootStream.current;
434 roots.add(new Reference(this, first, second));
432 } 435 }
433 var x = inflateReference(roots[0]); 436 var x = inflateReference(roots[0]);
434 return inflateReference(roots.first); 437 return inflateReference(roots.first);
435 } 438 }
436 439
437 /** 440 /**
438 * Inflate all of the objects for [rule]. Does the essential state for all 441 * Inflate all of the objects for [rule]. Does the essential state for all
439 * objects first, then the non-essential state. This avoids cycles in 442 * objects first, then the non-essential state. This avoids cycles in
440 * non-essential state, because all the objects will have already been 443 * non-essential state, because all the objects will have already been
441 * created. 444 * created.
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 * found for it. This normally only makes sense if the object is uniquely 642 * found for it. This normally only makes sense if the object is uniquely
640 * referenced, and is a more or less internal collection. See ListRuleEssential 643 * referenced, and is a more or less internal collection. See ListRuleEssential
641 * for an example. It knows how to return its object and how to filter. 644 * for an example. It knows how to return its object and how to filter.
642 */ 645 */
643 class DesignatedRuleForObject { 646 class DesignatedRuleForObject {
644 Function rulePredicate; 647 Function rulePredicate;
645 final target; 648 final target;
646 649
647 DesignatedRuleForObject(this.target, this.rulePredicate); 650 DesignatedRuleForObject(this.target, this.rulePredicate);
648 651
649 possibleRules(List rules) => rules.filter(rulePredicate); 652 possibleRules(List rules) => rules.where(rulePredicate).toList();
650 } 653 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/src/mirrors_helpers.dart ('k') | pkg/serialization/lib/src/serialization_helpers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698