JsonObject

Namespace: SnowBank.Data.Json · class

Implements: JsonValue, IEquatable<JsonValue>, IComparable<JsonValue>, IJsonSerializable, IFormattable, ISliceSerializable, IConvertible, IParsable<JsonValue>, ISpanParsable<JsonValue>, ISpanFormattable, IUtf8SpanFormattable, INumberBase<JsonValue>, IUtf8SpanParsable<JsonValue>, IAdditionOperators<JsonValue, JsonValue, JsonValue>, IAdditiveIdentity<JsonValue, JsonValue>, IDecrementOperators<JsonValue>, IDivisionOperators<JsonValue, JsonValue, JsonValue>, IEqualityOperators<JsonValue, JsonValue, bool>, IIncrementOperators<JsonValue>, IMultiplicativeIdentity<JsonValue, JsonValue>, IMultiplyOperators<JsonValue, JsonValue, JsonValue>, ISubtractionOperators<JsonValue, JsonValue, JsonValue>, IUnaryNegationOperators<JsonValue, JsonValue>, IUnaryPlusOperators<JsonValue, JsonValue>, IComparisonOperators<JsonValue, JsonValue, bool>, IDictionary<string, JsonValue>, ICollection<KeyValuePair<string, JsonValue>>, IEnumerable<KeyValuePair<string, JsonValue>>, IEnumerable, IReadOnlyDictionary<string, JsonValue>, IReadOnlyCollection<KeyValuePair<string, JsonValue>>, IEquatable<JsonObject>

JSON Object with fields

Constructors

JsonObject

JsonObject()

Creates a new JSON object that is empty

JsonObject(int capacity)

Creates a new JSON object that is empty and has the specified capacity

  • capacity — The initial number of elements that the JsonObject can contain.

JsonObject(IEqualityComparer<string> comparer)

Creates a new JSON object that is empty, and uses the specified IEqualityComparer.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default ordinal string comparer.

JsonObject(JsonObject copy)

JsonObject(int capacity, IEqualityComparer<string> comparer)

Properties

Comparer

IEqualityComparer<string> Comparer { get; }

Gets the IEqualityComparer that is used to determine the equality of keys for the object

Count

int Count { get; }

Gets the number of elements in the JsonObject

CustomClassName

string CustomClassName { get; }

Returns the "__class" attribute, if present

EmptyReadOnly

static JsonObject EmptyReadOnly { get; }

Returns an empty, read-only, JSON Object singleton

This instance cannot be modified, and should be used to reduce memory allocations when working with read-only JSON

HasValues

bool HasValues { get; }

Returns true if the object is not empty.

IsDefault

bool IsDefault { get; }

Returns true if the object is empty.

IsReadOnly

bool IsReadOnly { get; }

Returns true if this value is read-only, and cannot be modified, or false if it allows mutations.

Only JSON Objects and Arrays can return false. All other "value types" (string, boolean, numbers, ...) are always immutable, and will always be read-only.

If you need to modify a JSON Object or Array that is read-only, you should first create a copy, by calling either Copy or ToMutable, perform any changes required, and then either Freeze the copy, or call ToReadOnly again.

Item

JsonValue Item { get; set; }

JsonValue Item { get; set; }

JsonValue Item { get; set; }

JsonValue Item { get; set; }

Keys

KeyCollection Keys { get; }

Gets an enumerable collection that contains the keys in the JsonObject.

Type

JsonType Type { get; }

Type of JSON value (Object, Array, String, ...)

Values

ValueCollection Values { get; }

Gets an enumerable collection that contains the values in the JsonObject.

Methods

Add

void Add(KeyValuePair<string, JsonValue> item)

Adds an element to this JsonObject

void Add((string, JsonValue) item)

Adds an element to this JsonObject

void Add(string key, JsonValue value)

Adds an element to this JsonObject

void Add(ReadOnlySpan<char> key, JsonValue value)

Adds an element to this JsonObject

void Add(ReadOnlyMemory<char> key, JsonValue value)

Adds an element to this JsonObject

AddIfNonZero

void AddIfNonZero(string key, int value)

Adds a number to this JsonObject, only if its value is not 0

The method is a no-op if is

void AddIfNonZero(string key, int? value)

Adds a number to this JsonObject, only if its value is not null or 0

The method is a no-op if is or

void AddIfNonZero(string key, long value)

Adds a number to this JsonObject, only if its value is not 0

The method is a no-op if is

void AddIfNonZero(string key, long? value)

Adds a number to this JsonObject, only if its value is not null or 0

The method is a no-op if is or

void AddIfNonZero(string key, double value)

Adds a number to this JsonObject, only if its value is not 0

The method is a no-op if is

void AddIfNonZero(string key, double? value)

Adds a number to this JsonObject, only if its value is not null or 0

The method is a no-op if is or

AddIfNotNull

void AddIfNotNull(string key, JsonValue value)

Adds an element to this JsonObject, only if its value is not null or missing

The method is a no-op if is or an instance of

void AddIfNotNull(string key, string value)

Adds an element to this JsonObject, only if its value is not null

The method is a no-op if is

void AddIfNotNull(string key, bool? value)

Adds an element to this JsonObject, only if its value is not null or missing

The method is a no-op if is

void AddIfNotNull(string key, int? value)

Adds a number to this JsonObject, only if its value is not null

The method is a no-op if is

void AddIfNotNull(string key, long? value)

Adds a number to this JsonObject, only if its value is not null

The method is a no-op if is

void AddIfNotNull(string key, double? value)

Adds a number to this JsonObject, only if its value is not null

The method is a no-op if is

void AddIfNotNull(ReadOnlySpan<char> key, JsonValue value)

Adds an element to this JsonObject

void AddIfNotNull(ReadOnlyMemory<char> key, JsonValue value)

Adds an element to this JsonObject

AddIfNotNullOrEmpty

void AddIfNotNullOrEmpty(string key, string value)

Adds an element to this JsonObject, only if its value is not null or empty

The method is a no-op if is or the empty string

AddIfTrue

void AddIfTrue(string key, bool value)

Adds an element to this JsonObject, only if its value is true

The method is a no-op if is

void AddIfTrue(string key, bool? value)

Adds an element to this JsonObject, only if its value is true

The method is a no-op if is or

void AddIfTrue(string key, bool condition, JsonValue value)

Adds an element to this JsonObject, only if its value is true

The method is a no-op if is

AddRange

JsonObject AddRange(ReadOnlySpan<KeyValuePair<string, JsonValue>> items)

Adds several elements to this JsonObject

JsonObject AddRange(ReadOnlySpan<(string, JsonValue)> items)

Adds several elements to this JsonObject

JsonObject AddRange(KeyValuePair<string, JsonValue>[] items)

Adds several elements to this JsonObject

JsonObject AddRange((string, JsonValue)[] items)

Adds several elements to this JsonObject

JsonObject AddRange(IDictionary<string, JsonValue> items)

Adds the content of an existing dictionary to this JsonObject

JsonObject AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items)

Adds several elements to this JsonObject

JsonObject AddRange(IEnumerable<(string, JsonValue)> items)

Adds several elements to this JsonObject

AddRangeReadOnly

JsonObject AddRangeReadOnly(ReadOnlySpan<KeyValuePair<string, JsonValue>> items)

Adds several read-only elements to this JsonObject

JsonObject AddRangeReadOnly(ReadOnlySpan<(string, JsonValue)> items)

Adds several read-only elements to this JsonObject

JsonObject AddRangeReadOnly(KeyValuePair<string, JsonValue>[] items)

Adds several read-only elements to this JsonObject

JsonObject AddRangeReadOnly((string, JsonValue)[] items)

Adds several read-only elements to this JsonObject

JsonObject AddRangeReadOnly(IDictionary<string, JsonValue> items)

Adds several read-only elements to this JsonObject

  • items — Set of elements to add. If some values are mutable, a read-only copy will be added instead.

Returns: Same instance (for chaining)

Fields that already exist will be overwritten.

