Serialization converts an in-memory AST into JSON-compatible data that can be stored on disk or transported over HTTP. Because ASTs contain object references, simply calling JSON.stringify is insufficient: references must first become identifiable values. section 7.0 section 7.2
A practical AST serialization process:
- Assign every AST object a unique string id, generated here with nanoid. section 7.2
- Represent a reference as an object such as { ref: targetObject } while the AST is in memory. section 7.2
- Replace each reference target with its identifier, producing { refId: targetObject.id } instead of a JavaScript object reference. section 7.2
- Recursively transform AST objects, settings, arrays, and reference objects into a cycle-free, JSON-compatible representation. section 7.2
- Pass the transformed result to JSON.stringify or storage code; deserialization later reconstructs the AST and its references. section 7.0 section 7.2
Serializing a reference
An AST object receives an id such as "id1". A second object that refers to it is serialized with { "refId": "id1" }, preserving the relationship without embedding the referenced JavaScript object. section 7.2
Figure 7.2 What serialization and deserialization do, and how they relate to each other
Use the figure to trace how serialization produces a transferable representation and how deserialization reverses that transformation. section 7.0























