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

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

Issue 17578002: pkg/serialization: add format param to Serialization.read method (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fewer things Created 7 years, 5 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 part of serialization; 1 part of serialization;
2 2
3 /** 3 /**
4 * An abstract class for serialization formats. Subclasses define how data 4 * An abstract class for serialization formats. Subclasses define how data
5 * is read or written to a particular output mechanism. 5 * is read or written to a particular output mechanism.
6 */ 6 */
7 abstract class Format { 7 abstract class Format {
8 8
9 const Format(); 9 const Format();
10 10
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 result["roots"] = result["roots"].map( 108 result["roots"] = result["roots"].map(
109 (x) => x is Reference ? referenceToMap(x) : x).toList(); 109 (x) => x is Reference ? referenceToMap(x) : x).toList();
110 return result; 110 return result;
111 } 111 }
112 112
113 /** 113 /**
114 * Convert the data generated by the rules to have maps with the fields 114 * Convert the data generated by the rules to have maps with the fields
115 * of [Reference] objects instead of the [Reference] so that the structure 115 * of [Reference] objects instead of the [Reference] so that the structure
116 * can be serialized between isolates and json easily. 116 * can be serialized between isolates and json easily.
117 */ 117 */
118 forAllStates(ReaderOrWriter w, bool predicate(value), 118 void forAllStates(ReaderOrWriter w, bool predicate(value),
119 void transform(value)) { 119 void transform(value)) {
120 for (var eachRule in w.rules) { 120 for (var eachRule in w.rules) {
121 var ruleData = w.states[eachRule.number]; 121 var ruleData = w.states[eachRule.number];
122 for (var data in ruleData) { 122 for (var data in ruleData) {
123 keysAndValues(data).forEach((key, value) { 123 keysAndValues(data).forEach((key, value) {
124 if (predicate(value)) { 124 if (predicate(value)) {
125 data[key] = transform(value); 125 data[key] = transform(value);
126 } 126 }
127 }); 127 });
128 } 128 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 ..[DATA] = root; 207 ..[DATA] = root;
208 } 208 }
209 return root; 209 return root;
210 } 210 }
211 211
212 /** 212 /**
213 * Convert the data generated by the rules to have nested maps instead 213 * Convert the data generated by the rules to have nested maps instead
214 * of Reference objects and to add rule numbers if [storeRoundTripInfo] 214 * of Reference objects and to add rule numbers if [storeRoundTripInfo]
215 * is true. 215 * is true.
216 */ 216 */
217 jsonify(Writer w) { 217 void jsonify(Writer w) {
218 for (var eachRule in w.rules) { 218 for (var eachRule in w.rules) {
219 var ruleData = w.states[eachRule.number]; 219 var ruleData = w.states[eachRule.number];
220 jsonifyForRule(ruleData, w, eachRule); 220 jsonifyForRule(ruleData, w, eachRule);
221 } 221 }
222 } 222 }
223 223
224 /** 224 /**
225 * For a particular [rule] modify the [ruleData] to conform to this format. 225 * For a particular [rule] modify the [ruleData] to conform to this format.
226 */ 226 */
227 jsonifyForRule(List ruleData, Writer w, SerializationRule rule) { 227 void jsonifyForRule(List ruleData, Writer w, SerializationRule rule) {
228 for (var i = 0; i < ruleData.length; i++) { 228 for (var i = 0; i < ruleData.length; i++) {
229 var each = ruleData[i]; 229 var each = ruleData[i];
230 if (each is List) { 230 if (each is List) {
231 jsonifyEntry(each, w); 231 jsonifyEntry(each, w);
232 if (storeRoundTripInfo) ruleData[i].add(rule.number); 232 if (storeRoundTripInfo) ruleData[i].add(rule.number);
233 } else if (each is Map) { 233 } else if (each is Map) {
234 jsonifyEntry(each, w); 234 jsonifyEntry(each, w);
235 if (storeRoundTripInfo) each[RULE] = rule.number; 235 if (storeRoundTripInfo) each[RULE] = rule.number;
236 } 236 }
237 } 237 }
238 } 238 }
239 239
240 /** 240 /**
241 * For one particular entry, which is either a Map or a List, update it 241 * For one particular entry, which is either a Map or a List, update it
242 * to turn References into a nested List/Map. 242 * to turn References into a nested List/Map.
243 */ 243 */
244 jsonifyEntry(map, Writer w) { 244 void jsonifyEntry(map, Writer w) {
245 // Note, if this is a Map, and the key might be a reference, we need to 245 // Note, if this is a Map, and the key might be a reference, we need to
246 // bend over backwards to avoid concurrent modifications. Non-string keys 246 // bend over backwards to avoid concurrent modifications. Non-string keys
247 // won't actually work if we try to write this to json, but might happen 247 // won't actually work if we try to write this to json, but might happen
248 // if e.g. sending between isolates. 248 // if e.g. sending between isolates.
249 var updates = new Map(); 249 var updates = new Map();
250 keysAndValues(map).forEach((key, value) { 250 keysAndValues(map).forEach((key, value) {
251 if (value is Reference) updates[key] = w.stateForReference(value); 251 if (value is Reference) updates[key] = w.stateForReference(value);
252 }); 252 });
253 updates.forEach((k, v) => map[k] = v); 253 updates.forEach((k, v) => map[k] = v);
254 } 254 }
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 return []; 489 return [];
490 } else { 490 } else {
491 throw new SerializationException("Invalid data in serialization"); 491 throw new SerializationException("Invalid data in serialization");
492 } 492 }
493 } 493 }
494 494
495 /** 495 /**
496 * Read data for [rule] from [input] with [length] number of entries, 496 * Read data for [rule] from [input] with [length] number of entries,
497 * creating lists from the results. 497 * creating lists from the results.
498 */ 498 */
499 readLists(Iterator input, SerializationRule rule, int length, Reader r) { 499 List readLists(Iterator input, SerializationRule rule, int length, Reader r) {
500 var ruleData = []; 500 var ruleData = [];
501 for (var i = 0; i < length; i++) { 501 for (var i = 0; i < length; i++) {
502 var subLength = 502 var subLength =
503 rule.hasVariableLengthEntries ? _next(input) : rule.dataLength; 503 rule.hasVariableLengthEntries ? _next(input) : rule.dataLength;
504 var subList = []; 504 var subList = [];
505 ruleData.add(subList); 505 ruleData.add(subList);
506 for (var j = 0; j < subLength; j++) { 506 for (var j = 0; j < subLength; j++) {
507 subList.add(nextReferenceFrom(input, r)); 507 subList.add(nextReferenceFrom(input, r));
508 } 508 }
509 } 509 }
510 return ruleData; 510 return ruleData;
511 } 511 }
512 512
513 /** 513 /**
514 * Read data for [rule] from [input] with [length] number of entries, 514 * Read data for [rule] from [input] with [length] number of entries,
515 * creating maps from the results. 515 * creating maps from the results.
516 */ 516 */
517 readMaps(Iterator input, SerializationRule rule, int length, Reader r) { 517 List readMaps(Iterator input, SerializationRule rule, int length, Reader r) {
518 var ruleData = []; 518 var ruleData = [];
519 for (var i = 0; i < length; i++) { 519 for (var i = 0; i < length; i++) {
520 var subLength = 520 var subLength =
521 rule.hasVariableLengthEntries ? _next(input) : rule.dataLength; 521 rule.hasVariableLengthEntries ? _next(input) : rule.dataLength;
522 var map = new Map(); 522 var map = new Map();
523 ruleData.add(map); 523 ruleData.add(map);
524 for (var j = 0; j < subLength; j++) { 524 for (var j = 0; j < subLength; j++) {
525 var key = nextReferenceFrom(input, r); 525 var key = nextReferenceFrom(input, r);
526 var value = nextReferenceFrom(input, r); 526 var value = nextReferenceFrom(input, r);
527 map[key] = value; 527 map[key] = value;
528 } 528 }
529 } 529 }
530 return ruleData; 530 return ruleData;
531 } 531 }
532 532
533 /** 533 /**
534 * Read data for [rule] from [input] with [length] number of entries, 534 * Read data for [rule] from [input] with [length] number of entries,
535 * treating the data as primitives that can be returned directly. 535 * treating the data as primitives that can be returned directly.
536 */ 536 */
537 readPrimitives(Iterator input, SerializationRule rule, int length) { 537 List readPrimitives(Iterator input, SerializationRule rule, int length) {
538 var ruleData = []; 538 var ruleData = [];
539 for (var i = 0; i < length; i++) { 539 for (var i = 0; i < length; i++) {
540 ruleData.add(_next(input)); 540 ruleData.add(_next(input));
541 } 541 }
542 return ruleData; 542 return ruleData;
543 } 543 }
544 544
545 /** Read the next Reference from the input. */ 545 /** Read the next Reference from the input. */
546 Reference nextReferenceFrom(Iterator input, Reader r) { 546 Reference nextReferenceFrom(Iterator input, Reader r) {
547 var a = _next(input); 547 var a = _next(input);
548 var b = _next(input); 548 var b = _next(input);
549 if (a == null) { 549 if (a == null) {
550 return null; 550 return null;
551 } else { 551 } else {
552 return new Reference(r, a, b); 552 return new Reference(r, a, b);
553 } 553 }
554 } 554 }
555 555
556 /** Return the next element from the input. */ 556 /** Return the next element from the input. */
557 _next(Iterator input) { 557 _next(Iterator input) {
558 input.moveNext(); 558 input.moveNext();
559 return input.current; 559 return input.current;
560 } 560 }
561 } 561 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/serialization.dart ('k') | pkg/serialization/test/serialization_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698