-
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
JsonElement and JsonDocument should be deserializable from JSON payloads #1573
Comments
@ahsonkhan asked why we would make it work for both. My argument is that |
As a workaround, you could create your own converter and register it with the options, based on the JsonElement converter. internal sealed class JsonConverterJsonDocument : JsonConverter<JsonDocument>
{
public override JsonDocument Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return JsonDocument.ParseValue(ref reader);
}
public override void Write(Utf8JsonWriter writer, JsonDocument value, JsonSerializerOptions options)
{
value.WriteTo(writer);
}
} var options = new JsonSerializerOptions();
options.Converters.Add(new JsonConverterJsonDocument());
using (var document = JsonSerializer.Deserialize<JsonDocument>("{\"data\": 12}", options))
{
// {"data": 12}
Console.WriteLine(document.RootElement);
} There is a question of lifetime semantics though, particularly who is responsible of disposing of the cc @bartonjs |
Well, it's sort of true for |
Agreed. That makes sense to me too.
I don't think restricting it to just |
Deserializing to JsonDocument is weird, it's just a different entrypoint to JsonDocument.Parse (or JsonDocument.ParseValue); but there's nothing fundamentally wrong with it, I guess. Since you asked for it as a whole document it makes sense that it could be pool backed and you are responsible for disposing it. (It holds true for all Disposable objects, really... you caused it to be born by deserializing it, you own the lifetime) Serializing a JsonDocument is likewise a bit weird, but has easy semantics, since it's just Serialize(doc.RootElement) |
Sounds like we agree to allow |
What do you mean by If the concern is about using up the array pool and not returning things back, we are cloning the element before returning it (which uses a GC-tracked/regular array) and we are disposing the original document. This returns the original array back to the pool. |
Got it. In since we do have to support |
I'd like to take this one. Any guidance or things to be aware of? |
This change adds support to `System.Text.Json` for deserializing `JsonDocument`. Specifically, an internal converter `JsonDocumentConverter` is added to the default converter dictionary. I have created a basic test, but I feel more could be done here, and it may not be in the right file/class - some guidance would be helpful here. Fixes dotnet#1573
* Adds deserialization support for JsonDocument This change adds support to `System.Text.Json` for deserializing `JsonDocument`. Specifically, an internal converter `JsonDocumentConverter` is added to the default converter dictionary. I have created a basic test, but I feel more could be done here, and it may not be in the right file/class - some guidance would be helpful here. Fixes #1573 * Adds JsonDocumentTests * Dispose JsonDocument
This works for
JsonElement
today:but fails for
JsonDocument
:Equally,
Serialize
should also work for both, but only works forJsonElement
./cc @steveharter
The text was updated successfully, but these errors were encountered: