Skip to content

JsonElement.Mutable

Definition

Namespace: Corvus.Text.Json
Assembly: Corvus.Text.Json.dll
Source: JsonElement.Mutable.cs

public readonly struct JsonElement.Mutable : IMutableJsonElement<JsonElement.Mutable>, IJsonElement<JsonElement.Mutable>, IJsonElement, IFormattable, ISpanFormattable, IUtf8SpanFormattable

Implements

IMutableJsonElement<JsonElement.Mutable>, IJsonElement<JsonElement.Mutable>, IJsonElement, IFormattable, ISpanFormattable, IUtf8SpanFormattable

Properties

Property Type Description
Item[...]
ValueKind JsonValueKind

Methods

Method Description
AddItem
AddItemNull()
AddRange
Apply(ref T)
Clone()
CreateBuilder(JsonWorkspace)
EnsurePropertyMap(ref JsonElement.Mutable) static
EnumerateArray()
EnumerateObject()
Equals
EvaluateSchema(IJsonSchemaResultsCollector)
Freeze()
From(ref T) static
GetArrayLength()
GetBigInteger()
GetBigNumber()
GetBoolean()
GetByte()
GetBytesFromBase64()
GetDateTime()
GetDateTimeOffset()
GetDecimal()
GetDouble()
GetGuid()
GetHalf()
GetHashCode()
GetInt128()
GetInt16()
GetInt32()
GetInt64()
GetLocalDate()
GetOffsetDate()
GetOffsetDateTime()
GetOffsetTime()
GetPeriod()
GetProperty
GetPropertyCount()
GetRawText()
GetSByte()
GetSingle()
GetString()
GetUInt128()
GetUInt16()
GetUInt32()
GetUInt64()
GetUtf16String()
GetUtf8String()
InsertItem
InsertItemNull(int)
InsertRange
Remove(ref JsonElement)
RemoveAt(int)
RemoveProperty
RemoveRange(int, int)
RemoveWhere
Replace(ref JsonElement, ref JsonElement.Source)
SetItem
SetItemNull(int)
SetProperty
SetPropertyNull
ToString
TryFormat
TryGetBigInteger(ref BigInteger)
TryGetBigNumber(ref BigNumber)
TryGetByte(ref byte)
TryGetBytesFromBase64(ref byte[])
TryGetDateTime(ref DateTime)
TryGetDateTimeOffset(ref DateTimeOffset)
TryGetDecimal(ref decimal)
TryGetDouble(ref double)
TryGetGuid(ref Guid)
TryGetHalf(ref Half)
TryGetInt128(ref Int128)
TryGetInt16(ref short)
TryGetInt32(ref int)
TryGetInt64(ref long)
TryGetLocalDate(ref LocalDate)
TryGetOffsetDate(ref OffsetDate)
TryGetOffsetDateTime(ref OffsetDateTime)
TryGetOffsetTime(ref OffsetTime)
TryGetPeriod(ref Period)
TryGetProperty
TryGetSByte(ref sbyte)
TryGetSingle(ref float)
TryGetUInt128(ref UInt128)
TryGetUInt16(ref ushort)
TryGetUInt32(ref uint)
TryGetUInt64(ref ulong)
TryReplaceProperty(ReadOnlySpan<byte>, ref JsonElement.Source, int)
ValueEquals
WriteTo(Utf8JsonWriter)

Operators

Operator Description
Equality
explicit operator JsonElement.Mutable(JsonElement)
implicit operator JsonElement(JsonElement.Mutable)
Inequality

Examples

The following example obtains a mutable element from a builder and modifies properties.

using JsonWorkspace workspace = JsonWorkspace.Create();
using ParsedJsonDocument<Person> doc =
    ParsedJsonDocument<Person>.Parse(personJson);
using var builder = doc.RootElement.CreateBuilder(workspace);

Person.Mutable root = builder.RootElement;
root.SetAge(31);
root.SetEmail("michael@example.com"u8);

Version tracking

The builder tracks a version number, and every Mutable reference records the version at which it was obtained. If the document structure changes after you captured an intermediate reference, using that stale reference throws InvalidOperationException.

The root element is always live — it never needs to be re-obtained:

Person.Mutable root = builder.RootElement;  // always live — cache freely
root.RemoveEmail();              // structural change
root.Address.SetCity("London"u8); // still valid — navigated from root

You can also make multiple modifications to the same child entity without re-obtaining it, because its own version is refreshed on modification:

Person.Mutable root = builder.RootElement;
Person.AddressEntity.Mutable address = root.Address;
address.SetCity("London"u8);
address.SetZipCode("SE3"u8);  // same entity — still valid

However, caching an intermediate reference and using it after a sibling mutation is not permitted:

Person.Mutable root = builder.RootElement;
Person.AddressEntity.Mutable address = root.Address;
address.SetCity("London"u8);       // OK

root.SetAge(32);                   // structural change on a sibling

address.SetZipCode("SE3"u8);      // throws InvalidOperationException — stale reference

Removing properties

Optional properties can be removed from mutable instances:

root.RemoveEmail();

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