Convert long number as string in the serialization

Posted by Bruno on Stack Overflow See other posts from Stack Overflow or by Bruno
Published on 2013-06-28T16:19:35Z Indexed on 2013/06/28 16:21 UTC
Read the original article Hit count: 210

Filed under:
|
|
|

I have a custom made class that use a long as ID. However, when I call my action using ajax, my ID is truncated and it loses the last 2 numbers because javascript loses precision when dealing with large numbers. My solution would be to give a string to my javascript, but the ID have to stay as a long on the server side.

Is there a way to serialize the property as a string? I'm looking for some kind of attribute.

Controller

public class CustomersController : ApiController
{
   public IEnumerable<CustomerEntity> Get()
   {
      yield return new CustomerEntity() { ID = 1306270928525862486, Name = "Test" };
   }
}

Model

public class CustomerEntity
{
   public long ID { get; set; }
   public string Name { get; set; }
}

JSON Result

[{"Name":"Test","ID":1306270928525862400}]

© Stack Overflow or respective owner

Related posts about c#

Related posts about JSON