Table of Contents

Class JsonPrimitives

Namespace
Nerdbank.Json
Assembly
Nerdbank.Json.dll

Provides stateless UTF-8 JSON scalar encoding and decoding.

public static class JsonPrimitives
Inheritance
JsonPrimitives
Inherited Members

Remarks

Readers start exactly at a token, without skipping whitespace, comments, or a BOM. Set isFinalBlock only when no more input can arrive. A scalar at the end of a non-final block needs another byte to establish its boundary. Failure outputs are not decoded values. An incomplete token's size is a lower bound, not a completion guarantee: further bytes may extend its lexeme, and a delimiter or final-block indication is still required. Size hints are capped at MaxValue, the maximum span length. Writers leave the destination unchanged on failure and report the exact required size, or zero for invalid input. Floating-point reads follow the target framework's numeric parsing behavior, including legacy rounding limitations. Legacy floating-point writers use exponential notation with nine significant digits for Single and seventeen for Double rather than the legacy formatter's lossy default precision. Negative zero is preserved. Scalar reads and writes use overloads for boolean values and each numeric type from byte through decimal. Floating-point values must be finite; single-precision values are parsed and formatted without widening to double precision.

Methods

TryRead(ReadOnlySpan<byte>, out bool, out int, bool)

Attempts to decode a JSON boolean literal.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out bool value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value bool

The decoded value, meaningful only on success.

tokenSize int

The consumed size on success, the minimum required buffer length on incomplete input, or zero on mismatch.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out byte, out int, bool)

Attempts to decode an integer into an unsigned 8-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out byte value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value byte

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out decimal, out int, bool)

Attempts to decode a JSON number into a decimal value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out decimal value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value decimal

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out double, out int, bool)

Attempts to decode a JSON number into a finite double-precision value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out double value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value double

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out short, out int, bool)

Attempts to decode an integer into a signed 16-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out short value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value short

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out int, out int, bool)

Attempts to decode an integer into a signed 32-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out int value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value int

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out long, out int, bool)

Attempts to decode an integer into a signed 64-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out long value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value long

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out sbyte, out int, bool)

Attempts to decode an integer into a signed 8-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out sbyte value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value sbyte

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out float, out int, bool)

Attempts to decode a JSON number into a finite single-precision value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out float value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value float

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out ushort, out int, bool)

Attempts to decode an integer into an unsigned 16-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out ushort value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value ushort

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out uint, out int, bool)

Attempts to decode an integer into an unsigned 32-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out uint value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value uint

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryRead(ReadOnlySpan<byte>, out ulong, out int, bool)

Attempts to decode an integer into an unsigned 64-bit value.

public static JsonPrimitives.DecodeResult TryRead(ReadOnlySpan<byte> source, out ulong value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value ulong

The decoded value, meaningful only on success.

tokenSize int

The consumed size, minimum required size, or zero on mismatch or overflow.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryReadNull(ReadOnlySpan<byte>, out int, bool)

Attempts to decode a JSON null literal.

public static JsonPrimitives.DecodeResult TryReadNull(ReadOnlySpan<byte> source, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

tokenSize int

The consumed size on success, the minimum required buffer length on incomplete input, or zero on mismatch.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryReadNumberToken(ReadOnlySpan<byte>, out ReadOnlySpan<byte>, out int, bool)

Attempts to frame a JSON number without converting or copying its bytes.

public static JsonPrimitives.DecodeResult TryReadNumberToken(ReadOnlySpan<byte> source, out ReadOnlySpan<byte> value, out int tokenSize, bool isFinalBlock = true)

Parameters

source ReadOnlySpan<byte>

Bytes starting at the token.

value ReadOnlySpan<byte>

A borrowed view of the complete token, meaningful only on success.

tokenSize int

The consumed size on success, the minimum required buffer length on incomplete input, or zero on mismatch.

isFinalBlock bool

Whether this is the end of the input.

Returns

JsonPrimitives.DecodeResult

The decoding classification.

TryWrite(Span<byte>, bool, out int)

Attempts to write a JSON boolean literal.

public static bool TryWrite(Span<byte> destination, bool value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value bool

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, byte, out int)

Attempts to write an unsigned 8-bit integer as a JSON number.

public static bool TryWrite(Span<byte> destination, byte value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value byte

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, decimal, out int)

Attempts to write a decimal JSON number.

public static bool TryWrite(Span<byte> destination, decimal value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value decimal

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, double, out int)

Attempts to write a finite double-precision JSON number.

public static bool TryWrite(Span<byte> destination, double value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value double

The finite value to encode.

tokenSize int

The exact number of bytes written or required, or zero for a non-finite value.

Returns

bool

Whether the value is finite and the destination was large enough.

TryWrite(Span<byte>, short, out int)

Attempts to write a signed 16-bit integer as a JSON number.

public static bool TryWrite(Span<byte> destination, short value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value short

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, int, out int)

Attempts to write a signed 32-bit integer as a JSON number.

public static bool TryWrite(Span<byte> destination, int value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value int

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, long, out int)

Attempts to write a signed integer as a JSON number.

public static bool TryWrite(Span<byte> destination, long value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value long

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, sbyte, out int)

Attempts to write a signed 8-bit integer as a JSON number.

public static bool TryWrite(Span<byte> destination, sbyte value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value sbyte

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, float, out int)

Attempts to write a finite single-precision JSON number without widening it to double precision.

public static bool TryWrite(Span<byte> destination, float value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value float

The finite value to encode.

tokenSize int

The exact number of bytes written or required, or zero for a non-finite value.

Returns

bool

Whether the value is finite and the destination was large enough.

TryWrite(Span<byte>, ushort, out int)

Attempts to write an unsigned 16-bit integer as a JSON number.

public static bool TryWrite(Span<byte> destination, ushort value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value ushort

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, uint, out int)

Attempts to write an unsigned 32-bit integer as a JSON number.

public static bool TryWrite(Span<byte> destination, uint value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value uint

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWrite(Span<byte>, ulong, out int)

Attempts to write an unsigned integer as a JSON number.

public static bool TryWrite(Span<byte> destination, ulong value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value ulong

The value to encode.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWriteNull(Span<byte>, out int)

Attempts to write a JSON null literal.

public static bool TryWriteNull(Span<byte> destination, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

tokenSize int

The exact number of bytes written or required.

Returns

bool

Whether the destination was large enough.

TryWriteNumberToken(Span<byte>, ReadOnlySpan<byte>, out int)

Attempts to write exactly one validated JSON number without normalizing its spelling.

public static bool TryWriteNumberToken(Span<byte> destination, ReadOnlySpan<byte> value, out int tokenSize)

Parameters

destination Span<byte>

The output buffer.

value ReadOnlySpan<byte>

An entire number token with no surrounding whitespace.

tokenSize int

The exact required size, or zero for invalid input.

Returns

bool

Whether the token is valid and the destination was large enough.