JToken.explicit operator bool cannot call operator or accessor directly

138 Views Asked by At
if (JToken.op_Explicit(apiConfig["finjVersion"]) < JToken.op_Explicit(latestData["finjVersion"]))
{
 ...
}

Error: JToken.explicit operator bool(JToken)': cannot call operator or accessor directly

I tried searching it in Newtonsoft's website or Microsoft and found nothing.

1

There are 1 best solutions below

1
dbc On BEST ANSWER

It looks like you are trying to invoke one of the explicit conversion operators of JToken to convert a JSON property value to a .NET primitive. In order to use these operators, you must use the explicit conversion syntax of c# to cast the value to the required primitive type. You must also choose an appropriate .NET primitive type to which to cast that corresponds to the JSON property value.

E.g. if the value of "finjVersion" is some integer such as "finjVersion" : 1 you should do:

if ((long)apiConfig["finjVersion"] < (long)latestData["finjVersion"])
{
    //...
}

Demo #1 here.

If the value of "finjVersion" does not correspond to a .NET primitive, rather than casting you must use ToObject<T>() to deserialize to the required type. E.g. if "finjVersion" looks like "finjVersion":"1.1.2.0", you could deserialize to System.Version like so:

if (apiConfig["finjVersion"].ToObject<Version>() < latestData["finjVersion"].ToObject<Version>())
{
    //...
}

Demo #2 here.