JsonSyntaxTree Class

A parsed JSON document.

public sealed class JsonSyntaxTree : SyntaxTree

Remarks

Parsing never throws and never gives up: whatever the text says, the tree reproduces it exactly, and anything wrong with it is reported through JsonSyntaxTree.GetDiagnostics. Comments and trailing commas are accepted and kept.

Example

var tree = JsonSyntaxTree.ParseText("""{ "name": "value" }""");
var name = ((JsonObjectSyntax)tree.GetRoot().Value!).GetMember("name");
Assert.Equal("value", ((JsonStringSyntax)name!.Value).Value);
Inheritance
SyntaxTree JsonSyntaxTree

Properties

NameDescription
FilePath
public override string? FilePath { get; }

Property value

string?

No documentation is available.

Methods

NameDescription
Create(JsonDocumentSyntax, string?)Creates a tree over root, taking its text from the root itself.
GetChanges(JsonSyntaxTree)Describes how oldTree would have to change to become this one.
GetDiagnostics()Gets every diagnostic in the tree, in source order.
GetRoot()Gets the root of the tree.
GetText()
IsEquivalentTo(JsonSyntaxTree?)Determines whether the two trees have the same structure and text.
ParseText(SourceText, string?)Parses text.
ParseText(string, string?)Parses text.
WithChangedText(SourceText)Returns a tree over newText.
WithChanges(TextChange[])Returns a tree over this text with changes applied.
WithChanges(IEnumerable<TextChange>)Returns a tree over this text with changes applied.
WithRoot(JsonDocumentSyntax)Returns a tree whose root is root.

Create(JsonDocumentSyntax, string?)

JsonSyntaxTree.cs

Creates a tree over root, taking its text from the root itself.

public static JsonSyntaxTree Create(JsonDocumentSyntax root, string? path = null)

Parameters

root JsonDocumentSyntax
path string?

Returns

JsonSyntaxTree

Exceptions

ArgumentNullException
root is null.

GetChanges(JsonSyntaxTree)

JsonSyntaxTree.cs

Describes how oldTree would have to change to become this one.

public IReadOnlyList<TextChange> GetChanges(JsonSyntaxTree oldTree)

Parameters

oldTree JsonSyntaxTree

Returns

IReadOnlyList<TextChange>

Exceptions

ArgumentNullException
oldTree is null.

GetDiagnostics()

JsonSyntaxTree.cs

Gets every diagnostic in the tree, in source order.

public override IReadOnlyList<Diagnostic> GetDiagnostics()

Gets the root of the tree.

public JsonDocumentSyntax GetRoot()
public override SourceText GetText()

Returns

SourceText

No documentation is available.

IsEquivalentTo(JsonSyntaxTree?)

JsonSyntaxTree.cs

Determines whether the two trees have the same structure and text.

public bool IsEquivalentTo(JsonSyntaxTree? other)

Parameters

other JsonSyntaxTree?

Returns

bool

ParseText overloads

ParseText(SourceText, string?)

JsonSyntaxTree.cs

Parses text.

public static JsonSyntaxTree ParseText(SourceText text, string? path = null)
Parameters
text SourceText
path string?
Returns

JsonSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(string, string?)

JsonSyntaxTree.cs

Parses text.

public static JsonSyntaxTree ParseText(string text, string? path = null)
Parameters
text string
The JSON to read.
path string?
Where the text came from, for the diagnostics to refer to.
Returns

JsonSyntaxTree

Exceptions
ArgumentNullException
text is null.

WithChangedText(SourceText)

JsonSyntaxTree.cs

Returns a tree over newText.

public JsonSyntaxTree WithChangedText(SourceText newText)

Parameters

newText SourceText

Returns

JsonSyntaxTree

Exceptions

ArgumentNullException
newText is null.

WithChanges overloads

WithChanges(TextChange[])

JsonSyntaxTree.cs

Returns a tree over this text with changes applied.

public JsonSyntaxTree WithChanges(params TextChange[] changes)
Parameters
changes TextChange[]
Returns

JsonSyntaxTree

Exceptions
ArgumentNullException
changes is null.

WithChanges(IEnumerable<TextChange>)

JsonSyntaxTree.cs

Returns a tree over this text with changes applied.

public JsonSyntaxTree WithChanges(IEnumerable<TextChange> changes)
Parameters
changes IEnumerable<TextChange>
Returns

JsonSyntaxTree

Exceptions
ArgumentNullException
changes is null.

WithRoot(JsonDocumentSyntax)

JsonSyntaxTree.cs

Returns a tree whose root is root.

public JsonSyntaxTree WithRoot(JsonDocumentSyntax root)

Parameters

root JsonDocumentSyntax

Returns

JsonSyntaxTree

Exceptions

ArgumentNullException
root is null.