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
sourceReadOnlySpan<byte>Bytes starting at the token.
valueboolThe decoded value, meaningful only on success.
tokenSizeintThe consumed size on success, the minimum required buffer length on incomplete input, or zero on mismatch.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valuebyteThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valuedecimalThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valuedoubleThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valueshortThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valueintThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valuelongThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valuesbyteThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valuefloatThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valueushortThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valueuintThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valueulongThe decoded value, meaningful only on success.
tokenSizeintThe consumed size, minimum required size, or zero on mismatch or overflow.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
tokenSizeintThe consumed size on success, the minimum required buffer length on incomplete input, or zero on mismatch.
isFinalBlockboolWhether 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
sourceReadOnlySpan<byte>Bytes starting at the token.
valueReadOnlySpan<byte>A borrowed view of the complete token, meaningful only on success.
tokenSizeintThe consumed size on success, the minimum required buffer length on incomplete input, or zero on mismatch.
isFinalBlockboolWhether 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
destinationSpan<byte>The output buffer.
valueboolThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valuebyteThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valuedecimalThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valuedoubleThe finite value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valueshortThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valueintThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valuelongThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valuesbyteThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valuefloatThe finite value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valueushortThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valueuintThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valueulongThe value to encode.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
tokenSizeintThe 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
destinationSpan<byte>The output buffer.
valueReadOnlySpan<byte>An entire number token with no surrounding whitespace.
tokenSizeintThe exact required size, or zero for invalid input.
Returns
- bool
Whether the token is valid and the destination was large enough.