I want to know the equivalent of the ToObject<>()
method in Json.NET for System.Text.Json.
Using Json.NET you can use any JToken
and convert it to a class. EG:
var str = ""; // some json string
var jObj = JObject.Parse(str);
var myClass = jObj["SomeProperty"].ToObject<SomeClass>();
How would we be able to do this with .NET Core 3's new System.Text.Json
var str = ""; // some json string
var jDoc = JsonDocument.Parse(str);
var myClass = jDoc.RootElement.GetProperty("SomeProperty"). <-- now what??
Initially I was thinking I'd just convert the JsonElement
that is returned in jDoc.RootElement.GetPRoperty("SomeProperty")
to a string and then deserialize that string. But I feel that might not be the most efficient method, and I can't really find documentation on doing it another way.
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.
Then use as follows:
There is an open enhancement about this, currently targeted for .NET Core 5.0:
In the interim you may get better performance by writing to an intermediate
byte
buffer rather than to a string, since bothJsonDocument
andUtf8JsonReader
work directly withbyte
spans rather than strings orchar
spans:Demo fiddle here.