Class SyntaxNodeJson
Shared System.Text.Json configuration for (de)serializing a SyntaxNode tree.
public static class SyntaxNodeJson
- Inheritance
-
SyntaxNodeJson
- Inherited Members
Remarks
The AST crosses the ParseServer-to-LanguageServer process boundary as JSON on every parse
(PlatformJson). Most node types store the same children under two or three differently-named
properties: the generic inherited spine (Children, and
ExpressionNode/StatementNode's own Inputs) and specific typed
properties (e.g. InlineIfStatementNode.ConditionExpression). Left alone,
System.Text.Json serializes all of them, so each nesting level multiplies the
payload rather than adding to it: an ordinary depth-16 nested expression (1 + 1 + ... + 1)
serializes to hundreds of megabytes.
Options installs a DefaultJsonTypeInfoResolver modifier that detects, per
concrete node type, whether the generic spine (Children/Inputs) is exactly
reconstructable from that type's other, non-[JsonIgnore]d node-bearing properties,
and if so, omits the spine from the JSON instead of the reverse. Never the reverse: a typed property
(read directly by consumers like SymbolBuilder, post-deserialize, in a different process)
silently losing data on the wire would be a far worse failure than a bloated payload. The check runs
per instance, not just per type, and falls back to keeping the spine on any shape it can't
prove: false negatives (a payload that could have shrunk further but didn't) are an acceptable
missed optimization; false positives (dropping data that wasn't actually reconstructable) are not.
Fields
MaxNodeDepth
The maximum nesting depth an AST payload may reach, in JSON levels.
public const int MaxNodeDepth = 1024
Field Value
Remarks
A guard against a hostile payload, not a language limit. A syntax tree is written flat, an array of its nodes, so the depth of the code it holds costs it no level: a payload reaches a few levels per node, whatever the depth of its tree.
Properties
HeaderOptions
LocationFreeOptions, less the statements a block contains: a block statement written with these is its header alone - a
For line, an If condition, a With target - and not the statements of its body, nor its ElseIf, Else or
Case blocks.
public static JsonSerializerOptions HeaderOptions { get; }
Property Value
Remarks
For comparing, and so for writing only. An instruction of a lowered body is written with these: the statements of a block are instructions of their own, and an edit of one of them leaves the block's opening instruction as it was.
LocationFreeOptions
Options, less every property that says where something was written: in the source (a SourceLocation, a SourceRange) or in the tree (a node's SyntaxNodeId, its path from the module). Two writings of the same code with these are the same text, however the code moved: a blank line inserted above it, a comment edited, or a declaration added before it leaves it as it was.
public static JsonSerializerOptions LocationFreeOptions { get; }
Property Value
Remarks
For comparing, and so for writing only: what is read with these has no location. The fingerprints of a program image are written with them, and so are those of the declarations a program that waits was suspended with.
Options
The options every AST (de)serialization call site should share: PlatformJson (the live wire path) and tests alike, so what a test exercises matches what actually ships.
public static JsonSerializerOptions Options { get; }
Property Value
Remarks
A syntax tree is written flat: an array of its nodes, each after the nodes it holds, which it holds by their index in the array. Writing and reading
one takes the same stack however deep the tree is (a chain of thousands of additions, hundreds of nested parentheses). The outermost node a payload
holds is the root of a tree, written {"$nodes": [...], "$root": n}; a node held by two properties is written once.