| OLD | NEW |
| 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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 234 |
| 235 /** | 235 /** |
| 236 * An abstract class for Reader and Writer, which primarily exists so we can | 236 * An abstract class for Reader and Writer, which primarily exists so we can |
| 237 * type things that will refer to one or the other, depending on which | 237 * type things that will refer to one or the other, depending on which |
| 238 * operation we're doing. | 238 * operation we're doing. |
| 239 */ | 239 */ |
| 240 abstract class ReaderOrWriter { | 240 abstract class ReaderOrWriter { |
| 241 /** Return the list of serialization rules we are using.*/ | 241 /** Return the list of serialization rules we are using.*/ |
| 242 List<SerializationRule> get rules; | 242 List<SerializationRule> get rules; |
| 243 | 243 |
| 244 /** Return the internal collection of object state and [Reference] objects. */ |
| 245 List<List> get states; |
| 246 |
| 244 /** | 247 /** |
| 245 * Return the object, or state, that ref points to, depending on which | 248 * Return the object, or state, that ref points to, depending on which |
| 246 * we're generating. | 249 * we're generating. |
| 247 */ | 250 */ |
| 248 resolveReference(Reference ref); | 251 resolveReference(Reference ref); |
| 249 } | 252 } |
| 250 | 253 |
| 251 /** | 254 /** |
| 252 * The main class responsible for reading. It holds | 255 * The main class responsible for reading. It holds |
| 253 * onto the necessary state and to the objects that have been inflated. | 256 * onto the necessary state and to the objects that have been inflated. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 271 /** | 274 /** |
| 272 * The state of objects that have been serialized is stored here. | 275 * The state of objects that have been serialized is stored here. |
| 273 * Each rule has a number, and rules keep track of the objects that they | 276 * Each rule has a number, and rules keep track of the objects that they |
| 274 * serialize, in order. So the state of any object can be found by indexing | 277 * serialize, in order. So the state of any object can be found by indexing |
| 275 * from the rule number and the object number within the rule. | 278 * from the rule number and the object number within the rule. |
| 276 * The actual representation of the state is determined by the rule. Lists | 279 * The actual representation of the state is determined by the rule. Lists |
| 277 * and Maps are common, but it is arbitrary. See [Writer.states]. | 280 * and Maps are common, but it is arbitrary. See [Writer.states]. |
| 278 */ | 281 */ |
| 279 List<List> _data; | 282 List<List> _data; |
| 280 | 283 |
| 284 /** Return the internal collection of object state and [Reference] objects. */ |
| 285 get states => _data; |
| 286 |
| 281 /** | 287 /** |
| 282 * The resulting objects, indexed according to the same scheme as | 288 * The resulting objects, indexed according to the same scheme as |
| 283 * _data, where each rule has a number, and rules keep track of the objects | 289 * _data, where each rule has a number, and rules keep track of the objects |
| 284 * that they serialize, in order. | 290 * that they serialize, in order. |
| 285 */ | 291 */ |
| 286 List<List> objects; | 292 List<List> objects; |
| 287 | 293 |
| 288 final Format format; | 294 final Format format; |
| 289 | 295 |
| 290 /** | 296 /** |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 | 466 |
| 461 /** | 467 /** |
| 462 * Given a possible reference [anObject], call either [ifReference] or | 468 * Given a possible reference [anObject], call either [ifReference] or |
| 463 * [ifNotReference], depending if it's a reference or not. This is the | 469 * [ifNotReference], depending if it's a reference or not. This is the |
| 464 * primary place that knows about the serialized representation of a | 470 * primary place that knows about the serialized representation of a |
| 465 * reference. | 471 * reference. |
| 466 */ | 472 */ |
| 467 asReference(anObject, {Function ifReference: doNothing, | 473 asReference(anObject, {Function ifReference: doNothing, |
| 468 Function ifNotReference : doNothing}) { | 474 Function ifNotReference : doNothing}) { |
| 469 if (anObject is Reference) return ifReference(anObject); | 475 if (anObject is Reference) return ifReference(anObject); |
| 470 if (anObject is Map && anObject["__Ref"] == true) { | 476 if (anObject is Map && anObject["__Ref"] != null) { |
| 471 var ref = | 477 var ref = |
| 472 new Reference(this, anObject["rule"], anObject["object"]); | 478 new Reference(this, anObject["rule"], anObject["object"]); |
| 473 return ifReference(ref); | 479 return ifReference(ref); |
| 474 } else { | 480 } else { |
| 475 return ifNotReference(anObject); | 481 return ifNotReference(anObject); |
| 476 } | 482 } |
| 477 } | 483 } |
| 478 } | 484 } |
| 479 | 485 |
| 480 /** | 486 /** |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 */ | 584 */ |
| 579 inflated() => parent.resolveReference(this); | 585 inflated() => parent.resolveReference(this); |
| 580 | 586 |
| 581 /** | 587 /** |
| 582 * Convert the reference to a map in JSON format. This is specific to the | 588 * Convert the reference to a map in JSON format. This is specific to the |
| 583 * custom JSON format we define, and must be consistent with the | 589 * custom JSON format we define, and must be consistent with the |
| 584 * [Reader.asReference] method. | 590 * [Reader.asReference] method. |
| 585 */ | 591 */ |
| 586 // TODO(alanknight): This is a hack both in defining a toJson specific to a | 592 // TODO(alanknight): This is a hack both in defining a toJson specific to a |
| 587 // particular representation, and the use of a bogus sentinel "__Ref" | 593 // particular representation, and the use of a bogus sentinel "__Ref" |
| 588 Map<String, dynamic> toJson() => { | 594 Map<String, int> toJson() => { |
| 589 "__Ref" : true, | 595 "__Ref" : 0, |
| 590 "rule" : ruleNumber, | 596 "rule" : ruleNumber, |
| 591 "object" : objectNumber | 597 "object" : objectNumber |
| 592 }; | 598 }; |
| 593 | 599 |
| 594 /** Write our information to [list]. Useful in writing to flat formats.*/ | 600 /** Write our information to [list]. Useful in writing to flat formats.*/ |
| 595 void writeToList(List list) { | 601 void writeToList(List list) { |
| 596 list.add(ruleNumber); | 602 list.add(ruleNumber); |
| 597 list.add(objectNumber); | 603 list.add(objectNumber); |
| 598 } | 604 } |
| 599 | 605 |
| 600 String toString() => "Reference($ruleNumber, $objectNumber)"; | 606 String toString() => "Reference($ruleNumber, $objectNumber)"; |
| 601 } | 607 } |
| 602 | 608 |
| 603 /** | 609 /** |
| 604 * This is used during tracing to indicate that an object should be processed | 610 * This is used during tracing to indicate that an object should be processed |
| 605 * using a particular rule, rather than the one that might ordinarily be | 611 * using a particular rule, rather than the one that might ordinarily be |
| 606 * found for it. This normally only makes sense if the object is uniquely | 612 * found for it. This normally only makes sense if the object is uniquely |
| 607 * referenced, and is a more or less internal collection. See ListRuleEssential | 613 * referenced, and is a more or less internal collection. See ListRuleEssential |
| 608 * for an example. It knows how to return its object and how to filter. | 614 * for an example. It knows how to return its object and how to filter. |
| 609 */ | 615 */ |
| 610 class DesignatedRuleForObject { | 616 class DesignatedRuleForObject { |
| 611 final Function rulePredicate; | 617 final Function rulePredicate; |
| 612 final target; | 618 final target; |
| 613 | 619 |
| 614 DesignatedRuleForObject(this.target, this.rulePredicate); | 620 DesignatedRuleForObject(this.target, this.rulePredicate); |
| 615 | 621 |
| 616 List possibleRules(List rules) => rules.where(rulePredicate).toList(); | 622 List possibleRules(List rules) => rules.where(rulePredicate).toList(); |
| 617 } | 623 } |
| OLD | NEW |