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(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(BigInteger) Attempts to represent the current JSON number as a BigInteger.
TryGetBigNumber(BigNumber) Attempts to represent the current JSON number as a BigNumber.
TryGetBoolean(bool) Tries to get the value as a boolean
TryGetByte(byte) Attempts to represent the current JSON number as a Byte.
TryGetBytesFromBase64(byte[]) Attempts to represent the current JSON string as bytes assuming it is Base64 encoded.
TryGetDateTime(DateTime) Attempts to represent the current JSON string as a DateTime.
TryGetDateTimeOffset(DateTimeOffset) Attempts to represent the current JSON string as a DateTimeOffset.
TryGetDecimal(decimal) Attempts to represent the current JSON number as a Decimal.
TryGetDouble(double) Attempts to represent the current JSON number as a Double.
TryGetGuid(Guid) Attempts to represent the current JSON string as a Guid.
TryGetHalf(Half) Attempts to represent the current JSON number as a Half.
TryGetInt128(Int128) Attempts to represent the current JSON number as a Int128.
TryGetInt16(short) Attempts to represent the current JSON number as an Int16.
TryGetInt32(int) Attempts to represent the current JSON number as an Int32.
TryGetInt64(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(LocalDate) Attempts to represent the current JSON string as a LocalDate.
TryGetOffsetDate(OffsetDate) Attempts to represent the current JSON string as a OffsetDate.
TryGetOffsetDateTime(OffsetDateTime) Attempts to represent the current JSON string as a OffsetDateTime.
TryGetOffsetTime(OffsetTime) Attempts to represent the current JSON string as a OffsetTime.
TryGetPeriod(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(sbyte) Attempts to represent the current JSON number as an SByte.
TryGetSingle(float) Attempts to represent the current JSON number as a Single.
TryGetUInt128(UInt128) Attempts to represent the current JSON number as a UInt128.
TryGetUInt16(ushort) Attempts to represent the current JSON number as a UInt16.
TryGetUInt32(uint) Attempts to represent the current JSON number as a UInt32.
TryGetUInt64(ulong) Attempts to represent the current JSON number as a UInt64.
TryParseValue(Utf8JsonReader, 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

JsonElement is the general-purpose read-only JSON value type in Corvus.Text.Json. Parse through ParsedJsonDocument<JsonElement> so the document owns the pooled memory while you inspect the root element.

using Corvus.Text.Json;

using ParsedJsonDocument<JsonElement> doc =
    ParsedJsonDocument<JsonElement>.Parse(
        """
        {
          "name": "Anne",
          "age": 42,
          "roles": ["admin", "editor"]
        }
        """);

JsonElement root = doc.RootElement;
string? name = root.GetProperty("name"u8).GetString();
int age = root.GetProperty("age"u8).GetInt32();

if (root.TryGetProperty("roles"u8, out JsonElement roles))
{
    foreach (JsonElement role in roles.EnumerateArray())
    {
        Console.WriteLine(role.GetString());
    }
}

Inspecting value kinds

Use ValueKind before reading a value when the shape is not guaranteed.

JsonElement maybeName = root.GetProperty("name"u8);

if (maybeName.ValueKind == JsonValueKind.String)
{
    Console.WriteLine(maybeName.GetString());
}

Zero-allocation string comparison

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

bool isAnne = root.GetProperty("name"u8).ValueEquals("Anne"u8);

Enumerating object properties

Object enumeration exposes property names and values without converting the entire object to another model:

foreach (JsonProperty<JsonElement> property in root.EnumerateObject())
{
    Console.WriteLine($"{property.Name}: {property.Value.ValueKind}");
}

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