Skip to content

API Overview

All types are in the Cocoar.Json.Mutable namespace. The library targets .NET 8.0.

Thread Safety

This library is not thread-safe. No type is safe for concurrent reads and writes from multiple threads. If you need to share a MutableJsonObject across threads, use external synchronization (e.g., a lock).

After serialization, the resulting byte[] is an independent copy and can be safely shared across threads.

MutableJsonNode

Abstract base class for all JSON node types.

MemberTypeDescription
KindJsonValueKindThe JSON value kind (Object, Array, String, Number, True, False, Null)
WriteTo(Utf8JsonWriter)voidSerialize this node to a writer
ToDebugString()stringHuman-readable debug representation

MutableJsonObject

Sealed. A JSON object with ordered, named properties.

Constructor

csharp
MutableJsonObject(int indexThreshold = -1)

Creates an empty object. When indexThreshold is -1, uses DefaultIndexThreshold (default: 12).

Static Properties

MemberTypeDescription
DefaultIndexThresholdintProperty count at which a dictionary index is built. Default: 12

Properties

MemberTypeDescription
PropertiesIReadOnlyList<Property>All properties in insertion order
KindJsonValueKindAlways JsonValueKind.Object

Methods

MethodReturnDescription
Get(ReadOnlySpan<byte>)MutableJsonNode?Get by UTF-8 property name
Get(string)MutableJsonNode?Get by string property name
Set(ReadOnlySpan<byte>, MutableJsonNode)voidSet or overwrite by UTF-8 name. Overwrites preserve position
Set(string, MutableJsonNode)voidSet or overwrite by string name. Overwrites preserve position
Remove(ReadOnlySpan<byte>)boolRemove by UTF-8 name. Returns true if found
Remove(string)boolRemove by string name. Returns true if found

Property Struct

MutableJsonObject.Property is a readonly struct:

MemberTypeDescription
NameUtf8ReadOnlyMemory<byte>Property name as UTF-8 bytes
NamestringProperty name as string (decoded on access)
ValueMutableJsonNodeThe property value

MutableJsonArray

Sealed. A JSON array of nodes. Intentionally minimal — supports append and read. For removals or reordering, build a new array.

Properties

MemberTypeDescription
ItemsIReadOnlyList<MutableJsonNode>All items in order
KindJsonValueKindAlways JsonValueKind.Array

Methods

MethodReturnDescription
Add(MutableJsonNode)voidAppend an item
this[int]MutableJsonNodeRead-only indexer

MutableJsonString

Sealed. A JSON string stored as UTF-8 bytes.

Constructors

ConstructorCopies?Description
MutableJsonString(byte[])NoStores the array reference directly — caller retains access to the internal buffer
MutableJsonString(string)YesEncodes to a new UTF-8 byte array

Static Factory Methods

MethodDescription
FromOwned(byte[])Takes ownership of the array — no copy
FromCopy(ReadOnlySpan<byte>)Copies the bytes into a new array

Properties & Methods

MemberTypeDescription
ValueUtf8ReadOnlySpan<byte>The raw UTF-8 content
KindJsonValueKindAlways JsonValueKind.String
Replace(byte[])voidReplace the value in place

MutableJsonNumber

Sealed. A JSON number stored as raw UTF-8 digits.

Constructors

ConstructorDescription
MutableJsonNumber(ReadOnlySpan<byte>)From raw UTF-8 digit bytes
MutableJsonNumber(int)From int
MutableJsonNumber(long)From long
MutableJsonNumber(double)From double

Static Factory Methods

MethodDescription
FromOwned(byte[])Takes ownership — no copy
FromCopy(ReadOnlySpan<byte>)Copies the bytes

Properties

MemberTypeDescription
ValueUtf8ReadOnlySpan<byte>The raw UTF-8 representation
KindJsonValueKindAlways JsonValueKind.Number

MutableJsonBool

Sealed. A JSON boolean.

Constructor

csharp
MutableJsonBool(bool value)

Properties

MemberTypeDescription
KindJsonValueKindJsonValueKind.True or JsonValueKind.False

MutableJsonNull

Sealed. A JSON null. Singleton pattern.

MemberTypeDescription
InstanceMutableJsonNullThe single null instance
KindJsonValueKindAlways JsonValueKind.Null

MutableJsonDocument

Static class. Entry point for parsing and serialization.

Parsing

MethodReturnDescription
Parse(byte[])MutableJsonNodeParse from byte array
Parse(ReadOnlySpan<byte>)MutableJsonNodeParse from span
Parse(ReadOnlyMemory<byte>)MutableJsonNodeParse from memory (provider-friendly)
ParseFromStream(Stream)MutableJsonNodeParse from stream (64 KB initial buffer, grows exponentially)

All Parse methods throw JsonException for malformed input.

Serialization

MethodReturnDescription
ToUtf8Bytes(MutableJsonNode)byte[]Serialize to compact UTF-8 JSON
WriteTo(MutableJsonNode, Stream)voidWrite to stream (compact)
WriteTo(MutableJsonNode, Stream, JsonWriterOptions)voidWrite to stream with options
WriteToAsync(MutableJsonNode, Stream, CancellationToken)TaskAsync write to stream
WriteToAsync(MutableJsonNode, Stream, JsonWriterOptions, CancellationToken)TaskAsync write with options

MutableJsonMerge

Static class. Merging and cloning operations.

MethodReturnDescription
Merge(MutableJsonObject, MutableJsonObject)MutableJsonObjectNon-destructive merge — clones source values
MergeDestructive(MutableJsonObject, MutableJsonObject)MutableJsonObjectDestructive merge — moves source values
Clone(MutableJsonNode)MutableJsonNodeDeep clone any node

Both merge methods return the target object for chaining.

Released under the Apache-2.0 License.