ie421.NET

面对技术,你别无选择,.NET世界是如此精彩,而我们要做的就是:Thinking More

博客园 首页 新随笔 联系 订阅 管理
 
Json.NET - Quick Starts & API Documentation
Serializing and deserializing JSON

The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent and back again.

For simple scenarios the SerializeObject and DeserializeObject methods on JavaScriptConvert provide an easy to use wrapper over JsonSerializer.

Product product = new Product();
 
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
 
string output = JavaScriptConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": "\/Date(1230375600000+1300)\/",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}
 
Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));

JsonSerializer

For more control over how an object is serialized the JsonSerializer can be used directly. The JsonSerializer has a number of properties to control its behaviour when serializing and deserializing.

Product product = new Product();
product.Expiry = new DateTime(2008, 12, 28);
 
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
 
using (StreamWriter sw = new StreamWriter(@"c:\json.txt"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
  serializer.Serialize(writer, product);
  // {"Expiry":new Date(1230375600000),"Price":0}
}
ReferenceLoopHandling

Controls how circular referencing objects are serialized. Error, ignore or serialize.

MissingMemberHandling

Controls how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization. Error or ignore.

NullValueHandling

Controls how null values are handled during serialization and deserialization. Include or ignore.

ObjectCreationHandling

Controls how objects are created during deserialization. Auto, reuse, replace.

Converters

A collection of JsonConverters that will be used during serialization and deserialization.

Serialization Attributes

There are a number of optional attributes that can be placed on a class to control how it is serialized.

[JsonObject(MemberSerialization.OptIn)]
public class Person
{
  [JsonProperty]
  public string Name { get; set; }
 
  [JsonProperty]
  public DateTime BirthDate { get; set; }
 
  // not serialized
  public string Department { get; set; }
}
JsonObjectAttribute

The MemberSerialization flag on this attribute specifies whether member serialization is opt-in (a member must have the JsonPropertyAttribute to be serialized) or opt-out (everything is serialized by default but can be ignored with the JsonIgnoreAttribute).

JsonPropertyAttribute

Allows the name of the serialized member to be customized. The attribute also indicates that a member should be serialized when member serialization is set to opt-in.

JsonIgnoreAttribute

Excludes a field or property from serialization.

JsonConverters

JsonConverters allows JSON to be manually be written during serialization and read during deserialization. This is useful for particularly complex JSON structures or for when you want to change how a type is serialized.

To create your own custom converter inherit from the JsonConverter class. Json.NET also comes with a number of JsonConverters:

IsoDateTimeConverter

Converts a DateTime to and from the ISO 8601 date format (e.g. 2008-04-12T12:53Z).

JavaScriptDateTimeConverter

Converts a DateTime to and from a JavaScript date constructor (e.g. new Date(52231943)).

XmlNodeConverter

Converts an XmlNode to and from JSON. Note that to convert a JSON object it must have only a single property. This is required because properties are converted into nodes and XML can only have one root node.

posted on 2008-08-19 16:57  ie421  阅读(1062)  评论(0编辑  收藏  举报