Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

JsonTokenType — JSON token kind

JsonTokenType is an enumeration in System.Text.Json describing a token recognized by Utf8JsonReader. It distinguishes object and array boundaries, property names, and values. None means the state before reading, not the JSON value null.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

JsonTokenType tokenType = reader.TokenType;
if (tokenType == JsonTokenType.Null) { /* ... */ }

JsonTokenType in System.Text.Json describes a token's kind. Utf8JsonReader.TokenType exposes the kind of the last processed token; inspect it after a successful Read().

StartObject and EndObject mark object boundaries; StartArray and EndArray mark array boundaries. PropertyName identifies a property name, not its string value: that uses String. Other value kinds are Number, True, False, and Null.

None is the initial state before reading data, distinct from the Null token. Comment represents a comment but requires JsonCommentHandling.Allow. The default, Disallow, rejects comments; Skip ignores them without exposing tokens.

The example first prints None, then successive token kinds. The loop continues while Read() returns true. Invalid JSON can throw JsonException rather than simply ending the loop.

Examples

using System;
using System.Text.Json;

var reader = new Utf8JsonReader("{\"name\":\"Ada\",\"active\":true,\"note\":null}"u8);
Console.WriteLine(reader.TokenType);

while (reader.Read())
{
    JsonTokenType tokenType = reader.TokenType;
    Console.WriteLine(tokenType);
}

Result

None
StartObject
PropertyName
String
PropertyName
True
PropertyName
Null
EndObject

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources