Reputation: 4366
not sure whether the subject is the best description for this problem but I am getting an unusual problem where I have a single Web API operation and a single field on a request and for some odd reason the value gets manipulated. Depending on the input this gets converted / translated by Web API or even by something else such as JSON.NET?
Should mention that this a brand new project with no additional references apart from what gets added by default when creating a new Web API project in Visual Studio.
public class TestController : ApiController
{
public void Post(Foo request)
{
}
}
public class Foo
{
public string Blah { get; set; }
}
Using a rest client I hit the operation using the following request:
{
"Blah": 43443333222211111117
}
When debugging the value of Blah gets converted to "6549845074792007885"
. I don't understand how and why its doing this? Any other value it respects. For example:
{
"Blah": 53443333222211111117
}
This is absolutely fine but is a bigger number.
Thanks, DS.
Upvotes: 2
Views: 91
Reputation: 15709
Update
This bug has been fixed and is scheduled to be included in the next release.
Original Answer
This is a bug in JSON.NET as hinted at, but it's not as simple as it first seems.
Versions prior to 5.0.4 work for both of these test cases. Anything after seems to fail but only for the first test case which is odd. I've gone through some of the JSON.NET code to try and see where this confusion occurs but at present cannot work out the why this is the case, I need to do more digging.
2147483647 Int Max 4444333322221111 Valid Credit Card Number Format 9223372036854775807 Int 64 Max 43443333222211111117 Dodgy Number greater than Int 64 hence overflow 53443333222211111117 Larger than above and Int 64, but works oddly. 1.7976931348623157E+308. Decimal max
Why 53443333222211111117 works is very odd. JSON.NET seems to have a 1025 character buffer set aside that contains a load of jibberish for my test cases, eventually the number is read incorrectly. I'll check this out further and raise an issue with JSON.NET.
If you use a decimal for the property this will work in all cases where the leading number is not zero, but this isn't a solution. For the short term, use version 5.0.3.
Take this example program to demonstrate the problem.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sending in: \n43443333222211111117");
var largeBrokenNumber = JsonConvert.DeserializeObject<Foo>("{\"Blah\": 43443333222211111117 }");
Console.WriteLine(largeBrokenNumber.Blah);
Console.WriteLine();
Console.WriteLine("Sending in: \n53443333222211111117");
var largeOddWorkingNumber = JsonConvert.DeserializeObject<Foo>("{\"Blah\": 53443333222211111117 }");
Console.WriteLine(largeOddWorkingNumber.Blah);
}
}
public class Foo
{
public string Blah { get; set; }
}
Upvotes: 1