谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了。因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”。但前几天有人询问为何很久没有更新,这让我感觉把这文章翻译出来还是有价值的。为此,本人打算将此工作继续下去。这些关于Web API的技术文章均由微软专业人员撰写,虽然文章作为博客帖子而写得比较简单,但如果仔细揣摩其内容,应当还是能够有不少收获的,也希望我的这些译文能够使那些对Web API有兴趣的园友从中得到一些收获。
尽管最近正忙于《Pro ASP.NET MVC 4》一书的翻译,但我仍会将此系列文章继续下去,只不过可能不会仔细检查,译文一定会有比较多的错误,恳请读者谅解。
6.2 JSON and XML Serialization in ASP.NET Web API6.2 ASP.NET Web API中的JSON和XML序列化
本文引自:
By Mike Wasson|May 30, 2012
作者:Mike Wasson|2012-3-30
This article describes the JSON and XML formatters in ASP.NET Web API.
本文描述ASP.NET Web API中的JSON和XML格式化器。
In ASP.NET Web API, a media-type formatter is an object that can:
在ASP.NET Web API中,媒体类型格式化器(Media-type Formatter)是一种能够做以下工作的对象:
从HTTP消息体读取CLR(公共语言运行时)对象
将CLR对象写入HTTP消息体
Web API provides media-type formatters for both JSON and XML. The framework inserts these formatters into the pipeline by default. Clients can request either JSON or XML in the Accept header of the HTTP request.
Web API提供了用于JSON和XML的媒体类型格式化器。框架已默认将这些格式化器插入到消息处理管线之中。客户端在HTTP请求的Accept报头中可以请求JSON或XML。
Contents
本小节内容
6.2.1 JSON媒体类型格式化器
JSON formatting is provided by the JsonMediaTypeFormatter class. By default, JsonMediaTypeFormatter uses the Json.NET library to perform serialization. Json.NET is a third-party open source project.
JSON格式化是由JsonMediaTypeFormatter类提供的。默认情况下,JsonMediaTypeFormatter使用Json.NET库执行序列化工作。Json.NET是一个第三方开源项目。
If you prefer, you can configure the JsonMediaTypeFormatter class to use the DataContractJsonSerializer instead of Json.NET. To do so, set the UseDataContractJsonSerializer property to true:
如果喜欢,你可以将JsonMediaTypeFormatter配置成使用DataContractJsonSerializer来代替Json.NET。要想这么做,只需UseDataContractJsonSerializer将属性设置为true即可:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = true;
6.2.2 JSON Serialization6.2.2 JSON序列化
This section describes some specific behaviors of the JSON formatter, using the default Json.NET serializer. This is not meant to be comprehensive documentation of the Json.NET library; for more information, see the Json.NET Documentation.
本小节描述,在使用默认的Json.NET序列化器时,JSON格式化器的一些特定行为。这并不意味着要包含Json.NET库的整个文档。更多信息参阅Json.NET Documentation。
什么会被序列化?
By default, all public properties and fields are included in the serialized JSON. To omit a property or field, decorate it with the JsonIgnore attribute.
默认情况下,所有public属性和字段都会被包含在序列化的JSON中。为了忽略一个属性或字段,需要用JsonIgnore注解属性修饰它。
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
[JsonIgnore]
public int ProductCode { get; set; } // omitted
}
If you prefer an "opt-in" approach, decorate the class with the DataContract attribute. If this attribute is present, members are ignored unless they have the DataMember. You can also use DataMember to serialize private members.
如果你更喜欢“opt-in(选入)”方法,可以用DataContract注解属性来修饰类。如果有注解属性,则成员均被忽略,除非有DataMember。DataMember也可以序列化private成员。
[DataContract]
public class Product
{
[DataMember]
public string Name { get; set; }
[DataMember]
public decimal Price { get; set; }
public int ProductCode { get; set; } // omitted by default
}
只读属性
Read-only properties are serialized by default.
只读属性默认是序列化的。
By default, Json.NET writes dates in ISO 8601 format. Dates in UTC (Coordinated Universal Time) are written with a "Z" suffix. Dates in local time include a time-zone offset. For example:
默认情况下,Json.NET会将日期写成ISO 8601格式。UTC(Coordinated Universal Time — 世界标准时间)格式的日期书写时带有后缀“Z”。本地时间格式的日期包括了一个时区偏移量。例如:
2012-07-27T18:51:45.53403Z // UTC(标准时间) 2012-07-27T11:51:45.53403-07:00 // Local(本地时间)
By default, Json.NET preserves the time zone. You can override this by setting the DateTimeZoneHandling property:
默认情况下,Json.NET保留时区。通过设置DateTimeZoneHandling属性,可以重写这一行为: