Skip to content

Utf8JsonReader.CopyString Method

Definition

Namespace: Corvus.Text.Json
Assembly: Corvus.Text.Json.dll

Overloads

Method Description
CopyString(Span<byte>) Copies the current JSON token value from the source, unescaped as a UTF-8 string to the destination buffer.
CopyString(Span<char>) Copies the current JSON token value from the source, unescaped, and transcoded as a UTF-16 char buffer.

CopyString(Span<byte>)

Source: Utf8JsonReader.TryGet.cs

Copies the current JSON token value from the source, unescaped as a UTF-8 string to the destination buffer.

public int CopyString(Span<byte> utf8Destination)

Parameters

Name Type Description
utf8Destination Span<byte> A buffer to write the unescaped UTF-8 bytes into.

Returns

int

The number of bytes written to utf8Destination.

Exceptions

Exception Description
InvalidOperationException Thrown if trying to get the value of the JSON token that is not a string (i.e. other than String or PropertyName. It will also throw when the JSON string contains invalid UTF-8 bytes, or invalid UTF-16 surrogates.
ArgumentException The destination buffer is too small to hold the unescaped value.

Remarks

Unlike GetString, this method does not support Null. This method will throw ArgumentException if the destination buffer is too small to hold the unescaped value. An appropriately sized buffer can be determined by consulting the length of either ValueSpan or ValueSequence, since the unescaped result is always less than or equal to the length of the encoded strings.

Applies To

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

CopyString(Span<char>)

Source: Utf8JsonReader.TryGet.cs

Copies the current JSON token value from the source, unescaped, and transcoded as a UTF-16 char buffer.

public int CopyString(Span<char> destination)

Parameters

Name Type Description
destination Span<char> A buffer to write the transcoded UTF-16 characters into.

Returns

int

The number of characters written to destination.

Exceptions

Exception Description
InvalidOperationException Thrown if trying to get the value of the JSON token that is not a string (i.e. other than String or PropertyName. It will also throw when the JSON string contains invalid UTF-8 bytes, or invalid UTF-16 surrogates.
ArgumentException The destination buffer is too small to hold the unescaped value.

Remarks

Unlike GetString, this method does not support Null. This method will throw ArgumentException if the destination buffer is too small to hold the unescaped value. An appropriately sized buffer can be determined by consulting the length of either ValueSpan or ValueSequence, since the unescaped result is always less than or equal to the length of the encoded strings.

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