liveBook · Manning

Deserialization

As explained in Building User-Friendly DSLs

The reconstruction of structured in-memory data from a stored or transferred representation.

Deserialization reconstructs a structured in-memory AST from its serialized JSON form. It makes persisted ASTs available again after storage on a backend or transfer over HTTP, so a frontend can retrieve and use the saved model. section 7.0

For an AST, reconstruction must preserve both contained objects and reference relations. Serialized references use objects such as `{ refId: id }`; deserialization replaces each with `{ ref: astObject }`, where `astObject` is the reconstructed object with that ID. An unselected target remains `{ ref: undefined }`. section 7.3

A practical implementation uses a depth-first traversal with deferred reference resolution:

  • Recognize AST objects, arrays, primitive values, and serialized reference objects separately. section 7.3
  • Create a new AST object for every serialized AST object rather than modifying the serialized input. section 7.3
  • Record each reconstructed object in an ID-to-AST lookup table. section 7.3
  • Collect references while traversing, then resolve their IDs after all AST objects have been created; this supports references whose targets appear later in the structure. section 7.3

Restoring a saved AST

After reading JSON contents from storage, pass the result to `deserialize` and use the returned AST. A serialized Attribute Reference holding `refId: "SlLaELvAy0"` is restored to a JavaScript reference to the reconstructed Data Attribute with that ID. section 7.3

Deserialization can also construct an observable AST directly. Creating observable AST objects and reference wrappers explicitly before linking references avoids MobX recursively converting a circular reference and hitting a maximum call stack error. The resulting reference remains a real observable relation, so dependent projections update when the referenced attribute changes. section 7.4

Figure 7.2 What serialization and deserialization do, and how they relate to each other
figure

Compare the stored representation with the reconstructed AST and see serialization and deserialization as opposite transformations. section 7.0

How Building User-Friendly DSLs covers it

How deep it goes
Works through depth-first reconstruction, lookup tables, deferred reference resolution, and observable handling for circular AST references.
Examples
Uses saved JSON contents, an Attribute Reference to a Data Attribute, and a circular self-reference AST.
What it assumes
Assumes familiarity with AST objects, JSON, JavaScript objects and arrays, then defines serialized references and reference resolution.
What you can do after
Build it from scratch by traversing serialized AST data, indexing objects by ID, and resolving references afterward.
How it connects
Connects ASTs, JSON, serialization, HTTP, reference relations, depth-first tree traversal, and MobX observables.

Also explained in

Compare how 10 books explain deserialization →

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.