JsonDocumentBuilder<T>
Definition
Namespace: Corvus.Text.Json
Assembly: Corvus.Text.Json.dll
Source: JsonDocumentBuilder.cs
A mutable JSON document builder that provides functionality to construct and modify JSON documents.
public sealed class JsonDocumentBuilder<T> : JsonDocument, IMutableJsonDocument, IWorkspaceManagedDocument, IJsonDocument, IDisposable
where T : struct, IMutableJsonElement<T>
Type Parameters
| Parameter | Description |
|---|---|
T |
The type of mutable JSON element this builder works with. |
Inheritance
Object → JsonDocument → JsonDocumentBuilder
Implements
IMutableJsonDocument, IWorkspaceManagedDocument, IJsonDocument, IDisposable
Properties
| Property | Type | Description |
|---|---|---|
| RootElement | T |
Gets the root element of the JSON document. |
Methods
| Method | Description |
|---|---|
| CreateSnapshot() | Creates a snapshot of this builder's current state. The snapshot can be used to cheaply restore the builder to this state via [Restore](/api/v5/corvus-text-json-jsondocumentbuilder-t.restore.html... |
| Dispose() | |
Parse static |
Parses UTF-8 encoded JSON directly into a mutable document builder, avoiding the intermediate ParsedJsonDocument allocation and tree walk. |
ParseValue(JsonWorkspace, ref Utf8JsonReader) static |
Parses one JSON value (including objects or arrays) from the provided reader directly into a mutable document builder. |
| Restore(JsonDocumentBuilderSnapshot<T>) | Restores this builder to the state captured in snapshot. The existing backing buffers are reused (they can only grow, never shrink), so this is a pure memcpy with no allocations. |
| WriteTo(Utf8JsonWriter) | Write the document into the provided writer as a JSON value. |
Examples
The following example creates a mutable builder from a parsed document, modifies properties, and serializes the result.
using JsonWorkspace workspace = JsonWorkspace.Create();
using ParsedJsonDocument<Person> doc =
ParsedJsonDocument<Person>.Parse(
"""
{
"name": { "familyName": "Oldroyd", "givenName": "Michael" },
"age": 30
}
""");
using var builder = doc.RootElement.CreateBuilder(workspace);
Person.Mutable root = builder.RootElement;
root.SetAge(31);
root.SetEmail("michael@example.com"u8);
Console.WriteLine(root.ToString());
// {"name":{"familyName":"Oldroyd","givenName":"Michael"},"age":31,"email":"michael@example.com"}
If you intend to mutate immediately, you can skip the intermediate ParsedJsonDocument and parse directly into a builder for better performance:
using JsonWorkspace workspace = JsonWorkspace.Create();
using var builder = JsonDocumentBuilder<Person.Mutable>.Parse(
workspace,
"""
{
"name": { "familyName": "Oldroyd", "givenName": "Michael" },
"age": 30
}
""");
Person.Mutable root = builder.RootElement;
root.SetAge(31);
root.SetEmail("michael@example.com"u8);
Console.WriteLine(root.ToString());
// {"name":{"familyName":"Oldroyd","givenName":"Michael"},"age":31,"email":"michael@example.com"}
You can also build a document from scratch using the convenience CreateBuilder() overload with named parameters:
using JsonWorkspace workspace = JsonWorkspace.Create();
using var builder = Person.CreateBuilder(
workspace,
name: Person.PersonNameEntity.Build(
(ref nb) => nb.Create(familyName: "Oldroyd"u8, givenName: "Michael"u8)),
age: 30,
email: "michael@example.com"u8);
Console.WriteLine(builder.RootElement.ToString());
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