0

I have an object with an attribute defined as long and the exact value is 635980054734850470 but when it gets serialised the JSON output gives me 635980054734850400

It seems to be consistently dropping the last two digit values, rather than giving me the exact, value. Is there any reason for this?

Here's the example C# code:

[Route("/timestamp", Verbs = "GET")]
public class GetTimestamp : IReturn<TimestampData>
{

}

public class TimestampData 
{ 
    public long Timestamp { get; set; }
}

public class TimestampService : CustomerServiceBase
{
    public object Get(GetTimestamp request)
    {
        var timestamp = DateTime.UtcNow.Ticks;
        Console.WriteLine(timestamp);
        return new TimestampData() { Timestamp = timestamp };
    }
}

Sample output:

{"Timestamp":635984646884003500}

Notice the output always rounds to the nearest 100.

vonec
  • 681
  • 10
  • 22
  • Please post the JSON and C# code you're using. Please try to always provide a [minimal verifiable example](http://stackoverflow.com/help/mcve) when asking support questions. – mythz May 10 '16 at 06:14
  • @mythz sorry I've added the example c# and sample JSON output – vonec May 10 '16 at 08:14

1 Answers1

2

The JSON Serializer is working as expected:

var json = "{\"Timestamp\":635980054734850470}";
var dto = json.FromJson<TimestampData>();
dto.ToJson().Print(); //= {"Timestamp":635980054734850470}

And so is the JSON Service Response which you can see by adding .json, e.g:

/timestamp.json

Or viewing the response in Web Inspector, Fiddler or other packet inspector.

The only time I see it rounding is in ServiceStack's auto HTML5 Report Format pages which parses the JSON response into a JavaScript object, e.g:

var model = {"Timestamp":635980054734850470};

Which creates a JavaScript object containing:

{Timestamp: 635980054734850400}

This is because numbers in JavaScript are stored as doubles where the highest integer it can store without losing precision is 9007199254740992 which as it's less than 635980054734850470 it ends up losing some precision.

mythz
  • 141,670
  • 29
  • 246
  • 390