liveBook · Manning

Serialization

As explained in Building User-Friendly DSLs

Serialization is the conversion of structured in-memory data into a storable or transferable representation.

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
figure

Use the figure to trace how serialization produces a transferable representation and how deserialization reverses that transformation. section 7.0

How Building User-Friendly DSLs covers it

How deep it goes
Explains recursive transformation and reference replacement, while deferring the reverse reconstruction to deserialization.
Examples
Uses a rental AST, generated identifiers, nested settings, and ref-to-refId transformations.
What it assumes
Assumes JavaScript objects and AST references, while defining serialization through the concrete AST problem.
What you can do after
Build it from scratch by assigning IDs and recursively converting AST references into JSON-compatible data.
How it connects
Connects ASTs, JSON, object references, deserialization, HTTP, and observable state.

Also explained in

Compare how 24 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.