For performance reasons, added JSON Objects or Arrays should already be read-only, otherwise a deep-copy will be performed.

JsonObject AddRangeReadOnly(IEnumerable<KeyValuePair<string, JsonValue>> items)

Adds several read-only elements to this JsonObject

JsonObject AddRangeReadOnly(IEnumerable<(string, JsonValue)> items)

Adds several read-only elements to this JsonObject

AddValues

JsonObject AddValues<TValue>(ReadOnlySpan<KeyValuePair<string, TValue>> items, Func<TValue, JsonValue> valueSelector)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(KeyValuePair<string, TValue>[] items, Func<TValue, JsonValue> valueSelector)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(IDictionary<string, TValue> items, Func<TValue, JsonValue> valueSelector)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(IEnumerable<KeyValuePair<string, TValue>> items, Func<TValue, JsonValue> valueSelector)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(ReadOnlySpan<(string, TValue)> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(ReadOnlySpan<KeyValuePair<string, TValue>> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(KeyValuePair<string, TValue>[] items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(IDictionary<string, TValue> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(IEnumerable<KeyValuePair<string, TValue>> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(ReadOnlySpan<KeyValuePair<string, TValue>> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(KeyValuePair<string, TValue>[] items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(IDictionary<string, TValue> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

JsonObject AddValues<TValue>(IEnumerable<KeyValuePair<string, TValue>> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several elements to this JsonObject

AddValuesReadOnly

JsonObject AddValuesReadOnly<TValue>(ReadOnlySpan<KeyValuePair<string, TValue>> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(KeyValuePair<string, TValue>[] items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(Dictionary<string, TValue> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(List<KeyValuePair<string, TValue>> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(IEnumerable<KeyValuePair<string, TValue>> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(ReadOnlySpan<KeyValuePair<string, TValue>> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(KeyValuePair<string, TValue>[] items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(Dictionary<string, TValue> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(List<KeyValuePair<string, TValue>> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

JsonObject AddValuesReadOnly<TValue>(IEnumerable<KeyValuePair<string, TValue>> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Adds several read-only elements to this JsonObject

ApplyPatch

JsonObject ApplyPatch(JsonObject patch, bool deepCopy = false)

Apply a patch to the object (in place)

  • patch — Object that will be copied to the parent.

Returns: The same object instance

Bind

TValue Bind<TValue>(TValue defaultValue = null, ICrystalJsonTypeResolver resolver = null)

Bind this value into an instance of type TValue

  • defaultValue — Default value to return if the current instance is null or missing
  • resolver — Optional custom resolver used to bind the value into a managed type.

Returns: An instance of the type TValue that is equivalent to the original JSON value, if there exists a valid conversion path. Otherwise, an exception will be thrown.

JsonNumber.Return(123).Bind() will return the value 123.

object Bind(Type type, ICrystalJsonTypeResolver resolver = null)

Bind this value into an instance of the specified type

  • type — Target managed type
  • resolver — Optional custom resolver used to bind the value into a managed type.

Returns: An instance of the target type that is equivalent to the original JSON value, if there exists a valid conversion path or convention. Otherwise, an exception will be thrown.

If the target type is a Value Type, the instance will be boxed, which may cause extra memory allocations. Consider calling instance, or use any of the convenience methods like , , ...

JsonNumber.Return(123).Bind(typeof(long)) will return a boxed Int64 with value 123.

Clear

void Clear()

Removes all the element of this object

CompareTo

int CompareTo(JsonValue other)

Compares two JSON values, and returns an integer that indicates whether the first value precedes, follows, or occurs in the same position in the sort order as the second value.

ComputePatch

JsonObject ComputePatch(JsonObject after, bool deepCopy = false, bool readOnly = false)

ContainsKey

bool ContainsKey(string key)

Determines whether this JsonObject contains an element with the given key.

  • key — Name of the key

Returns: Returns true if the entry is present; otherwise, false

Please note that this will return even if the property value is . To treat the same as missing, please use instead.

// Cases that return 'true':
new JsonObject { ["Foo"] = "hello" }.Has("Foo") => true  // 'Foo' exists
new JsonObject { ["Foo"] = ""      }.Has("Foo") => true  // 'Foo' is the empty string, which is not 'null'
new JsonObject { ["Foo"] = false   }.Has("Foo") => true  // 'Foo' is false, which is not 'null'
new JsonObject { ["Foo"] = null    }.Has("Foo") => true  // 'Foo' is an explicit 'null', ***: behavior different from Has(key)
// Cases that return 'false':
new JsonObject { ["Bar"] = "world" }.Has("Foo") => false // 'Foo' not found

bool ContainsKey(ReadOnlySpan<char> key)

Determines whether this JsonObject contains an element with the given key.

  • key — Name of the key

Returns: Returns true if the entry is present; otherwise, false

bool ContainsKey(ReadOnlyMemory<char> key)

Determines whether this JsonObject contains an element with the given key.

  • key — Name of the key

Returns: Returns true if the entry is present; otherwise, false

Copy

JsonObject Copy()

Returns a new mutable copy of this JSON array (and all of its children)

Returns: A deep copy of this array and its children.

This will recursively copy all JSON objects or arrays present in the array, even if they are already mutable.

The new instance can be freely modified without any effect on its parent. Likewise, if the parent is modified, it will not have any effect on the copy.

static JsonObject Copy(JsonObject obj, bool deep = false, bool readOnly = false)

Creates a copy of a JSON object

  • obj — Object to copy
  • deep — If true, recursively copy the children as well. If false, perform a shallow copy that reuse the same children.
  • readOnly — If true, the copy will become read-only. If false, the copy will be writable.

Returns: Copy of the object, and optionally of its children (if deep is true

Performing a deep copy will protect against any change, but will induce a lot of memory allocations. For example, any child array will be cloned even if they will not be modified later on.

CopyAndAdd

JsonObject CopyAndAdd(string key, JsonValue value)

Returns a new read-only copy of this object with an additional item

  • key — Name of the field to add. If a field with the same name already exists, an exception will be thrown.
  • value — Value of the new item

Returns: A new instance with the same content of the original object, plus the additional item

If a field with the same name already exists, an exception will be thrown.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

JsonObject CopyAndAdd(ReadOnlySpan<char> key, JsonValue value)

Returns a new read-only copy of this object with an additional item

  • key — Name of the field to add. If a field with the same name already exists, an exception will be thrown.
  • value — Value of the new item

Returns: A new instance with the same content of the original object, plus the additional item

If a field with the same name already exists, an exception will be thrown.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

JsonObject CopyAndAdd(ReadOnlyMemory<char> key, JsonValue value)

Returns a new read-only copy of this object with an additional item

  • key — Name of the field to add. If a field with the same name already exists, an exception will be thrown.
  • value — Value of the new item

Returns: A new instance with the same content of the original object, plus the additional item

If a field with the same name already exists, an exception will be thrown.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

static JsonObject CopyAndAdd(ref JsonObject original, string key, JsonValue value)

Replaces a published JSON Object with a new version with an added field, in a thread-safe manner, using a SpinWait if necessary.

  • original — Reference to the currently published JSON Object
  • key — Name of the field to add. If a field with the same name already exists, an exception will be thrown.
  • value — Value of the field to add

Returns: New published JSON Object, that includes the new field.

This method will attempt to atomically replace the original JSON Object with a new version, unless another thread was able to update it faster, in which case it will simply retry with the newest version, until it is able to successfully update the reference.

Caution: the order of operation between threads is not guaranteed, and this method may loop infinitely if it is perpetually blocked by another, faster, thread !

CopyAndRemove

JsonObject CopyAndRemove(string key)

Returns a new read-only copy of this object without the specified item

  • key — Name of the field to remove from the copy

Returns: A new instance with the same content of the original object, but with the specified item removed.

If the object was not read-only, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already read-only objects.

JsonObject CopyAndRemove(ReadOnlySpan<char> key)

Returns a new read-only copy of this object without the specified item

  • key — Name of the field to remove from the copy

Returns: A new instance with the same content of the original object, but with the specified item removed.

If the object was not read-only, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already read-only objects.

JsonObject CopyAndRemove(ReadOnlyMemory<char> key)

Returns a new read-only copy of this object without the specified item

  • key — Name of the field to remove from the copy

Returns: A new instance with the same content of the original object, but with the specified item removed.

If the object was not read-only, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already read-only objects.

JsonObject CopyAndRemove(JsonPath path)

Returns a new read-only copy of this object without the specified item

  • path — Path of the field to remove from the copy

Returns: A new instance with the same content of the original object, but with the specified item removed.

If the object was not read-only, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already read-only objects.

JsonObject CopyAndRemove(string key, out JsonValue previous)

Returns a new read-only copy of this object without the specified item

  • key — Name of the field to remove from the copy

Returns: A new instance with the same content of the original object, but with the specified item removed.

If the object was not read-only, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already read-only objects.

JsonObject CopyAndRemove(ReadOnlySpan<char> key, out JsonValue previous)

Returns a new read-only copy of this object without the specified item

  • key — Name of the field to remove from the copy

Returns: A new instance with the same content of the original object, but with the specified item removed.

If the object was not read-only, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already read-only objects.

JsonObject CopyAndRemove(ReadOnlyMemory<char> key, out JsonValue previous)

Returns a new read-only copy of this object without the specified item

  • key — Name of the field to remove from the copy

Returns: A new instance with the same content of the original object, but with the specified item removed.

If the object was not read-only, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already read-only objects.

static JsonObject CopyAndRemove(ref JsonObject original, string key)

Replaces a published JSON Object with a new version without the specified field, in a thread-safe manner, using a SpinWait if necessary.

  • original — Reference to the currently published JSON Object
  • key — Name of the field to remove. If the field was not present, the object will not be changed.

Returns: New published JSON Object without the field, or the original object if the was not present.

This method will attempt to atomically replace the original JSON Object with a new version, unless another thread was able to update it faster, in which case it will simply retry with the newest version, until it is able to successfully update the reference.

Caution: the order of operation between threads is not guaranteed, and this method may loop infinitely if it is perpetually blocked by another, faster, thread !

CopyAndSet

JsonObject CopyAndSet(string key, JsonValue value)

Returns a new read-only copy of this object, with an additional field

  • key — Name of the field to set. If a field with the same name already exists, its previous value will be overwritten.
  • value — Value of the new field

Returns: A new instance with the same content of the original object, plus the additional item

If a field with the same name already exists, its value will be overwritten.

If value is Missing, the field will be removed.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

JsonObject CopyAndSet(ReadOnlySpan<char> key, JsonValue value)

Returns a new read-only copy of this object, with an additional field

  • key — Name of the field to set. If a field with the same name already exists, its previous value will be overwritten.
  • value — Value of the new field

Returns: A new instance with the same content of the original object, plus the additional item

If a field with the same name already exists, its value will be overwritten.

If value is Missing, the field will be removed.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

JsonObject CopyAndSet(ReadOnlyMemory<char> key, JsonValue value)

Returns a new read-only copy of this object, with an additional field

  • key — Name of the field to set. If a field with the same name already exists, its previous value will be overwritten.
  • value — Value of the new field

Returns: A new instance with the same content of the original object, plus the additional item

If a field with the same name already exists, its value will be overwritten.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

JsonObject CopyAndSet(JsonPath path, JsonValue value)

Returns a new read-only copy of this object, with an additional field

  • path — Path to the field
  • value — Value of the new field

Returns: A new instance with the same content of the original object, plus the additional field

If a field with the same name already exists, its value will be overwritten.

If value is Missing, the field will be removed.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

static JsonObject CopyAndSet(ref JsonObject original, string key, JsonValue value)

Replaces a published JSON Object with a new version with an added field, in a thread-safe manner, using a SpinWait if necessary.

  • original — Reference to the currently published JSON Object
  • key — Name of the field to set. If a field with the same name already exists, its previous value will be overwritten.
  • value — Value of the field.

Returns: New published JSON Object, that includes the new field.

This method will attempt to atomically replace the original JSON Object with a new version, unless another thread was able to update it faster, in which case it will simply retry with the newest version, until it is able to successfully update the reference.

Caution: the order of operation between threads is not guaranteed, and this method may loop infinitely if it is perpetually blocked by another, faster, thread !

JsonObject CopyAndSet(string key, JsonValue value, out JsonValue previous)

Returns a new read-only copy of this object, with an additional field

  • key — Name of the new field
  • value — Value of the new field
  • previous — If the field was already present, receives its previous value. If not, receives null.

Returns: A new instance with the same content of the original object, plus the additional item

If a field with the same name already exists, its value will be overwritten and the previous value will be stored in previous.

If value is Missing, the field will be removed.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

CopyTo

void CopyTo(KeyValuePair<string, JsonValue>[] array)

void CopyTo(Span<KeyValuePair<string, JsonValue>> array)

void CopyTo(Span<(string, JsonValue)> array)

void CopyTo(KeyValuePair<string, JsonValue>[] array, int arrayIndex)

Create

static JsonObject Create()

Creates a new empty JSON object

Returns: JSON object of size 0, that can be modified.

static JsonObject Create(IEqualityComparer<string> comparer)

Creates a new empty JSON object

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.

Returns: JSON object of size 0, that can be modified.

static JsonObject Create((string, JsonValue) item)

Creates a new JSON object with a single field

  • item — Name and value of the field

Returns: JSON object of size 1, that can be modified.

static JsonObject Create(IDictionary<string, JsonValue> items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(ReadOnlySpan<KeyValuePair<string, JsonValue>> items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(ReadOnlySpan<(string, JsonValue)> items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(KeyValuePair<string, JsonValue>[] items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create((string, JsonValue)[] items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(IEnumerable<KeyValuePair<string, JsonValue>> items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(IEnumerable<(string, JsonValue)> items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(string key0, JsonValue value0)

Creates a new JSON object with a single field

  • key0 — Name of the field
  • value0 — Value of the field

Returns: JSON object of size 1, that can be modified.

static JsonObject Create((string, JsonValue) item1, (string, JsonValue) item2)

Creates a new JSON object with 2 fields

  • item1 — Name and value of the first field
  • item2 — Name and value of the second field

Returns: JSON object of size 2, that can be modified.

static JsonObject Create(IEqualityComparer<string> comparer, ReadOnlySpan<KeyValuePair<string, JsonValue>> items)

Creates a new JSON object with the specified items

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify items (and vice versa), but any change to a mutable children will be reflected in both.

This overload is intended for creating a JsonObject with case-insensitive keys via collection expressions using the following syntax: var obj = (JsonObject) [ with(StringComparer.OrdinalIgnoreCase), "hElLo": 123, "WorLd": 456, /.../ ]; Console.WriteLine(obj["hello"]); // => 123 Console.WriteLine(obj["WORLD"]); // => 456

static JsonObject Create(bool readOnly, ReadOnlySpan<KeyValuePair<string, JsonValue>> items)

Creates a new JSON object from the specified items, that will be either read-only or mutable.

  • readOnly — If true, creates a read-only JsonObject that cannot be modified.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items.

If readOnly is true, any JsonValue in items will replaced by a read-only equivalent, if they are mutable.

This overload is intended for creating a read-only JsonObject using collection expressions via the following syntax: // create a new read-only object JsonObject immutable = [ with(readOnly: true), "hello": 123, "world": 456, /.../ ]; // object cannot be modified immutable["hello"] = "there"; // => throws InvalidOperationException

static JsonObject Create(IEqualityComparer<string> comparer, ReadOnlySpan<(string, JsonValue)> items)

Creates a new JSON object with the specified items

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(IEqualityComparer<string> comparer, KeyValuePair<string, JsonValue>[] items)

Creates a new JSON object with the specified items

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(bool readOnly, KeyValuePair<string, JsonValue>[] items)

Creates a new JSON object from the specified items, that will be either read-only or mutable.

  • readOnly — If true, creates a read-only JsonObject that cannot be modified.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items.

If readOnly is true, any JsonValue in items will replaced by a read-only equivalent, if they are mutable.

static JsonObject Create(IEqualityComparer<string> comparer, (string, JsonValue)[] items)

Creates a new JSON object with the specified items

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(bool readOnly, IEnumerable<KeyValuePair<string, JsonValue>> items)

Creates a new JSON object from the specified items, that will be either read-only or mutable.

  • readOnly — If true, creates a read-only JsonObject that cannot be modified.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items.

If readOnly is true, any JsonValue in items will replaced by a read-only equivalent, if they are mutable.

static JsonObject Create(IEqualityComparer<string> comparer, IEnumerable<KeyValuePair<string, JsonValue>> items)

Creates a new JSON object with the specified items

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create(IEqualityComparer<string> comparer, IEnumerable<(string, JsonValue)> items)

Creates a new JSON object with the specified items

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject Create((string, JsonValue) item1, (string, JsonValue) item2, (string, JsonValue) item3)

Creates a new JSON object with 3 fields

  • item1 — Name and value of the first field
  • item2 — Name and value of the second field
  • item3 — Name and value of the third field

Returns: JSON object of size 3, that can be modified.

static JsonObject Create(string key0, JsonValue value0, string key1, JsonValue value1)

Creates a new JSON object with 2 fields

  • key0 — Name of the first field
  • value0 — Value of the first field
  • key1 — Name of the second field
  • value1 — Value of the second field

Returns: JSON object of size 2, that can be modified.

static JsonObject Create((string, JsonValue) item1, (string, JsonValue) item2, (string, JsonValue) item3, (string, JsonValue) item4)

Creates a new JSON object with 4 fields

  • item1 — Name and value of the first field
  • item2 — Name and value of the second field
  • item3 — Name and value of the third field
  • item4 — Name and value of the fourth field

Returns: JSON object of size 4, that can be modified.

static JsonObject Create(string key0, JsonValue value0, string key1, JsonValue value1, string key2, JsonValue value2)

Creates a new JSON object with 3 fields

  • key0 — Name of the first field
  • value0 — Value of the first field
  • key1 — Name of the second field
  • value1 — Value of the second field
  • key2 — Name of the third field
  • value2 — Value of the third field

Returns: JSON object of size 3, that can be modified.

static JsonObject Create(string key0, JsonValue value0, string key1, JsonValue value1, string key2, JsonValue value2, string key3, JsonValue value3)

Creates a new JSON object with 4 fields

  • key0 — Name of the first field
  • value0 — Value of the first field
  • key1 — Name of the second field
  • value1 — Value of the second field
  • key2 — Name of the third field
  • value2 — Value of the third field
  • key3 — Name of the fourth field
  • value3 — Value of the fourth field

Returns: JSON object of size 4, that can be modified.

CreateBoxed

static JsonObject CreateBoxed(IDictionary<string, object> members)

Converts an untyped dictionary into a JSON Object

Returns: Corresponding mutable JSON Object

This should only be used to interface with legacy APIs that generate a Dictionary<string, object>.

static JsonObject CreateBoxed(IEqualityComparer<string> comparer, IDictionary<string, object> members)

Converts an untyped dictionary into a JSON Object

Returns: Corresponding mutable JSON Object

This should only be used to interface with legacy APIs that generate a Dictionary<string, object>.

CreatePicker

static Func<JsonObject, JsonObject> CreatePicker(ReadOnlySpan<string> fields, bool removeFromSource = false)

Generates a cacheable Picker that can extract a list of fields from a JSON Object

static Func<JsonObject, JsonObject> CreatePicker(IEnumerable<string> fields, bool keepMissing, bool removeFromSource = false)

Generates a cacheable Picker that can extract a list of fields from a JSON Object

static Func<JsonObject, JsonObject> CreatePicker(IDictionary<string, JsonValue> defaults, bool removeFromSource = false, bool keepMutable = false)

Generates a cacheable Picker that can extract a list of fields from a JSON Object

EnsureCapacity

int EnsureCapacity(int capacity)

Ensures that the dictionary can hold up to a specified number of entries without any further expansion of its backing storage.

  • capacity — The number of entries.

Returns: The current capacity of the Dictionary.

Equals

bool Equals(JsonValue other)

Tests if these two JSON values are considered equal, using the relaxed JSON comparison semantics

Two values are considered "equal" if they would produce the same result when serialized using a canonical representation.

Numbers are equal if they represent the "same" number (ie: 1 and 1.0 are equal)

Numbers and strings can be equal, if the later can be parsed into a number equal to the former (ie: 123 and "123" are equal)

Arrays are equal if they have the same length and all their elements are equals pairwise

Objects are equal if they have the same key/value pairs, irrespective of their order.

A Missing field is not considered equal to an explicit Null field, and vice versa

If you require strict equality (ie: "123" and 123 are different), consider calling StrictEquals instead.

bool Equals(JsonObject other)

bool Equals(JsonObject other, IEqualityComparer<JsonValue> comparer)

Tests if two objects are considered equal, using a custom equality comparer

Erase

JsonObject Erase(string fieldToRemove)

Removes a field from this object

  • fieldToRemove — Name of the field to remove

Returns: The same object, but with the field removed (if it was present)

This method is identical to , be can be chained with another call

Freeze

JsonObject Freeze()

Freezes this object, once it has been initialized, by switching it to read-only mode.

Once "frozen", the operation cannot be reverted, and if additional mutation is required, a new copy of the object must be used.

FromObject

static JsonObject FromObject<TValue>(TValue value, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Converts an instance of type TValue into the equivalent JSON Object.

  • value — Instance to convert.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON Object, or null if value is null

The JSON Object that is returned is mutable, and cannot safely be cached or shared. If you need an immutable instance, consider calling instead.

FromValues

static JsonObject FromValues<TValue>(ReadOnlySpan<(string, TValue)> items)

Creates a new JSON object with the specified items

  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject FromValues<TValue>(IEqualityComparer<string> comparer, ReadOnlySpan<(string, TValue)> items)

Creates a new JSON object with the specified items

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Map of key/values to copy

Returns: New JSON object with the same elements in items

Adding or removing items in this new object will not modify (and vice versa), but any change to a mutable children will be reflected in both.

static JsonObject FromValues<TValue>(IDictionary<string, TValue> members, Func<TValue, JsonValue> valueSelector)

Creates a JSON Object from an existing dictionary, using a custom JSON converter.

  • members — Dictionary that must be converted.
  • valueSelector — Handler that is called for each value of the dictionary, and must return the converted JSON value.

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TValue>(IDictionary<string, TValue> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of key/value pairs.

  • items — Sequence of key/value pairs that will become the fields of the new JSON Object. There must not be any duplicate key, or an exception will be thrown.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TValue>(IEnumerable<KeyValuePair<string, TValue>> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of key/value pairs.

  • items — Sequence of key/value pairs that will become the fields of the new JSON Object. There must not be any duplicate key, or an exception will be thrown.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TValue>(IEqualityComparer<string> comparer, IEnumerable<KeyValuePair<string, TValue>> items, Func<TValue, JsonValue> valueSelector)

Creates a JSON Object from a sequence of key/value pairs.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Sequence of key/value pairs that will become the fields of the new JSON Object. There must not be any duplicate key, or an exception will be thrown.
  • valueSelector — Handler that is called for each value of the dictionary, and must return the converted JSON value.

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TValue>(IEqualityComparer<string> comparer, IDictionary<string, TValue> members, Func<TValue, JsonValue> valueSelector)

Creates a JSON Object from an existing dictionary, using a custom JSON converter.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • members — Dictionary that must be converted.
  • valueSelector — Handler that is called for each value of the dictionary, and must return the converted JSON value.

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TElement>(IEnumerable<TElement> source, Func<TElement, string> keySelector, Func<TElement, JsonValue> valueSelector)

Creates a JSON Object from a sequence of elements, using a custom key and value selector.

  • source — Sequence of elements to convert
  • keySelector — Handler that is called for each element of the sequence, and should return the corresponding unique key.
  • valueSelector — Handler that is called for each element of the sequence, and should return the corresponding JSON value.

Returns: Corresponding JSON object, that can be modified

static JsonObject FromValues<TValue>(IEqualityComparer<string> comparer, IDictionary<string, TValue> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of key/value pairs.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Sequence of key/value pairs that will become the fields of the new JSON Object. There must not be any duplicate key, or an exception will be thrown.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TValue>(IEqualityComparer<string> comparer, IEnumerable<KeyValuePair<string, TValue>> items, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of key/value pairs.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Sequence of key/value pairs that will become the fields of the new JSON Object. There must not be any duplicate key, or an exception will be thrown.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TElement>(IEqualityComparer<string> comparer, IEnumerable<TElement> source, Func<TElement, string> keySelector, Func<TElement, JsonValue> valueSelector)

Creates a JSON Object from a sequence of elements, using a custom key and value selector.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • source — Sequence of elements to convert
  • keySelector — Handler that is called for each element of the sequence, and should return the corresponding unique key.
  • valueSelector — Handler that is called for each element of the sequence, and should return the corresponding JSON value.

Returns: Corresponding JSON object, that can be modified

static JsonObject FromValues<TValue>(IEqualityComparer<string> comparer, IEnumerable<KeyValuePair<string, TValue>> items, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of key/value pairs.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • items — Sequence of key/value pairs that will become the fields of the new JSON Object. There must not be any duplicate key, or an exception will be thrown.
  • packer — Helper that can pack instances of TValue
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified.

static JsonObject FromValues<TElement, TValue>(IEnumerable<TElement> source, Func<TElement, string> keySelector, Func<TElement, TValue> valueSelector, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of elements, using a custom key and value selector.

  • source — Sequence of elements to convert
  • keySelector — Handler that is called for each element of the sequence, and should return the corresponding unique key.
  • valueSelector — Handler that is called for each element of the sequence, and should return the corresponding value, that will in turn be converted into JSON.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified

static JsonObject FromValues<TElement, TValue>(IEnumerable<TElement> source, Func<TElement, string> keySelector, Func<TElement, TValue> valueSelector, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of elements, using a custom key and value selector.

  • source — Sequence of elements to convert
  • keySelector — Handler that is called for each element of the sequence, and should return the corresponding unique key.
  • valueSelector — Handler that is called for each element of the sequence, and should return the corresponding value, that will in turn be converted into JSON.
  • packer — Helper that can pack instances of TValue
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified

static JsonObject FromValues<TElement, TValue>(IEqualityComparer<string> comparer, IEnumerable<TElement> source, Func<TElement, string> keySelector, Func<TElement, TValue> valueSelector, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of elements, using a custom key and value selector.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • source — Sequence of elements to convert
  • keySelector — Handler that is called for each element of the sequence, and should return the corresponding unique key.
  • valueSelector — Handler that is called for each element of the sequence, and should return the corresponding value, that will in turn be converted into JSON.
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified

static JsonObject FromValues<TElement, TValue>(IEqualityComparer<string> comparer, IEnumerable<TElement> source, Func<TElement, string> keySelector, Func<TElement, TValue> valueSelector, IJsonPacker<TValue> packer, CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Creates a JSON Object from a sequence of elements, using a custom key and value selector.

  • comparer — The IEqualityComparer implementation to use when comparing keys, or null to use the default EqualityComparer for the type of the key.
  • source — Sequence of elements to convert
  • keySelector — Handler that is called for each element of the sequence, and should return the corresponding unique key.
  • valueSelector — Handler that is called for each element of the sequence, and should return the corresponding value, that will in turn be converted into JSON.
  • packer — Helper that can pack instances of TValue
  • settings — Serialization settings (use default JSON settings if null)
  • resolver — Custom type resolver (use default behavior if null)

Returns: Corresponding JSON object, that can be modified

GetEnumerator

Enumerator GetEnumerator()

Returns an enumerator that iterates through the key/value pairs in the JsonObject.

GetHashCode

int GetHashCode()

Returns a hash code that can be used to quickly identify a JSON value.

The hash code is guaranteed to remain unchanged during the lifetime of the object.

Caution: there is NO guarantee that two equivalent Objects or Arrays will have the same hash code! This means that it is NOT safe to use a JSON object or array has the key of a Dictionary or other collection that uses hash codes to quickly compare two instances.

GetOrCreateArray

JsonArray GetOrCreateArray(string path)

Returns a JSON Array at the given path, or create a new empty array if missing

  • pathpath to the array

Returns: Existing array, or a new empty array.

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

{ }.GetOrCreateArray("foo").Set(0, "bar") => { "foo": [ "bar" ] }
{ }.GetOrCreateArray("foo.bar").Set(0, "baz") => { "foo": { "bar": [ "baz" ] } }

JsonArray GetOrCreateArray(ReadOnlyMemory<char> path)

Returns a JSON Array at the given path, or create a new empty array if missing

  • pathpath to the array

Returns: Existing array, or a new empty array.

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

{ }.GetOrCreateArray("foo").Set(0, "bar") => { "foo": [ "bar" ] }
{ }.GetOrCreateArray("foo.bar").Set(0, "baz") => { "foo": { "bar": [ "baz" ] } }

JsonArray GetOrCreateArray(JsonPath path)

Returns a JSON Array at the given path, or create a new empty array if missing

  • pathpath to the array

Returns: Existing array, or a new empty array.

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

{ }.GetOrCreateArray("foo").Set(0, "bar") => { "foo": [ "bar" ] }
{ }.GetOrCreateArray("foo.bar").Set(0, "baz") => { "foo": { "bar": [ "baz" ] } }

GetOrCreateObject

JsonObject GetOrCreateObject(string path)

Returns a JSON Object at the given path, or create a new empty object if missing

  • pathpath to the object

Returns: Existing object, or a new empty object.

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

{ }.GetOrCreateObject("foo").Set("bar", 123) => { "foo": { "bar": 123 } }
{ }.GetOrCreateObject("foo.bar").Set("baz", 123) => { "foo": { "bar": { "baz": 123 } } }

JsonObject GetOrCreateObject(ReadOnlyMemory<char> path)

Returns a JSON Object at the given path, or create a new empty object if missing

  • pathpath to the object

Returns: Existing object, or a new empty object.

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

{ }.GetOrCreateObject("foo").Set("bar", 123) => { "foo": { "bar": 123 } }
{ }.GetOrCreateObject("foo.bar").Set("baz", 123) => { "foo": { "bar": { "baz": 123 } } }

JsonObject GetOrCreateObject(JsonPath path)

Returns a JSON Object at the given path, or create a new empty object if missing

  • pathpath to the object

Returns: Existing object, or a new empty object.

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

{ }.GetOrCreateObject("foo").Set("bar", 123) => { "foo": { "bar": 123 } }
{ }.GetOrCreateObject("foo.bar").Set("baz", 123) => { "foo": { "bar": { "baz": 123 } } }

GetValue

JsonValue GetValue(string key)

Returns the value of the required field with the specified name.

  • key — Name of the field to retrieve

Returns: The value of the specified field, or an exception if it is null or missing.

JsonValue GetValue(ReadOnlySpan<char> key)

Returns the value of the required field with the specified name.

  • key — Name of the field to retrieve

Returns: The value of the specified field, or an exception if it is null or missing.

JsonValue GetValue(ReadOnlyMemory<char> key)

Returns the value of the required field with the specified name.

  • key — Name of the field to retrieve

Returns: The value of the specified field, or an exception if it is null or missing.

JsonValue GetValue(ReadOnlySpan<char> key, out string actualKey)

Returns the value of the required field with the specified name.

  • key — Name of the field to retrieve
  • actualKey — If the field is present, receives the previously allocated key; otherwise, null.

Returns: The value of the specified field, or an exception if it is null or missing.

JsonValue GetValue(ReadOnlyMemory<char> key, out string actualKey)

Returns the value of the required field with the specified name.

  • key — Name of the field to retrieve
  • actualKey — If the field is present, receives the previously allocated key; otherwise, null.

Returns: The value of the specified field, or an exception if it is null or missing.

GetValueOrDefault

JsonValue GetValueOrDefault(string key, JsonValue missingValue = null)

Returns the value of the optional field with the specified name.

  • key — Name of the field to retrieve

Returns: The value of the specified field, or defaultValue if it is null or missing.

If the value is not a JsonObject or null or missing, an exception will be thrown.

JsonValue GetValueOrDefault(ReadOnlySpan<char> key, JsonValue missingValue = null)

Returns the value of the optional field with the specified name.

  • key — Name of the field to retrieve

Returns: The value of the specified field, or defaultValue if it is null or missing.

If the value is not a JsonObject or null or missing, an exception will be thrown.

JsonValue GetValueOrDefault(ReadOnlyMemory<char> key, JsonValue missingValue = null)

Returns the value of the optional field with the specified name.

  • key — Name of the field to retrieve

Returns: The value of the specified field, or defaultValue if it is null or missing.

If the value is not a JsonObject or null or missing, an exception will be thrown.

JsonValue GetValueOrDefault(ReadOnlySpan<char> key, JsonValue missingValue, out string actualKey)

Returns the value of the optional field with the specified name.

  • key — Name of the field to retrieve
  • actualKey — If the field is present, receives the previously allocated key; otherwise, null.

Returns: The value of the specified field, or defaultValue if it is null or missing.

If the value is not a JsonObject or null or missing, an exception will be thrown.

JsonValue GetValueOrDefault(ReadOnlyMemory<char> key, JsonValue missingValue, out string actualKey)

Returns the value of the optional field with the specified name.

  • key — Name of the field to retrieve
  • actualKey — If the field is present, receives the previously allocated key; otherwise, null.

Returns: The value of the specified field, or defaultValue if it is null or missing.

If the value is not a JsonObject or null or missing, an exception will be thrown.

Has

bool Has(string key)

Determines whether this JsonObject contains an element with the given key and with a non-null value

  • key — Name of the key

Returns: Returns true if the entry is present and not Null or Missing.

Result of obj.Has("Foo"):

  • { "Foo" = "hello" } true, 'Foo' exists
  • { "Foo" = "" } true, 'Foo' is the empty string, which is not 'null'
  • { "Foo" = false } true, 'Foo' is false, which is not 'null'
  • { "Foo" = null } false, 'Foo' is an explicit 'null', ***: behavior different from ContainsKey(key)
  • { "Bar" = "world" } false, 'Foo' not found

JsonSerialize

void JsonSerialize(CrystalJsonWriter writer)

Serializes this instance as JSON

  • writer — Writer that will output the content of this instance

LoadFrom

static JsonObject LoadFrom(string path, CrystalJsonSettings settings = null)

LoadFromAsync

static Task<JsonObject> LoadFromAsync(string path, CancellationToken ct)

static Task<JsonObject> LoadFromAsync(string path, CrystalJsonSettings settings, CancellationToken ct)

Merge

static JsonObject Merge(JsonObject parent, JsonObject other, bool deepCopy = false, bool keepNull = false)

Copy the fields of an object onto the another object

  • parent — Object that will be modified
  • other — Object that will be copied to the parent.
  • deepCopy — If false (default), copy the content of other as-is; otherwise, clone all the elements before merging them.
  • keepNull — If false (default), fields set to null in other will be removed; otherwise, they will be kept as null entries in the merged result.

MergeWith

void MergeWith(JsonObject other, bool deepCopy = false, bool keepNull = false)

Copy the fields of an object onto the current object

  • other — Object that will be merged with the current instance.
  • deepCopy — If false (default), copy the content of other as-is; otherwise, clone all the elements before merging them.
  • keepNull — If false (default), fields set to null in other will be removed; otherwise, they will be kept as null entries in the merged result.

OrderedByKeys

JsonObject OrderedByKeys(IComparer<string> comparer = null)

Returns a copy of this JSON Object, with the same content as the original, but with the keys sorted

static JsonObject OrderedByKeys(JsonObject map, IComparer<string> comparer = null)

Returns a new JSON Object, with the same content as the original, but with the keys sorted

  • map — Original JSON Object. This object will not be modified.
  • comparer — Optional key comparer, or Ordinal if omitted.

Returns: Shallow copy of the object, with the keys sorted using the specified key comparer

Parse

static JsonObject Parse(string jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal that is expected to contain an Object

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonText is empty or not an object, an exception will be thrown instead.

If the JSON document can sometimes be empty of the 'null' token, you should call and then use on the result.

static JsonObject Parse(ReadOnlySpan<char> jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal that is expected to contain an Object

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonText is empty or not an object, an exception will be thrown instead.

If the JSON document can sometimes be empty of the 'null' token, you should call and then use on the result.

static JsonObject Parse(ReadOnlySpan<byte> jsonBytes, CrystalJsonSettings settings = null)

Parses a buffer containing a document that is expected to be an Object

  • jsonBytes — UTF-8 encoded bytes
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonBytes is empty or not an Array, an exception will be thrown

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please use ParseObject instead.

static JsonObject Parse(Slice jsonBytes, CrystalJsonSettings settings = null)

Parses a buffer containing a UTF-8 JSON document, and returns the corresponding expected JSON Object

  • jsonBytes — UTF-8 encoded bytes
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonBytes is empty or not an Array, an exception will be thrown

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please use ParseObject instead.

static JsonObject Parse(string jsonText, IFormatProvider provider)

static JsonObject Parse(ReadOnlySpan<char> jsonText, IFormatProvider provider)

Parses a JSON text literal, and returns the corresponding JSON value.

  • jsonText — JSON text document to parse
  • provider — This parameter is ignored.

Returns: Corresponding JSON value. If jsonText is empty, will return Missing

It is better to call overloads of this method that accept a argument.

ParseArray

static JsonArray ParseArray(string jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal that is expected to contain an Array

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Array. If jsonText is empty or not an Array, an exception will be thrown instead.

The JSON Array is mutable and can be freely modified. If you require an immutable array, please use ParseArray instead.

If the JSON document can sometimes be empty of the 'null' token, you should call Parse and then use AsArrayOrDefault on the result.

static JsonArray ParseArray(ReadOnlySpan<char> jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal that is expected to contain an Array

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Array. If jsonText is empty or not an Array, an exception will be thrown instead.

The JSON Array is mutable and can be freely modified. If you require an immutable array, please use ParseArray instead.

If the JSON document can sometimes be empty of the 'null' token, you should call Parse and then use AsArrayOrDefault on the result.

static JsonArray ParseArray(ReadOnlySpan<byte> jsonBytes, CrystalJsonSettings settings = null)

Parses a buffer containing a document that is expected to be an Array

  • jsonBytes — UTF-8 encoded bytes
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Array. If jsonBytes is empty or not an Array, an exception will be thrown

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please use ParseArray instead.

static JsonArray ParseArray(Slice jsonBytes, CrystalJsonSettings settings = null)

Parses a buffer containing a document that is expected to be an Array

  • jsonBytes — UTF-8 encoded bytes
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Array.

The value may be mutable and can be modified. If you require an immutable thread-safe array, please use ParseArray instead.

ParseObject

static JsonObject ParseObject(string jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal that is expected to contain an Object

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonText is empty or not an object, an exception will be thrown instead.

If the JSON document can sometimes be empty of the 'null' token, you should call and then use on the result.

static JsonObject ParseObject(ReadOnlySpan<char> jsonText, CrystalJsonSettings settings = null)

Parses a JSON text literal that is expected to contain an Object

  • jsonText — JSON text document to parse
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonText is empty or not an object, an exception will be thrown instead.

If the JSON document can sometimes be empty of the 'null' token, you should call and then use on the result.

static JsonObject ParseObject(ReadOnlySpan<byte> jsonBytes, CrystalJsonSettings settings = null)

Parses a buffer containing a document that is expected to be an Object

  • jsonBytes — UTF-8 encoded bytes
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonBytes is empty or not an Array, an exception will be thrown

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please use ParseObject instead.

static JsonObject ParseObject(Slice jsonBytes, CrystalJsonSettings settings = null)

Parses a buffer containing a UTF-8 JSON document, and returns the corresponding expected JSON Object

  • jsonBytes — UTF-8 encoded bytes
  • settings — Serialization settings (use default JSON settings if null)

Returns: Corresponding JSON Object. If jsonBytes is empty or not an Array, an exception will be thrown

The value may be mutable (for objects and arrays) and can be modified. If you require an immutable thread-safe value, please use ParseObject instead.

Pick

JsonObject Pick(ReadOnlySpan<string> fields, bool keepMissing = false, bool keepMutable = false)

Returns a new object that only contains the specified fields of this instance

  • fields — List of the names of the fields to keep, each with a default value if they are missing from the source
  • keepMissing — If false, any field missing from the object will be omitted in the result. If true, they will be present but with a Missing value
  • keepMutable — If false, the created object will be marked as read-only if the source is already read-only; otherwise, it will be mutable.

Returns: New object that only contains the values of the fields specified in fields

JsonObject Pick(string[] fields, bool keepMissing = false, bool keepMutable = false)

Returns a new object that only contains the specified fields of this instance

  • fields — List of the names of the fields to keep, each with a default value if they are missing from the source
  • keepMissing — If false, any field missing from the object will be omitted in the result. If true, they will be present but with a Missing value
  • keepMutable — If false, the created object will be marked as read-only if the source is already read-only; otherwise, it will be mutable.

Returns: New object that only contains the values of the fields specified in fields

JsonObject Pick(IEnumerable<string> fields, bool keepMissing = false, bool keepMutable = false)

Returns a new object that only contains the specified fields of this instance

  • fields — List of the names of the fields to keep, each with a default value if they are missing from the source
  • keepMissing — If false, any field missing from the object will be omitted in the result. If true, they will be present but with a Missing value
  • keepMutable — If false, the created object will be marked as read-only if the source is already read-only; otherwise, it will be mutable.

Returns: New object that only contains the values of the fields specified in fields

PickFrom

JsonObject PickFrom(IDictionary<string, JsonValue> defaults)

Returns a new object containing only specific fields of this object

  • defaults — List of fields to keep, with an optional default value

Returns: New object that contains only the fields specified in defaults

JsonObject PickFrom(object defaults)

Returns a new object containing only specific fields of this object

  • defaults — List of fields to keep, with an optional default value

Returns: New object that contains only the fields specified in defaults

Project

JsonObject Project(ReadOnlySpan<(string, JsonPath, JsonValue)> fields, bool keepMutable = false)

Returns a new object that only contains the specified fields of this instance

  • fields — List of the names of the fields to keep, each with a default value if they are missing from the source
  • keepMutable — If false, the created object will be marked as read-only if the source is already read-only; otherwise, it will be mutable.

Returns: New object that only contains the values of the fields specified in fields

JsonObject Project(ReadOnlySpan<(string, JsonPath)> fields, bool keepMissing = false, bool keepMutable = false)

Returns a new object that only contains the specified fields of this instance

  • fields — List of the names of the fields to keep, each with a default value if they are missing from the source
  • keepMissing — If false, any field missing from the object will be omitted in the result. If true, they will be present but with a Missing value
  • keepMutable — If false, the created object will be marked as read-only if the source is already read-only; otherwise, it will be mutable.

Returns: New object that only contains the values of the fields specified in fields

Remove

bool Remove(string key)

Removes the value with the specified key from this object.

  • key — The key of the element to remove.

Returns: true if the element is successfully found and removed; otherwise, false.

bool Remove(ReadOnlySpan<char> key)

Removes the value with the specified key from this object.

  • key — The key of the element to remove.

Returns: true if the element is successfully found and removed; otherwise, false.

bool Remove(ReadOnlyMemory<char> key)

Removes the value with the specified key from this object.

  • key — The key of the element to remove.

Returns: true if the element is successfully found and removed; otherwise, false.

bool Remove(KeyValuePair<string, JsonValue> keyValuePair)

Removes the value with the specified key from this object, only if its value has not changed.

bool Remove(string key, out JsonValue value)

Removes the value with the specified key from this object, and copies the element to the value parameter.

  • key — The key of the element to remove.
  • value — The removed element.

Returns: true if the element is successfully found and removed; otherwise, false.

bool Remove(ReadOnlySpan<char> key, out JsonValue value)

Removes the value with the specified key from this object, and copies the element to the value parameter.

  • key — The key of the element to remove.
  • value — The removed element.

Returns: true if the element is successfully found and removed; otherwise, false.

bool Remove(ReadOnlyMemory<char> key, out JsonValue value)

Removes the value with the specified key from this object, and copies the element to the value parameter.

  • key — The key of the element to remove.
  • value — The removed element.

Returns: true if the element is successfully found and removed; otherwise, false.

bool Remove(ReadOnlySpan<char> key, out string actualKey, out JsonValue value)

Removes the value with the specified key from this object, and copies the element to the value parameter.

  • key — The key of the element to remove.
  • actualKey — The removed key.
  • value — The removed element.

Returns: true if the element is successfully found and removed; otherwise, false.

bool Remove(ReadOnlyMemory<char> key, out string actualKey, out JsonValue value)

Removes the value with the specified key from this object, and copies the element to the value parameter.

  • key — The key of the element to remove.
  • actualKey — The removed key.
  • value — The removed element.

Returns: true if the element is successfully found and removed; otherwise, false.

RemovePath

bool RemovePath(string path)

Removes the value at the given path

  • pathpath of the value to remove.

Returns: true if the value was found and was removed, or false if it was no present.

{ "foo": { "bar": 123, "baz": 456 } }.RemovePath("foo.bar") => { "foo": { "baz": 456 } }

bool RemovePath(ReadOnlyMemory<char> path)

Removes the value at the given path

  • pathpath of the value to remove.

Returns: true if the value was found and was removed, or false if it was no present.

{ "foo": { "bar": 123, "baz": 456 } }.RemovePath("foo.bar") => { "foo": { "baz": 456 } }

bool RemovePath(JsonPath path)

Removes the value at the given path

  • pathpath of the value to remove.

Returns: true if the value was found and was removed, or false if it was no present.

{ "foo": { "bar": 123, "baz": 456 } }.RemovePath("foo.bar") => { "foo": { "baz": 456 } }

Set

JsonObject Set<TValue>(string key, TValue value)

Sets the value of the field in this object

JsonObject Set<TValue>(ReadOnlySpan<char> key, TValue value)

Sets the value of the field in this object

JsonObject Set<TValue>(ReadOnlyMemory<char> key, TValue value)

Sets the value of the field in this object

JsonObject Set(string key, JsonValue value)

Sets the value of a field in the object

JsonObject Set(ReadOnlySpan<char> key, JsonValue value)

Sets the value of a field in the object

JsonObject Set(ReadOnlyMemory<char> key, JsonValue value)

Sets the value of a field in the object

JsonObject Set(int key, JsonValue value)

Sets the value of a field in the object

JsonObject Set(long key, JsonValue value)

Sets the value of a field in the object

SetPath

void SetPath(string path, JsonValue value)

Sets the value at the given path

  • pathpath of the value to set.
  • value — New value

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

void SetPath(ReadOnlyMemory<char> path, JsonValue value)

Sets the value at the given path

  • pathpath of the value to set.
  • value — New value

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

void SetPath(JsonPath path, JsonValue value)

Sets the value at the given path

  • pathpath of the value to set.
  • value — New value

If any intermediate element in the traversed path is missing, it will be created as required (either as an object or an array)

SetRange

JsonObject SetRange(ReadOnlySpan<KeyValuePair<string, JsonValue>> items)

Sets several elements to this JsonObject

If there are duplicate keys in , the last one wins.

JsonObject SetRange(ReadOnlySpan<(string, JsonValue)> items)

Adds several elements to this JsonObject

If there are duplicate keys in , the last one wins.

JsonObject SetRange(IEnumerable<KeyValuePair<string, JsonValue>> items)

Adds several elements to this JsonObject

JsonObject SetRange(IEnumerable<(string, JsonValue)> items)

Adds several elements to this JsonObject

SetRangeReadOnly

JsonObject SetRangeReadOnly(ReadOnlySpan<KeyValuePair<string, JsonValue>> items)

Adds several read-only elements to this JsonObject

JsonObject SetRangeReadOnly(ReadOnlySpan<(string, JsonValue)> items)

Adds several read-only elements to this JsonObject

JsonObject SetRangeReadOnly(IEnumerable<KeyValuePair<string, JsonValue>> items)

Adds several read-only elements to this JsonObject

JsonObject SetRangeReadOnly(IEnumerable<(string, JsonValue)> items)

Adds several read-only elements to this JsonObject

SortKeys

void SortKeys(IComparer<string> comparer = null)

Order the keys of this object

  • comparer — Optional key comparer, or Ordinal if omitted.

StrictEquals

bool StrictEquals(JsonValue other)

Tests if these two JSON values are considered equal, using the strict JSON comparison semantics

For two values to be considered "equal", they must be of the same type (string, number, array, ...) AND have the same result when serialized using a canonical representation.

Arrays are equals if they have the same length and all their elements are also strictly equal.

Objects are equal if they have the same keys, irrespective of their order, and each value is strictly equal.

A Missing field is not considered equal to an explicit Null field, and vice versa

bool StrictEquals(JsonObject other)

bool StrictEquals(Dictionary<string, JsonValue> other)

ToArray

KeyValuePair<string, JsonValue>[] ToArray()

ToDateTimeOffset

DateTimeOffset ToDateTimeOffset(DateTimeOffset defaultValue = null)

Returns the equivalent DateTimeOffset, if this object has the shape that DataContractJsonSerializer writes for that type

DataContractJsonSerializer has no notion of a DateTimeOffset. It decomposes the struct into an object, { "DateTime": "\/Date(...)\/", "OffsetMinutes": ... }. The inner date is the instant, normalized to UTC, and the offset travels beside it, in minutes. Every other spelling of a date is a JSON string, and JsonString converts those.

The match requires exactly those two members. A looser test would claim application objects that happen to have a DateTime member.

Without this override the value reaches the member binder of the reflection path. That binder finds no settable member on DateTimeOffset, assigns nothing, and returns default with no error.

ToDictionary

Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(ICrystalJsonTypeResolver resolver = null)

ToExpando

ExpandoObject ToExpando()

ToJsonText

string ToJsonText(CrystalJsonSettings settings = null, ICrystalJsonTypeResolver resolver = null)

Serializes this JSON value into a JSON string literal

  • settings — Settings used to change the serialized JSON output (optional)
  • resolver — Optional custom resolver.

Returns: JSON text literal that can be written to disk, returned as the body of an HTTP request, or

var jsonText = new JsonObject() { ["hello"] = "world" }.ToJsonText(); // == "{ "hello": "world" }"

ToMutable

JsonObject ToMutable()

Converts this JSON Object so that it, or any of its children that were previously read-only, can be mutated.

Returns: The same instance if it is already fully mutable, OR a copy where any read-only Object or Array has been converted to allow mutations.

Will return the same instance if it is already mutable, or a new deep copy with all children marked as mutable.

This attempts to only copy what is necessary, and will not copy objects or arrays that are already mutable, or all other "value types" (string, boolean, number, ...) that are always immutable.

ToObject

object ToObject()

Converts this JSON Object into a Dictionary<string, object?>.

ToReadOnly

JsonObject ToReadOnly()

Returns a new immutable read-only version of this JSON object (and all of its children)

Returns: The same object, if it is already immutable; otherwise, a deep copy marked as read-only.

A JSON object that is immutable is truly safe against any modification, including any of its direct or indirect children.

TrimExcess

void TrimExcess()

Sets the capacity of this dictionary to what it would be if it had been originally initialized with all its entries.

void TrimExcess(int capacity)

Sets the capacity of this dictionary to hold up a specified number of entries without any further expansion of its backing storage.

  • capacity — The new capacity.

TryAdd

bool TryAdd(string key, JsonValue value)

Adds an element to this JsonObject, only if it was not present before

bool TryAdd(ReadOnlySpan<char> key, JsonValue value)

Adds an element to this JsonObject, only if it was not present before

bool TryAdd(ReadOnlyMemory<char> key, JsonValue value)

Adds an element to this JsonObject, only if it was not present before

TryCopyAndAdd

bool TryCopyAndAdd(string key, JsonValue value, out JsonObject copy)

Returns a new read-only copy of this object with an additional item

  • key — Name of the field to add. If a field with the same name already exists, the method will return false.
  • value — Value of the new item
  • copy — Receives a new instance with the same content of the original object, plus the additional item

Returns: true if the field was added, or false if there was already a field with the same name.

If the object was not-readonly, existing non-readonly fields will also be converted to read-only.

For best performances, this should only be used on already-readonly objects, and with read-only values.

TryCopyTo

bool TryCopyTo(Span<KeyValuePair<string, JsonValue>> array)

bool TryCopyTo(Span<(string, JsonValue)> array)

TryFormat

bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = null, IFormatProvider provider = null)

bool TryFormat(Span<byte> destination, out int bytesWritten, ReadOnlySpan<char> format = null, IFormatProvider provider = null)

TryGetValue

bool TryGetValue(string key, out JsonValue value)

Gets the value at the specified path, if it exists

  • value — Receives the value found at this location, if it exists

Returns: true if the value was found and was not null or missing; otherwise, false

This method will return if a field is present but is explicitly set to .

bool TryGetValue(ReadOnlySpan<char> key, out JsonValue value)

Returns the value of field with the specified name, if the current is an object and the field was found.

  • key — Name of the field to retrieve
  • value — Value of the field, if it was found

Returns: true if the field was found, or false if the field was not found, or the current value is not an object

bool TryGetValue(ReadOnlyMemory<char> key, out JsonValue value)

Returns the value of field with the specified name, if the current is an object and the field was found.

  • key — Name of the field to retrieve
  • value — Value of the field, if it was found

Returns: true if the field was found, or false if the field was not found, or the current value is not an object

bool TryGetValue(ReadOnlySpan<char> key, out string actualKey, out JsonValue value)

Returns the value of field with the specified name, if the current is an object and the field was found.

  • key — Name of the field to retrieve
  • actualKey — If the field is present, receives the previously allocated key; otherwise, null.
  • value — Value of the field, if it was found

Returns: true if the field was found, or false if the field was not found, or the current value is not an object

bool TryGetValue(ReadOnlyMemory<char> key, out string actualKey, out JsonValue value)

Returns the value of field with the specified name, if the current is an object and the field was found.

  • key — Name of the field to retrieve
  • actualKey — If the field is present, receives the previously allocated key; otherwise, null.
  • value — Value of the field, if it was found

Returns: true if the field was found, or false if the field was not found, or the current value is not an object

TryLoadFromAsync

static Task<JsonObject> TryLoadFromAsync(string path, CrystalJsonSettings settings, CancellationToken ct)

Without

JsonObject Without(Func<string, bool> filter, bool deepCopy = false)

Returns a new object that only includes the fields of the original that passed a given filter

  • filter — Filter that is called for each field of the object. If the filter returns false, for a field, it will not be copied into the result.
  • deepCopy — If true, performs a deep copy of the fields that pass the filter. If false, copy them by reference. Has no effect for fields that are already read-only.

Returns: New object with only the fields that passed the filter.

JsonObject Without(string fieldToRemove, bool deepCopy = false)

Returns a new object, without the specified field

  • fieldToRemove — Name of the field that should be omitted, if present.
  • deepCopy — If true, performs a deep copy of the fields that pass the filter. If false, copy them by reference. Has no effect for fields that are already read-only.

Returns: New object that does not exclude the specified field.

WriteTo

void WriteTo(ref SliceWriter writer)

Serializes the current instance to the specified output buffer