转换长的数字作为字符串系列化 [英] Convert long number as string in the serialization
问题描述
我有使用只要ID的定制类。然而,当我使用Ajax调用我的行动,我的ID是截断,因为JavaScript处理大量交易时失去precision它失去了最后2个数字。我的解决办法是给一个字符串我的javascript,但ID必须保持作为服务器端的长。
有没有办法序列化属性作为一个字符串?我在寻找某种属性。
控制器
公共类CustomersController:ApiController
{
公共IEnumerable的< CustomerEntity>得到()
{
产量返回新CustomerEntity(){ID = 1306270928525862486,名称=测试};
}
}
示范
公共类CustomerEntity
{
众长ID {搞定;组; }
公共字符串名称{;组; }
}
JSON结果
[{名称:测试,ID:1306270928525862400}]
您也许可以创建自定义的 JsonConverter
键,它适用于你的财产。
下面是一个例子(注意:我还没有使用此API之前,因此也许可以进一步提高,但下面应该给你一个粗略的想法):
公共类Person
{
[JsonConverter(typeof运算(IdToStringConverter))]
众长ID {搞定;组; } 公共字符串名称{;组; }
}公共类IdToStringConverter:JsonConverter
{
公众覆盖对象ReadJson(JsonReader读者,类型的objectType,对象existingValue,JsonSerializer串行)
{
JToken JT = JValue.ReadFrom(读卡器); 返回jt.Value<长>();
} 公众覆盖布尔CanConvert(类型的objectType)
{
返回的typeof(System.Int64).Equals(的objectType);
} 公共覆盖无效WriteJson(JsonWriter作家,对象的值,JsonSerializer串行)
{
serializer.Serialize(作家,value.ToString());
}
}
网页API操作:
公众人物发表([FromBody]人的人)
{
返回的人;
}
请求:
POST HTTP:// asdfasdf / API /值HTTP / 1.1
主持人:服务器:9095
连接:保持活动
内容类型:应用程序/ JSON
内容长度:42{ID:1306270928525862400,名:迈克}
响应:
HTTP / 1.1 200 OK
内容长度:42
内容类型:应用程序/ JSON的;字符集= UTF-8
服务器:HTTPAPI / 2.0
日期:星期五,2013年6月28日17时02分18秒GMT{ID:1306270928525862400,名:迈克}
修改:结果
如果你不想装饰与属性的属性,可以转而将其添加到转换器集合。例如:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(新IdToStringConverter());
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}]
You could probably create a custom JsonConverter
and apply it on your property.
Following is an example (NOTE: I haven't used this api before so it could probably be improved more, but following should give you a rough idea):
public class Person
{
[JsonConverter(typeof(IdToStringConverter))]
public long ID { get; set; }
public string Name { get; set; }
}
public class IdToStringConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken jt = JValue.ReadFrom(reader);
return jt.Value<long>();
}
public override bool CanConvert(Type objectType)
{
return typeof(System.Int64).Equals(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value.ToString());
}
}
Web API Action:
public Person Post([FromBody]Person person)
{
return person;
}
Request:
POST http://asdfasdf/api/values HTTP/1.1
Host: servername:9095
Connection: Keep-Alive
Content-Type: application/json
Content-Length: 42
{"ID":"1306270928525862400","Name":"Mike"}
Response:
HTTP/1.1 200 OK
Content-Length: 42
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 28 Jun 2013 17:02:18 GMT
{"ID":"1306270928525862400","Name":"Mike"}
EDIT:
if you do not want to decorate the property with an attribute, you could instead add it to the Converters collection. Example:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IdToStringConverter());
这篇关于转换长的数字作为字符串系列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!