Skip to content

ParsedJsonDocument<T>

Definition

Namespace: Corvus.Text.Json
Assembly: Corvus.Text.Json.dll
Source: ParsedJsonDocument.cs

Represents the structure of a JSON value in a lightweight, read-only form.

public sealed class ParsedJsonDocument<T> : JsonDocument, IJsonDocument, IDisposable
    where T : struct, IJsonElement<T>

Remarks

This class utilizes resources from pooled memory to minimize the garbage collector (GC) impact in high-usage scenarios. Failure to properly Dispose this object will result in the memory not being returned to the pool, which will cause an increase in GC impact across various parts of the framework.

Inheritance

ObjectJsonDocumentParsedJsonDocument

Implements

IJsonDocument, IDisposable

Properties

Property Type Description
False static T Gets the False instance.
Null static T Gets the null instance.
RootElement T The IJsonElement representing the value of the document.
True static T Gets the True instance.

Methods

Method Description
Dispose()
NumberConstant(byte[]) static Creates a constant number instance that does not require disposal.
Parse static Parse memory as UTF-8 encoded text representing a single JSON value into a ParsedJsonDocument.
ParseAsync(Stream, JsonDocumentOptions, CancellationToken) static Parse a Stream as UTF-8 encoded data representing a single JSON value into a ParsedJsonDocument. The Stream will be read to completion.
ParseValue(ref Utf8JsonReader) static Parses one JSON value (including objects or arrays) from the provided reader.
StringConstant(byte[]) static Creates a constant string instance that does not require disposal.
TryParseValue(ref Utf8JsonReader, ref ParsedJsonDocument<T>) static Attempts to parse one JSON value (including objects or arrays) from the provided reader.
WriteTo(Utf8JsonWriter) Write the document into the provided writer as a JSON value.

Examples

The following example parses a JSON document from a string and accesses typed properties on the root element.

using ParsedJsonDocument<Person> doc =
    ParsedJsonDocument<Person>.Parse(jsonString);
Person person = doc.RootElement;

string familyName = (string)person.FamilyName;

You can also parse from UTF-8 bytes, which avoids the cost of transcoding from UTF-16:

using ParsedJsonDocument<Person> doc =
    ParsedJsonDocument<Person>.Parse(utf8Bytes);

The returned document implements IDisposable and must be disposed to return pooled memory. Always use a using statement or using declaration:

using ParsedJsonDocument<Person> doc =
    ParsedJsonDocument<Person>.Parse(jsonString);
Person person = doc.RootElement;

// Work with person...

// Memory is returned when 'doc' is disposed at the end of the scope

Applies To

Product Versions
.NET 9, 10
.NET Standard 2.0, 2.1

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests.

Open an issue