Skip to content

JsonElement

Definition

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

Represents a specific JSON value within a JsonDocument.

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

Implements

IJsonElement<JsonElement>, IJsonElement, IFormattable, ISpanFormattable, IUtf8SpanFormattable

Properties

Property Type Description
Item[...] Get the value at a specified index when the current value is a Array.
ValueKind JsonValueKind The JsonValueKind that the value is.

Methods

Method Description
Clone() Get a JsonElement which can be safely stored beyond the lifetime of the original JsonDocument.
CreateArrayBuilder(JsonWorkspace, int, int) static Creates an empty mutable array document builder.
CreateBuilder static
CreateObjectBuilder(JsonWorkspace, int, int) static Creates an empty mutable object document builder.
EnsurePropertyMap() Ensures that a fast-lookup property map is created for this element.
EnumerateArray() Get an enumerator to enumerate the values in the JSON array represented by this JsonElement.
EnumerateDescendantProperties(ReadOnlySpan<byte>) Returns an enumerator that yields the values of all descendant properties whose unescaped name equals utf8PropertyName.
EnumerateObject() Get an enumerator to enumerate the properties in the JSON object represented by this JsonElement.
Equals Determines whether the specified object is equal to the current JsonElement.
EvaluateSchema(IJsonSchemaResultsCollector) Evaluates the JSON Schema for this element.
Freeze() Creates a frozen (immutable) copy of this element if it is backed by a mutable document, or returns this instance if it is already immutable.
From(ref T) static Create an instance of a JsonElement from a IJsonElement.
GetArrayLength() Get the number of values contained within the current array value.
GetBigInteger() Gets the current JSON number as a BigInteger.
GetBigNumber() Gets the current JSON number as a BigNumber.
GetBoolean() Gets the value of the element as a Boolean.
GetByte() Gets the current JSON number as a Byte.
GetBytesFromBase64() Gets the value of the element as bytes.
GetDateTime() Gets the value of the element as a DateTime.
GetDateTimeOffset() Gets the value of the element as a DateTimeOffset.
GetDecimal() Gets the current JSON number as a Decimal.
GetDouble() Gets the current JSON number as a Double.
GetGuid() Gets the value of the element as a Guid.
GetHalf() Gets the current JSON number as a Half.
GetHashCode()
GetInt128() Gets the current JSON number as a Int128.
GetInt16() Gets the current JSON number as an Int16.
GetInt32() Gets the current JSON number as an Int32.
GetInt64() Gets the current JSON number as a Int64.
GetLocalDate() Gets the value of the element as a LocalDate.
GetOffsetDate() Gets the value of the element as a OffsetDate.
GetOffsetDateTime() Gets the value of the element as a OffsetDateTime.
GetOffsetTime() Gets the value of the element as a OffsetTime.
GetPeriod() Gets the value of the element as a Period.
GetProperty Gets a JsonElement representing the value of a required property identified by propertyName.
GetPropertyCount() Get the number of properties contained within the current object value.
GetRawText() Gets the original input data backing this value, returning it as a String.
GetSByte() Gets the current JSON number as an SByte.
GetSingle() Gets the current JSON number as a Single.
GetString() Gets the value of the element as a String.
GetUInt128() Gets the current JSON number as a UInt128.
GetUInt16() Gets the current JSON number as a UInt16.
GetUInt32() Gets the current JSON number as a UInt32.
GetUInt64() Gets the current JSON number as a UInt64.
GetUtf16String() Gets the value of the element as a UnescapedUtf16JsonString.
GetUtf8String() Gets the value of the element as a UnescapedUtf8JsonString.
ParseValue static Parses UTF8-encoded text representing a single JSON value into a JsonElement.
ToString Gets a string representation for the current value appropriate to the value type.
TryFormat
TryGetBigInteger(ref BigInteger) Attempts to represent the current JSON number as a BigInteger.
TryGetBigNumber(ref BigNumber) Attempts to represent the current JSON number as a BigNumber.
TryGetBoolean(ref bool) Tries to get the value as a boolean
TryGetByte(ref byte) Attempts to represent the current JSON number as a Byte.
TryGetBytesFromBase64(ref byte[]) Attempts to represent the current JSON string as bytes assuming it is Base64 encoded.
TryGetDateTime(ref DateTime) Attempts to represent the current JSON string as a DateTime.
TryGetDateTimeOffset(ref DateTimeOffset) Attempts to represent the current JSON string as a DateTimeOffset.
TryGetDecimal(ref decimal) Attempts to represent the current JSON number as a Decimal.
TryGetDouble(ref double) Attempts to represent the current JSON number as a Double.
TryGetGuid(ref Guid) Attempts to represent the current JSON string as a Guid.
TryGetHalf(ref Half) Attempts to represent the current JSON number as a Half.
TryGetInt128(ref Int128) Attempts to represent the current JSON number as a Int128.
TryGetInt16(ref short) Attempts to represent the current JSON number as an Int16.
TryGetInt32(ref int) Attempts to represent the current JSON number as an Int32.
TryGetInt64(ref long) Attempts to represent the current JSON number as a Int64.
TryGetLine Tries to get the specified line from the original source document as UTF-8 bytes.
TryGetLineAndOffset Tries to get the 1-based line number and character offset of this element in the original source document.
TryGetLocalDate(ref LocalDate) Attempts to represent the current JSON string as a LocalDate.
TryGetOffsetDate(ref OffsetDate) Attempts to represent the current JSON string as a OffsetDate.
TryGetOffsetDateTime(ref OffsetDateTime) Attempts to represent the current JSON string as a OffsetDateTime.
TryGetOffsetTime(ref OffsetTime) Attempts to represent the current JSON string as a OffsetTime.
TryGetPeriod(ref Period) Attempts to represent the current JSON string as a Period.
TryGetProperty Looks for a property named propertyName in the current object, returning whether or not such a property existed. When the property exists value is assigned to the value of that property.
TryGetSByte(ref sbyte) Attempts to represent the current JSON number as an SByte.
TryGetSingle(ref float) Attempts to represent the current JSON number as a Single.
TryGetUInt128(ref UInt128) Attempts to represent the current JSON number as a UInt128.
TryGetUInt16(ref ushort) Attempts to represent the current JSON number as a UInt16.
TryGetUInt32(ref uint) Attempts to represent the current JSON number as a UInt32.
TryGetUInt64(ref ulong) Attempts to represent the current JSON number as a UInt64.
TryParseValue(ref Utf8JsonReader, ref Nullable<JsonElement>) static Attempts to parse one JSON value (including objects or arrays) from the provided reader.
ValueEquals Compares text to the string value of this element.
WriteTo(Utf8JsonWriter) Write the element into the provided writer as a JSON value.

