Skip to content

JsonPatchDocument

Definition

Namespace: Corvus.Text.Json.Patch
Assembly: Corvus.Text.Json.dll

An RFC 6902 JSON Patch document.

public readonly struct JsonPatchDocument : IJsonElement<JsonPatchDocument>, IJsonElement, IFormattable, ISpanFormattable, IUtf8SpanFormattable

Implements

IJsonElement<JsonPatchDocument>, IJsonElement, IFormattable, ISpanFormattable, IUtf8SpanFormattable

Properties

Property Type Description
DefaultInstance static JsonPatchDocument Gets the default instance.
this[int] JsonPatchDocument.PatchOperation Gets the item at the given index.
Rank static int Gets the rank of the array.
ValueKind JsonValueKind

Methods

Method Description
Build static
Clone() Gets a JsonPatchDocument which can be safely stored beyond the lifetime of the original document.
CreateBuilder static
EnumerateArray() Enumerates the array.
Equals
EvaluateSchema(IJsonSchemaResultsCollector) Evaluate this instance against the JSON Schema for this type.
Freeze() Creates a frozen (immutable) copy of this element if it is backed by a mutable document, or returns this instance if it is already immutable.
From(ref T) static Gets an instance of the JSON value from another element.
GetArrayLength() Gets the array length.
GetHashCode()
ParseValue static Parses one JSON value (including objects or arrays) from the provided span.
ToString
TryFormat
TryParseValue(ref Utf8JsonReader, ref Nullable<JsonPatchDocument>) static Attempts to parse one JSON value (including objects or arrays) from the provided reader.
WriteTo(Utf8JsonWriter)

Operators

Operator Description
Equality Operator ==.
Implicit Converts the instance to a JsonElement.
Inequality Operator !=.

Examples

A JsonPatchDocument represents an RFC 6902 JSON Patch — an array of operations that describe mutations to apply to a JSON document.

Parsing from JSON

using Corvus.Text.Json.Patch;

JsonPatchDocument patch = JsonPatchDocument.ParseValue(
    """
    [
        { "op": "replace", "path": "/name", "value": "Bob" },
        { "op": "add", "path": "/email", "value": "bob@example.com" },
        { "op": "remove", "path": "/temporary" }
    ]
    """u8);

Applying to a mutable document

Use TryValidateAndApplyPatch when the patch comes from an untrusted source — it validates the patch against its JSON Schema before applying:

using Corvus.Text.Json;
using Corvus.Text.Json.Patch;

using var parsedDoc = ParsedJsonDocument<JsonElement>.Parse(json);
using JsonWorkspace workspace = JsonWorkspace.Create();
using var builder = parsedDoc.RootElement.CreateBuilder(workspace);

JsonElement.Mutable root = builder.RootElement;

bool applied = root.TryValidateAndApplyPatch(patch);

Building a patch with PatchBuilder

For locally-constructed patches that don't need validation, use PatchBuilder:

JsonPatchDocument patch = root.BeginPatch(workspace)
    .Replace("/name"u8, "Charlie")
    .Add("/tags/-"u8, "admin")
    .Remove("/temp"u8)
    .GetPatchAndDispose();

root.TryApplyPatch(patch);

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