OLD | NEW |
(Empty) | |
| 1 part of serialization; |
| 2 |
| 3 /** |
| 4 * An abstract class for serialization formats. Subclasses define how data |
| 5 * is read or written to a particular output mechanism. |
| 6 */ |
| 7 abstract class Format { |
| 8 |
| 9 const Format(); |
| 10 |
| 11 /** |
| 12 * Return true if this format stores primitives in their own area and uses |
| 13 * references to them (e.g. [SimpleFlatFormat]) and false if primitives |
| 14 * are stored directly (e.g. [SimpleJsonFormat], [SimpleMapFormat]). |
| 15 */ |
| 16 bool get shouldUseReferencesForPrimitives => false; |
| 17 |
| 18 /** |
| 19 * Generate output for [w] and return it. The particular form of the output |
| 20 * will depend on the format. The format can assume that [w] has data |
| 21 * generated by rules in a series of lists, and that each list will contain |
| 22 * either primitives (null, bool, num, String), Lists or Maps. The Lists or |
| 23 * Maps may contain any of the same things recursively, or may contain |
| 24 * Reference objects. For lists and maps the rule will tell us if they can |
| 25 * be of variable length or not. The format is allowed to operate |
| 26 * destructively on the rule data. |
| 27 */ |
| 28 generateOutput(Writer w); |
| 29 |
| 30 /** |
| 31 * Read the data from [input] in the context of [reader] and return it as a |
| 32 * Map with entries for "roots", "data" and "rules", which the reader knows |
| 33 * how to interpret. The type of [input] will depend on the particular format. |
| 34 */ |
| 35 Map<String, dynamic> read(input, Reader reader); |
| 36 } |
| 37 |
| 38 /** |
| 39 * This is the most basic format, which provides the internal representation |
| 40 * of the serialization, exposing the Reference objects. |
| 41 */ |
| 42 class InternalMapFormat extends Format { |
| 43 const InternalMapFormat(); |
| 44 |
| 45 /** |
| 46 * Generate output for this format from [w] and return it as a nested Map |
| 47 * structure. The top level has |
| 48 * 3 fields, "rules" which may hold a definition of the rules used, |
| 49 * "data" which holds the serialized data, and "roots", which holds |
| 50 * [Reference] objects indicating the root objects. Note that roots are |
| 51 * necessary because the data is not organized in the same way as the object |
| 52 * structure, it's a list of lists holding self-contained maps which only |
| 53 * refer to other parts via [Reference] objects. |
| 54 */ |
| 55 Map<String, dynamic> generateOutput(Writer w) { |
| 56 var result = { |
| 57 "rules" : w.serializedRules(), |
| 58 "data" : w.states, |
| 59 "roots" : w._rootReferences() |
| 60 }; |
| 61 return result; |
| 62 } |
| 63 |
| 64 /** |
| 65 * Read serialized data written from this format |
| 66 * and return the nested Map representation described in [generateOutput]. If |
| 67 * the data also includes rule definitions, then these will replace the rules |
| 68 * in the [Serialization] for [reader]. |
| 69 */ |
| 70 Map<String, dynamic> read(Map<String, dynamic> topLevel, Reader reader) { |
| 71 var ruleString = topLevel["rules"]; |
| 72 reader.readRules(ruleString); |
| 73 reader._data = topLevel["data"]; |
| 74 topLevel["roots"] = topLevel["roots"]; |
| 75 return topLevel; |
| 76 } |
| 77 } |
| 78 |
| 79 /** |
| 80 * A format that stores the data in maps which can be converted into a JSON |
| 81 * string or passed through an isolate. Note that this consists of maps, but |
| 82 * that they don't follow the original object structure or look like the nested |
| 83 * maps of a [json] representation. They are flat, and [Reference] objects |
| 84 * are converted into a map form that will not make sense to |
| 85 * anything but this format. For simple acyclic JSON that other programs |
| 86 * can read, use [SimpleJsonFormat]. This is the default format, and is |
| 87 * easier to read than the more efficient [SimpleFlatFormat]. |
| 88 */ |
| 89 class SimpleMapFormat extends InternalMapFormat { |
| 90 |
| 91 const SimpleMapFormat(); |
| 92 |
| 93 /** |
| 94 * Generate output for this format from [w] and return it as a String which |
| 95 * is the [json] representation of a nested Map structure. The top level has |
| 96 * 3 fields, "rules" which may hold a definition of the rules used, |
| 97 * "data" which holds the serialized data, and "roots", which holds |
| 98 * [Reference] objects indicating the root objects. Note that roots are |
| 99 * necessary because the data is not organized in the same way as the object |
| 100 * structure, it's a list of lists holding self-contained maps which only |
| 101 * refer to other parts via [Reference] objects. |
| 102 * This effectively defines a custom JSON serialization format, although |
| 103 * the details of the format vary depending which rules were used. |
| 104 */ |
| 105 Map<String, dynamic> generateOutput(Writer w) { |
| 106 forAllStates(w, (x) => x is Reference, referenceToMap); |
| 107 var result = super.generateOutput(w); |
| 108 result["roots"] = result["roots"].map( |
| 109 (x) => x is Reference ? referenceToMap(x) : x).toList(); |
| 110 return result; |
| 111 } |
| 112 |
| 113 /** |
| 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 |
| 116 * can be serialized between isolates and json easily. |
| 117 */ |
| 118 void forAllStates(ReaderOrWriter w, bool predicate(value), |
| 119 void transform(value)) { |
| 120 for (var eachRule in w.rules) { |
| 121 var ruleData = w.states[eachRule.number]; |
| 122 for (var data in ruleData) { |
| 123 keysAndValues(data).forEach((key, value) { |
| 124 if (predicate(value)) { |
| 125 data[key] = transform(value); |
| 126 } |
| 127 }); |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 /** Convert the reference to a [json] serializable form. */ |
| 133 Map<String, int> referenceToMap(Reference ref) => ref == null ? null : |
| 134 { |
| 135 "__Ref" : 0, |
| 136 "rule" : ref.ruleNumber, |
| 137 "object" : ref.objectNumber |
| 138 }; |
| 139 |
| 140 /** |
| 141 * Convert the [referenceToMap] form for a reference back to a [Reference] |
| 142 * object. |
| 143 */ |
| 144 Reference mapToReference(ReaderOrWriter parent, Map<String, int> ref) => |
| 145 ref == null ? null : new Reference(parent, ref["rule"], ref["object"]); |
| 146 |
| 147 /** |
| 148 * Read serialized data written in this format |
| 149 * and return the nested Map representation described in [generateOutput]. If |
| 150 * the data also includes rule definitions, then these will replace the rules |
| 151 * in the [Serialization] for [reader]. |
| 152 */ |
| 153 Map<String, dynamic> read(Map<String, dynamic> topLevel, Reader reader) { |
| 154 super.read(topLevel, reader); |
| 155 forAllStates(reader, |
| 156 (ref) => ref is Map && ref["__Ref"] != null, |
| 157 (ref) => mapToReference(reader, ref)); |
| 158 topLevel["roots"] = topLevel["roots"] |
| 159 .map((x) => x is Map<String, int> ? mapToReference(reader, x) : x) |
| 160 .toList(); |
| 161 return topLevel; |
| 162 } |
| 163 } |
| 164 |
| 165 /** |
| 166 * A format for "normal" [json] representation of objects. It stores |
| 167 * the fields of the objects as nested maps, and doesn't allow cycles. This can |
| 168 * be useful in talking to existing APIs that expect [json] format data. The |
| 169 * output will be either a simple object (string, num, bool), a List, or a Map, |
| 170 * with nesting of those. |
| 171 * Note that since the classes of objects aren't normally stored, this isn't |
| 172 * enough information to read back the objects. However, if the |
| 173 * If the [storeRoundTripInfo] field of the format is set to true, then this |
| 174 * will store the rule number along with the data, allowing reconstruction. |
| 175 */ |
| 176 class SimpleJsonFormat extends SimpleMapFormat { |
| 177 |
| 178 /** |
| 179 * Indicate if we should store rule numbers with map/list data so that we |
| 180 * will know how to reconstruct it with a read operation. If we don't, this |
| 181 * will be more compliant with things that expect known format JSON as input, |
| 182 * but we won't be able to read back the objects. |
| 183 */ |
| 184 final bool storeRoundTripInfo; |
| 185 |
| 186 /** |
| 187 * If we store the rule numbers, what key should we use to store them. |
| 188 */ |
| 189 static const String RULE = "_rule"; |
| 190 static const String RULES = "_rules"; |
| 191 static const String DATA = "_data"; |
| 192 static const String ROOTS = "_root"; |
| 193 |
| 194 const SimpleJsonFormat({this.storeRoundTripInfo : false}); |
| 195 |
| 196 /** |
| 197 * Generate output for this format from [w] and return it as |
| 198 * the [json] representation of a nested Map structure. |
| 199 */ |
| 200 generateOutput(Writer w) { |
| 201 jsonify(w); |
| 202 var root = w._rootReferences().first; |
| 203 if (root is Reference) root = w.stateForReference(root); |
| 204 if (w.selfDescribing && storeRoundTripInfo) { |
| 205 root = new Map() |
| 206 ..[RULES] = w.serializedRules() |
| 207 ..[DATA] = root; |
| 208 } |
| 209 return root; |
| 210 } |
| 211 |
| 212 /** |
| 213 * Convert the data generated by the rules to have nested maps instead |
| 214 * of Reference objects and to add rule numbers if [storeRoundTripInfo] |
| 215 * is true. |
| 216 */ |
| 217 void jsonify(Writer w) { |
| 218 for (var eachRule in w.rules) { |
| 219 var ruleData = w.states[eachRule.number]; |
| 220 jsonifyForRule(ruleData, w, eachRule); |
| 221 } |
| 222 } |
| 223 |
| 224 /** |
| 225 * For a particular [rule] modify the [ruleData] to conform to this format. |
| 226 */ |
| 227 void jsonifyForRule(List ruleData, Writer w, SerializationRule rule) { |
| 228 for (var i = 0; i < ruleData.length; i++) { |
| 229 var each = ruleData[i]; |
| 230 if (each is List) { |
| 231 jsonifyEntry(each, w); |
| 232 if (storeRoundTripInfo) ruleData[i].add(rule.number); |
| 233 } else if (each is Map) { |
| 234 jsonifyEntry(each, w); |
| 235 if (storeRoundTripInfo) each[RULE] = rule.number; |
| 236 } |
| 237 } |
| 238 } |
| 239 |
| 240 /** |
| 241 * For one particular entry, which is either a Map or a List, update it |
| 242 * to turn References into a nested List/Map. |
| 243 */ |
| 244 void jsonifyEntry(map, Writer w) { |
| 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 |
| 247 // won't actually work if we try to write this to json, but might happen |
| 248 // if e.g. sending between isolates. |
| 249 var updates = new Map(); |
| 250 keysAndValues(map).forEach((key, value) { |
| 251 if (value is Reference) updates[key] = w.stateForReference(value); |
| 252 }); |
| 253 updates.forEach((k, v) => map[k] = v); |
| 254 } |
| 255 |
| 256 /** |
| 257 * Read serialized data saved in this format, which should look like |
| 258 * either a simple type, a List or a Map and return the Map |
| 259 * representation that the reader expects, with top-level |
| 260 * entries for "rules", "data", and "roots". Nested lists/maps will be |
| 261 * converted into Reference objects. Note that if the data was not written |
| 262 * with [storeRoundTripInfo] true this will fail. |
| 263 */ |
| 264 Map<String, dynamic> read(data, Reader reader) { |
| 265 var result = new Map(); |
| 266 // Check the case of having been written without additional data and |
| 267 // read as if it had been written with storeRoundTripData set. |
| 268 if (reader.selfDescribing && !(data.containsKey(DATA))) { |
| 269 throw new SerializationException("Missing $DATA entry, " |
| 270 "may mean this was written and read with different values " |
| 271 "of selfDescribing."); |
| 272 } |
| 273 // If we are self-describing, we should have separate rule and data |
| 274 // sections. If not, we assume that we have just the data at the top level. |
| 275 var rules = reader.selfDescribing ? data[RULES] : null; |
| 276 var actualData = reader.selfDescribing ? data[DATA] : data; |
| 277 reader.readRules(rules); |
| 278 var ruleData = new List.generate(reader.rules.length, (_) => []); |
| 279 var top = recursivelyFixUp(actualData, reader, ruleData); |
| 280 result["data"] = ruleData; |
| 281 result["roots"] = [top]; |
| 282 return result; |
| 283 } |
| 284 |
| 285 /** |
| 286 * Convert nested references in [input] into [Reference] objects. |
| 287 */ |
| 288 recursivelyFixUp(input, Reader r, List result) { |
| 289 var data = input; |
| 290 if (isPrimitive(data)) { |
| 291 result[r._primitiveRule().number].add(data); |
| 292 return data; |
| 293 } |
| 294 var ruleNumber; |
| 295 // If we've added the rule number on as the last item in a list we have |
| 296 // to get rid of it or it will be interpreted as extra data. For a map |
| 297 // the library will be ok, but we need to get rid of the extra key before |
| 298 // the data is shown to the user, so we destructively modify. |
| 299 if (data is List) { |
| 300 ruleNumber = data.last; |
| 301 data = data.take(data.length - 1).toList(); |
| 302 } else if (data is Map) { |
| 303 ruleNumber = data.remove(RULE); |
| 304 } else { |
| 305 throw new SerializationException("Invalid data format"); |
| 306 } |
| 307 // Do not use map or other lazy operations for this. They do not play |
| 308 // well with a function that destructively modifies its arguments. |
| 309 var newData = mapValues(data, (each) => recursivelyFixUp(each, r, result)); |
| 310 result[ruleNumber].add(newData); |
| 311 return new Reference(r, ruleNumber, result[ruleNumber].length - 1); |
| 312 } |
| 313 } |
| 314 |
| 315 /** |
| 316 * Writes to a simple mostly-flat format. Details are subject to change. |
| 317 * Right now this produces a List containing null, num, and String. This is |
| 318 * more space-efficient than the map formats, but much less human-readable. |
| 319 * Simple usage is to turn this into JSON for transmission. |
| 320 */ |
| 321 class SimpleFlatFormat extends Format { |
| 322 bool get shouldUseReferencesForPrimitives => true; |
| 323 |
| 324 /** |
| 325 * For each rule we store data to indicate whether it will be reconstructed |
| 326 * as a primitive, a list or a map. |
| 327 */ |
| 328 static const int STORED_AS_LIST = 1; |
| 329 static const int STORED_AS_MAP = 2; |
| 330 static const int STORED_AS_PRIMITIVE = 3; |
| 331 |
| 332 const SimpleFlatFormat(); |
| 333 |
| 334 /** |
| 335 * Generate output for this format from [w]. This will return a List with |
| 336 * three entries, corresponding to the "rules", "data", and "roots" from |
| 337 * [SimpleMapFormat]. The data is stored as a single List containing |
| 338 * primitives. |
| 339 */ |
| 340 List generateOutput(Writer w) { |
| 341 var result = new List(3); |
| 342 var flatData = []; |
| 343 for (var eachRule in w.rules) { |
| 344 var ruleData = w.states[eachRule.number]; |
| 345 flatData.add(ruleData.length); |
| 346 writeStateInto(eachRule, ruleData, flatData); |
| 347 } |
| 348 result[0] = w.serializedRules(); |
| 349 result[1] = flatData; |
| 350 result[2] = []; |
| 351 w._rootReferences().forEach((x) => x.writeToList(result[2])); |
| 352 return result; |
| 353 } |
| 354 |
| 355 /** |
| 356 * Writes the data from [rule] into the [target] list. |
| 357 */ |
| 358 void writeStateInto(SerializationRule rule, List ruleData, List target) { |
| 359 if (!ruleData.isEmpty) { |
| 360 var sample = ruleData.first; |
| 361 if (rule.storesStateAsLists || sample is List) { |
| 362 writeLists(rule, ruleData, target); |
| 363 } else if (rule.storesStateAsMaps || sample is Map) { |
| 364 writeMaps(rule, ruleData, target); |
| 365 } else if (rule.storesStateAsPrimitives || isPrimitive(sample)) { |
| 366 writeObjects(ruleData, target); |
| 367 } else { |
| 368 throw new SerializationException("Invalid data format"); |
| 369 } |
| 370 } else { |
| 371 // If there is no data, write a zero for the length. |
| 372 target.add(0); |
| 373 } |
| 374 } |
| 375 |
| 376 /** |
| 377 * Write [entries], which contains Lists. Either the lists are variable |
| 378 * length, in which case we add a length field, or they are fixed length, in |
| 379 * which case we don't, and assume the [rule] will know how to read the |
| 380 * right length when we read it back. We expect everything in the list to be |
| 381 * a reference, which is stored as two numbers. |
| 382 */ |
| 383 void writeLists(SerializationRule rule, List<List> entries, List target) { |
| 384 target.add(STORED_AS_LIST); |
| 385 for (var eachEntry in entries) { |
| 386 if (rule.hasVariableLengthEntries) { |
| 387 target.add(eachEntry.length); |
| 388 } |
| 389 for (var eachReference in eachEntry) { |
| 390 writeReference(eachReference, target); |
| 391 } |
| 392 } |
| 393 } |
| 394 |
| 395 /** |
| 396 * Write [entries], which contains Maps. Either the Maps are variable |
| 397 * length, in which case we add a length field, or they are fixed length, in |
| 398 * which case we don't, and assume the [rule] will know how to read the |
| 399 * right length when we read it back. Then we write alternating keys and |
| 400 * values. We expect the values to be references, which we store as |
| 401 * two numbers. |
| 402 */ |
| 403 void writeMaps(SerializationRule rule, List<Map> entries, List target) { |
| 404 target.add(STORED_AS_MAP); |
| 405 for (var eachEntry in entries) { |
| 406 if (rule.hasVariableLengthEntries) { |
| 407 target.add(eachEntry.length); |
| 408 } |
| 409 eachEntry.forEach((key, value) { |
| 410 writeReference(key, target); |
| 411 writeReference(value, target); |
| 412 }); |
| 413 } |
| 414 } |
| 415 |
| 416 /** |
| 417 * Write [entries], which contains simple objects which we can put directly |
| 418 * into [target]. |
| 419 */ |
| 420 void writeObjects(List entries, List target) { |
| 421 target.add(STORED_AS_PRIMITIVE); |
| 422 for (var each in entries) { |
| 423 if (!isPrimitive(each)) throw new SerializationException("Invalid data"); |
| 424 } |
| 425 target.addAll(entries); |
| 426 } |
| 427 |
| 428 /** |
| 429 * Write [eachRef] to [target]. It will be written as two ints. If [eachRef] |
| 430 * is null it will be written as two nulls. |
| 431 */ |
| 432 void writeReference(Reference eachRef, List target) { |
| 433 // TODO(alanknight): Writing nulls is problematic in a real flat format. |
| 434 if (eachRef == null) { |
| 435 target..add(null)..add(null); |
| 436 } else { |
| 437 eachRef.writeToList(target); |
| 438 } |
| 439 } |
| 440 |
| 441 /** |
| 442 * Read the data from [rawInput] in the context of [r] and return it as a |
| 443 * Map with entries for "roots", "data" and "rules", which the reader knows |
| 444 * how to interpret. We expect [rawInput] to have been generated from this |
| 445 * format. |
| 446 */ |
| 447 Map<String, dynamic> read(List rawInput, Reader r) { |
| 448 // TODO(alanknight): It's annoying to have to pass the reader around so |
| 449 // much, consider having the format be specific to a particular |
| 450 // serialization operation along with the reader and having it as a field. |
| 451 var input = {}; |
| 452 input["rules"] = rawInput[0]; |
| 453 r.readRules(input["rules"]); |
| 454 |
| 455 var flatData = rawInput[1]; |
| 456 var stream = flatData.iterator; |
| 457 var tempData = new List(r.rules.length); |
| 458 for (var eachRule in r.rules) { |
| 459 tempData[eachRule.number] = readRuleDataFrom(stream, eachRule, r); |
| 460 } |
| 461 input["data"] = tempData; |
| 462 |
| 463 var roots = []; |
| 464 var rootsAsInts = rawInput[2].iterator; |
| 465 do { |
| 466 roots.add(nextReferenceFrom(rootsAsInts, r)); |
| 467 } while (rootsAsInts.current != null); |
| 468 |
| 469 input["roots"] = roots; |
| 470 return input; |
| 471 } |
| 472 |
| 473 /** |
| 474 * Read the data for [rule] from [input] and return it. |
| 475 */ |
| 476 readRuleDataFrom(Iterator input, SerializationRule rule, Reader r) { |
| 477 var numberOfEntries = _next(input); |
| 478 var entryType = _next(input); |
| 479 if (entryType == STORED_AS_LIST) { |
| 480 return readLists(input, rule, numberOfEntries, r); |
| 481 } |
| 482 if (entryType == STORED_AS_MAP) { |
| 483 return readMaps(input, rule, numberOfEntries, r); |
| 484 } |
| 485 if (entryType == STORED_AS_PRIMITIVE) { |
| 486 return readPrimitives(input, rule, numberOfEntries); |
| 487 } |
| 488 if (numberOfEntries == 0) { |
| 489 return []; |
| 490 } else { |
| 491 throw new SerializationException("Invalid data in serialization"); |
| 492 } |
| 493 } |
| 494 |
| 495 /** |
| 496 * Read data for [rule] from [input] with [length] number of entries, |
| 497 * creating lists from the results. |
| 498 */ |
| 499 List readLists(Iterator input, SerializationRule rule, int length, Reader r) { |
| 500 var ruleData = []; |
| 501 for (var i = 0; i < length; i++) { |
| 502 var subLength = |
| 503 rule.hasVariableLengthEntries ? _next(input) : rule.dataLength; |
| 504 var subList = []; |
| 505 ruleData.add(subList); |
| 506 for (var j = 0; j < subLength; j++) { |
| 507 subList.add(nextReferenceFrom(input, r)); |
| 508 } |
| 509 } |
| 510 return ruleData; |
| 511 } |
| 512 |
| 513 /** |
| 514 * Read data for [rule] from [input] with [length] number of entries, |
| 515 * creating maps from the results. |
| 516 */ |
| 517 List readMaps(Iterator input, SerializationRule rule, int length, Reader r) { |
| 518 var ruleData = []; |
| 519 for (var i = 0; i < length; i++) { |
| 520 var subLength = |
| 521 rule.hasVariableLengthEntries ? _next(input) : rule.dataLength; |
| 522 var map = new Map(); |
| 523 ruleData.add(map); |
| 524 for (var j = 0; j < subLength; j++) { |
| 525 var key = nextReferenceFrom(input, r); |
| 526 var value = nextReferenceFrom(input, r); |
| 527 map[key] = value; |
| 528 } |
| 529 } |
| 530 return ruleData; |
| 531 } |
| 532 |
| 533 /** |
| 534 * Read data for [rule] from [input] with [length] number of entries, |
| 535 * treating the data as primitives that can be returned directly. |
| 536 */ |
| 537 List readPrimitives(Iterator input, SerializationRule rule, int length) { |
| 538 var ruleData = []; |
| 539 for (var i = 0; i < length; i++) { |
| 540 ruleData.add(_next(input)); |
| 541 } |
| 542 return ruleData; |
| 543 } |
| 544 |
| 545 /** Read the next Reference from the input. */ |
| 546 Reference nextReferenceFrom(Iterator input, Reader r) { |
| 547 var a = _next(input); |
| 548 var b = _next(input); |
| 549 if (a == null) { |
| 550 return null; |
| 551 } else { |
| 552 return new Reference(r, a, b); |
| 553 } |
| 554 } |
| 555 |
| 556 /** Return the next element from the input. */ |
| 557 _next(Iterator input) { |
| 558 input.moveNext(); |
| 559 return input.current; |
| 560 } |
| 561 } |
OLD | NEW |