You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public static T ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
{
var bufferWriter = new ArrayBufferWriter();
using (var writer = new Utf8JsonWriter(bufferWriter))
{
element.WriteTo(writer);
}
return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options);
}
This is an additive API, so it shouldn't break anything.
Json.Net does this, so the pattern is well understood and used with existing audiences.
Reacted by Jack Kelliher, Jason Nelson, Soheil Alizadeh, Gérald Barré, kronic, David Fallah, Marton Balassa, Nick Drouin, olexandrvynnychenko, TONY HENRIQUE and 4 more
I think this will be covered by @steveharter's proposal #31274. It permits serialization/deserialization from the new writable DOM. I believe there are also ways to go from JsonElement to new DOM.
Activity
layomia commentedon Aug 6, 2020
cc @StephenBonikowsky
layomia commentedon Jul 8, 2021
Related to #31274, cc @steveharter.
danmoseley commentedon Jul 21, 2021
@layomia is this still committed for .NET 6?
ericstj commentedon Jul 21, 2021
I think this will be covered by @steveharter's proposal #31274. It permits serialization/deserialization from the new writable DOM. I believe there are also ways to go from JsonElement to new DOM.
steveharter commentedon Jul 21, 2021
Yes this is covered by #31274 and is currently under development for 6.0.
Specifically to go from a
JsonNodeinstance to an arbitrary object:namespace System.Text.Json { public static partial class JsonSerializer { + public static TValue? Deserialize<TValue>(this JsonNode node, JsonSerializerOptions? options = null); + public static TValue? Deserialize<TValue>(this JsonNode node, JsonTypeInfo<TValue> jsonTypeInfo); + public static object? Deserialize(this JsonNode node, Type returnType, JsonSerializerOptions? options = null); + public static object? Deserialize(this JsonNode node, Type returnType, JsonSerializerContext context); } }