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

Direct object methods operate only on the current object. For nested traversal, use MutableJsonPath.

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
Merge(MutableJsonObject, MutableJsonObject, MutableJsonMergeOptions?)MutableJsonObjectNon-destructive merge with options
MergeDestructive(MutableJsonObject, MutableJsonObject)MutableJsonObjectDestructive merge — moves source values
MergeDestructive(MutableJsonObject, MutableJsonObject, MutableJsonMergeOptions?)MutableJsonObjectDestructive merge with options
Clone(MutableJsonNode)MutableJsonNodeDeep clone any node

Both merge methods return the target object for chaining.

MutableJsonMergeOptions ​

PropertyTypeDefaultDescription
PropertyNameCaseInsensitiveboolfalseMatch source property names to existing target property names using StringComparer.OrdinalIgnoreCase. When a match is found, the target property name casing is preserved.

Case-insensitive matching is opt-in and applies recursively to nested object merges. It may allocate additional strings for property-name matching.

MutableJsonPath ​

Static class. Nested object traversal and mutation using explicit path segments.

MethodReturnDescription
GetAtPath(string[], MutableJsonPathOptions?)MutableJsonNode?Traverse nested objects by path segments. Returns null when a segment is missing or traversal hits a non-object
GetAtPath(IReadOnlyList<string>, MutableJsonPathOptions?)MutableJsonNode?Same as above for list-based callers
SetAtPath(string[], MutableJsonNode, MutableJsonPathOptions?)voidSet a nested value. Creates missing intermediate objects
SetAtPath(IReadOnlyList<string>, MutableJsonNode, MutableJsonPathOptions?)voidSame as above for list-based callers
RemoveAtPath(string[], MutableJsonRemovePathOptions?)boolRemove a nested value. Returns true when the leaf property existed and was removed
RemoveAtPath(IReadOnlyList<string>, MutableJsonRemovePathOptions?)boolSame as above for list-based callers

Path methods treat each string as one path segment. This means property names containing . are naturally supported, for example ["server.host", "port"].

MutableJsonPathOptions ​

PropertyTypeDefaultDescription
PropertyNameCaseInsensitiveboolfalseMatch each path segment using StringComparison.OrdinalIgnoreCase. When an existing property is updated, the target property casing is preserved.

MutableJsonRemovePathOptions ​

Inherits from MutableJsonPathOptions.

PropertyTypeDefaultDescription
PruneEmptyAncestorsboolfalseAfter removing the leaf property, recursively remove now-empty parent objects

Current scope:

  • Object segments only
  • No array index syntax
  • SetAtPath throws InvalidOperationException if an intermediate segment already exists but is not an object

Released under the Apache-2.0 License.