-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Utf8JsonReader should read from JsonNode #106047
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
It can be improved with something like: using System.Buffers;
using System.Text.Json;
using System.Text.Json.Nodes;
JsonNode node = GetNode();
Utf8JsonReader reader = node.AsUtf8JsonReader();
Use(reader);
public static class JsonNodeExtensions
{
public static Utf8JsonReader AsUtf8JsonReader(this JsonNode payloadData)
{
ArrayBufferWriter<byte> bufferWriter = new();
using Utf8JsonWriter writer = new(bufferWriter);
payloadData.WriteTo(writer);
return new(bufferWriter.WrittenMemory.Span);
}
} |
This is a known restriction emanating from I don't believe extending the Note that adding such an interface is dependent on the language adding support for ref struct interface implementations, but even then this would require duplicating most of the serialization API surface to support polymorphic readers. For starters, public partial class JsonConverter<T>
{
public virtual T? Read<TReader>(ref TReader reader, Type type, JsonSerializerOptions options) where TReader : allow ref struct, IJsonReader => throw new NotImplementedException();
} which converter authors would then need to implement. Needless to say, this has cost and complexity comparable to implementing a serializer from scratch. |
For completeness, this exists now in C# 13 and .NET 9. |
@am11 FYI, a |
Background and motivation
I have deserialisation code that reads data from JSON text, usually files on disk. It's using the
Utf8JsonReader
to read the source. It already exists and is somewhat complex so I don't want to duplicate the whole thing to also read fromJsonNode
. Another API that receives JSON-formatted messages from a network connection gives me the payload part asJsonNode
because it has already read the whole message and needed to process parts of it.So what I'm doing now is this:
This converts me the already parsed payload from
JsonNode
to serialised text, copies it over a few times for the encoding and finally I have something thatUtf8JsonReader
can read from.Since
Utf8JsonReader
is a ref struct, I can't simply derive from it and change the behaviour. It needs to be the exact thing that can read fromJsonNode
as well internally.API Proposal
API Usage
Save the text work and read tokens from the nodes directly. It should be easy since the tokens are already known. The nodes can simply be iterated and returned in the reader API.
Alternative Designs
Extra processing and memory allocation through the intermediate serialisation step. This is the current workaround.
Risks
I don't see any. It's a new API that has never existed before and nobody needs to use it if they don't need to.
The text was updated successfully, but these errors were encountered: