System.Text.Json.JsonElement ToObject workaround

I came across the same issue, so I wrote some extension methods which work fine for now. It would be nice if they provided this as built in to avoid the additional allocation to a string. public static T ToObject<T>(this JsonElement element) { var json = element.GetRawText(); return JsonSerializer.Deserialize<T>(json); } public static T ToObject<T>(this JsonDocument … Read more

Is polymorphic deserialization possible in System.Text.Json?

Is polymorphic deserialization possible in System.Text.Json? The answer is yes and no, depending on what you mean by “possible”. There is no polymorphic deserialization (equivalent to Newtonsoft.Json’s TypeNameHandling) support built-in to System.Text.Json. This is because reading the .NET type name specified as a string within the JSON payload (such as $type metadata property) to create … Read more