Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JToken.ToObject<T>() vs JToken.Value<T>()

Tags:

json

c#

json.net

What is the difference between the JToken.ToObject<T>() method and the JToken.Value<T>() extension method (the one without the key parameter)?

var jToken = JToken.Parse("123");
var toObjectStrResult = jToken.ToObject<string>();
var valueStrResult = jToken.Value<string>();
// toObjectStrResult  == valueStrResult == "123"

var toObjectLongResult = jToken.ToObject<long>();
var valueLongResult = jToken.Value<long>();
// toObjectLongResult  == valueLongResult  == 123L

like image 353
Darxis Avatar asked Oct 01 '19 15:10

Darxis


People also ask

What is JObject and JToken?

The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...

What does JToken parse do?

JToken is an abstract base class that represents any one of these possible tokens. If you have some JSON and don't know in advance what might be inside, you can parse it with JToken. Parse() and get a result as long as the JSON is well-formed.

What is JToken?

jToken refers to the “receipt” users get for supplying underlying assets when mining on JustLend, such as the jTRX, jUSDT, jSUN and jBTC you receive after supplying the corresponding assets. jToken is a TRC20 token in your wallet. JustLend mining pools are multi-reward pools jointly launched by SUN and JustLend.

What is JObject?

JObject. It represents a JSON Object. It helps to parse JSON data and apply querying (LINQ) to filter out required data. It is presented in Newtonsoft. Json.


1 Answers

The difference is as follows:

  1. ToObject<T>() is a deserialization operation. It constructs a JsonSerializer and uses it to deserialize the current JToken to the desired type. As such the token could be anything (a JSON array, a JSON object, or a JSON primitive value) and the serializer will, using reflection, try to deserialize the token to the desired type by reading through its contents with a JTokenReader.

    This method is useful when writing generic code where the input token and output type could be anything. It is the most general and fail-safe way to create a c# object from a JToken.

  2. Extensions.Value<U>(IEnumerable<JToken>) is a conversion/casting operation. It attempts to convert the value of the current token to the target type by invoking Convert.ChangeType() (as well as handling a few special cases).

    This method is useful when you know your JToken is, in fact, a JValue and you want to convert its Value to a specific, required .Net primitive type. For instance, if the JValue might contain a long or numeric string, you could convert it to an int, a decimal or a double. If it might contain a DateTime or a string in ISO 8601 format, you could convert it to a DateTime. And any primitive JSON value can always converted to a string.

    While this method is less general than ToObject<T>() it will be more performant in converting primitive values since the serializer invokes the same conversion methods internally when deserializing a primitive.

like image 107
dbc Avatar answered Sep 22 '22 16:09

dbc