Json.NET - Quick Starts & API Documentation

Introduction - Quick Starts & API Documentation

makes working with JSON formatted data in .NET simple. Quickly read and write JSON using LINQ to JSON or serialize your .NET objects with a single method call using the JsonSerializer.

Features

Flexible JSON serializer to convert .NET objects to JSON and back again LINQ to JSON for reading and writing JSON Writes indented, easy to read JSON Convert JSON to and from XML Supports Silverlight and the Compact Framework

The JSON serializer is a good choice when the JSON you are reading or writing maps closely to a .NET class. The serializer automatically reads and writes JSON for the class.

For situations where you are only interested in getting values from JSON, don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects then LINQ to JSON is what you should use. LINQ to JSON allows you to easily read, create and modify JSON in .NET.

History

grew out of projects I was working on in late 2005 involving JavaScript, AJAX and .NET. At the time there were no libraries for working with JavaScript in .NET so I began to grow my own.

Starting out as a couple of static methods for escaping JavaScript strings, evolved as features were added. To add support for reading JSON a major refactor was required and will split into the three major classes it still uses today, JsonReader, JsonWriter and JsonSerializer.

was first released in June 2006. Since then has been downloaded thousands of times by developers and is used in a number of major projects open source projects including MonoRail, Castle Project's MVC web framework, and Mono, an open source implementation of the .NET framework.

~ James Newton-King

Donate

is a free open source project that I have developed in my personal time.

I really appreciate your feedback and support for and its future development.

Serializing and - Quick Starts & API Documentation 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 where you want to convert to and from a JSON string the SerializeObject and DeserializeObject methods on JsonConvert 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"

string output = JsonConvert.SerializeObject(product); //{ // "Name": "Apple", // "Expiry": "\/Date(1230375600000+1300)\/", // "Price": 3.99, // "Sizes": [ // "Small", // "Medium", // "Large" // ] //}

Product deserializedProduct = JsonConvert.Deserialize

SerializeObject and DeserializeObject both have overloads that take a JsonSerializerSettings object. JsonSerializerSettings lets you use many of the JsonSerializer settings listed below while still using the simple serialization methods.

JsonSerializer

For more control over how an object is serialized the JsonSerializer can be used directly. The JsonSerializer is able to read and write JSON text directly to a stream via JsonTextReader and JsonTextWriter. Other kinds of JsonWriters can also be used such as JTokenReader/JTokenWriter to convert your object to and from LINQ to JSON objects or BsonReader/BsonWriter to convert to and from BSON.

Product product = new Product(); product.Expiry = new DateTime(2008, 12, 28);

JsonSerializer serializer = new JsonSerializer serializer.Converters.Add(new JavaScriptDateTimeConve serializer.NullValueHandling = NullValueHandling

using (StreamWriter sw = new StreamWriter(@"c:\json.t using (JsonWriter writer = new JsonTextWriter(sw)) {

serializer.Serialize(writer, product); // {"Expiry":new Date(1230375600000),"Price":0} }

JsonSerializer has a number of properties on it to customize how it serializes JSON. These can also be used with the methods on JsonConvert via the JsonSerializerSettings overloads.

Read more about the available JsonSerializer settings here:

Serialization Settings

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download