Operators

Operator Description
operator ==(JsonElement, JsonElement) Compares two JsonElement values for equality.
operator !=(JsonElement, JsonElement) Compares two JsonElement values for inequality.

Examples

The following example demonstrates common property access patterns on a JsonElement-based generated type.

using ParsedJsonDocument<Person> doc =
    ParsedJsonDocument<Person>.Parse(jsonString);
Person person = doc.RootElement;

// Explicit cast to a .NET type (throws if null or undefined)
string familyName = (string)person.FamilyName;

// TryGetValue for optional properties (does not throw)
if (person.GivenName.TryGetValue(out string? givenName))
{
    Console.WriteLine($"Given name: {givenName}");
}

// Check whether an optional property is present
if (person.OtherNames.IsUndefined())
{
    Console.WriteLine("otherNames is not present.");
}

Zero-allocation string comparison

Compare string values directly against UTF-8 or UTF-16 literals without allocating:

bool isAnne = person.GivenName.ValueEquals("Anne"u8);

Equality

Generated types support structural equality — two elements are equal if their underlying JSON is equivalent, regardless of which document they came from:

using ParsedJsonDocument<Person> doc1 =
    ParsedJsonDocument<Person>.Parse(jsonString);
using ParsedJsonDocument<Person> doc2 =
    ParsedJsonDocument<Person>.Parse(jsonString);

bool areEqual = doc1.RootElement == doc2.RootElement; // true

Applies To

Product Versions
.NET 9, 10
.NET Standard 2.0, 2.1

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests.

Open an issue