Description
I copy-pasted the code snippet from Consume decoded JSON strings
section into my Visual Studio:
int valueLength = reader.HasReadOnlySequence
? checked((int)ValueSequence.Length)
: ValueSpan.Length;
char[] buffer = ArrayPool<char>.Shared.Rent(valueLength);
int charsRead = reader.CopyString(buffer);
ReadOnlySpan<char> source = buffer.Slice(0, charsRead);
// Handle the unescaped JSON string.
ParseUnescapedString(source);
ArrayPool<char>.Shared.Return(buffer, clearArray: true);
and here is what I got:
all the symbols marked in red are not recognized. I searched a bit and looks like this is not a problem with my setup.
For example, Utf8JsonReader doesn't appear to have a HasReadOnlySequence
member.
It is also a bit confusing that the section says:
The following code snippet shows an example of consuming a UTF-16 string using CopyString.
but refers to UTF-8. So is it UTF-8 or UTF-16 ?
I think the doc should provide near-obviously-compilable code snippets. At the very least it should explain what needs to be done to make the snippet compile, like what dependencies to import to get the right Slice
method, how to implement ParseUnescapedString
and so on.
Related:
How to write custom converters for JSON serialization (marshalling) in .NET
should explain how to handle complex types #35020- The documentation on using
Utf8JsonReader
should explain how to obtain different kinds of objects from it, like e.g.JsonNode
. #35021
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
- ID: 73a13d38-4904-c068-c9eb-e43176ce5338
- Version Independent ID: c5dfa4d2-c77a-e752-dae7-0bfae5756b76
- Content: How to use a JSON document, Utf8JsonReader, and Utf8JsonWriter in System.Text.Json
- Content Source: docs/standard/serialization/system-text-json/use-dom-utf8jsonreader-utf8jsonwriter.md
- Product: dotnet-fundamentals
- GitHub Login: @gewarren
- Microsoft Alias: gewarren