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. For example:
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.
Deserialize()
usingoptions
if these options are not used when writing the internal buffer? – Jaejaeger