var jobject = JObject.Parse(str);
If the given json str has the attribute
"seed":11405418255883340000
Error will be thrown: JsonReaderException: JSON integer 11405418255883340000 is too large or small for an Int64
As you’ll notice, 11405418255883340000 is too big for int64. But is a legitimate uint64.
Is this a bug or expected? What’s the easiest workaround?
2
Answers
The easiest is probably to create a DTO that matches the payload you expect and then set the type to
ulong
of the seed property.Here is an example DTO:
And here is how you use it, instead of Parse:
This is what LinqPad shows me:
If I change the property type back to
long
I get this exception:Note that you don’t have to code a fully specified DTO object. This one works as wel, where we simply don’t have the seed property on our DTO class:
If you are lazy you can drop the JSON payload into an online class generator and/or tooling in your IDE. See for a starting point on that: https://stackoverflow.com/a/21611680
You need
System.Numerics
so that it can handleBigInteger
. The default is to uselong
notulong
, so it will get an overflow. Then it will try to parse it as aBigInteger
.The comments in the JSON.Net source code say
So if you can add that library to your project then it should work.
But your best bet is to just use a proper object model, like the other answer says.