JSON and XML Serialization in ASP.NET Web API
2021-06-17 03:05
标签:parse overview UNC names media sage msdn mod keyword https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization 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. 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: 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. 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. 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. 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: By default, Json.NET preserves the time zone. You can override this by setting the DateTimeZoneHandling property: If you prefer to use Microsoft JSON date format ( To write indented JSON, set the Formatting setting to Formatting.Indented: To write JSON property names with camel casing, without changing your data model, set the CamelCasePropertyNamesContractResolver on the serializer: An action method can return an anonymous object and serialize it to JSON. For example: The response message body will contain the following JSON: If your web API receives loosely structured JSON objects from clients, you can deserialize the request body to a Newtonsoft.Json.Linq.JObject type.JSON Media-Type Formatter
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.UseDataContractJsonSerializer = true;
JSON Serialization
What Gets Serialized?
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
[JsonIgnore]
public int ProductCode { get; set; } // omitted
}
[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
Dates
2012-07-27T18:51:45.53403Z // UTC
2012-07-27T11:51:45.53403-07:00 // Local
// Convert all dates to UTC
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
"\/Date(ticks)\/"
) instead of ISO 8601, set the DateFormatHandling property on the serializer settings:var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
Indenting
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
Camel Casing
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Anonymous and Weakly-Typed Objects
public object Get()
{
return new {
Name = "Alice",
Age = 23,
Pets = new Liststring> { "Fido", "Polly", "Spot" }
};
}
{"Name":"Alice","Age":23,"Pets":["Fido","Polly","Spot"]}
public void Post(JObject person)
{
string name = person["Name"].ToString();
int age = person["Age"].ToObjectint>();
}
However, it is usually better to use strongly typed data objects. Then you don‘t need to parse the data yourself, and you get the benefits of model validation.
The XML serializer does not support anonymous types or JObject instances. If you use these features for your JSON data, you should remove the XML formatter from the pipeline, as described later in this article.
XML Media-Type Formatter
XML formatting is provided by the XmlMediaTypeFormatter class. By default, XmlMediaTypeFormatter uses the DataContractSerializer class to perform serialization.
If you prefer, you can configure the XmlMediaTypeFormatter to use the XmlSerializer instead of the DataContractSerializer. To do so, set the UseXmlSerializer property to true:
JSON and XML Serialization in ASP.NET Web API
标签:parse overview UNC names media sage msdn mod keyword
原文地址:https://www.cnblogs.com/chucklu/p/10339035.html
文章标题:JSON and XML Serialization in ASP.NET Web API
文章链接:http://soscw.com/essay/94856.html