Out of the box Json.NET is faster than DataContractJsonSerializer and JavaScriptSerializer. Here are some tips to make it go even faster.

# Optimize Memory Usage

To keep an application consistantly fast it is important to minimize the amount of time the .NET framework spends performing garbage collection http://msdn.microsoft.com/en-us/library/ms973837.aspx _blank . Allocating too many objects, or allocating very large objects can slow down or even halt an application while garbage collection is in progress.

To minimize memory usage and the number of objects allocated Json.NET supports serializing and deserializing directly to a stream. Reading or writing JSON a piece at a time instead of having the entire JSON string loaded into memory is especially important when working with JSON documents greater than 85kb in size to avoid the JSON string ending up in the large object heap http://msdn.microsoft.com/en-us/magazine/cc534993.aspx _blank .

Deserialize String

Deserialize Stream

# JsonConverters

Passing a T:Newtonsoft.Json.JsonConverter to SerializeObject or DeserializeObject provides a simple way to completely change how an object is serialized. There is however a small overhead; the CanConvert method is called for every value to check whether serialization should be handled by that JsonConverter.

There are a couple of ways to continue to use JsonConverters without any overhead. The simplest way is to specify the JsonConverter using the T:Newtonsoft.Json.JsonConverterAttribute. This attribute tells the serializer to always use that converter when serializing and deserializing the type, without the check.

Use JsonConverter with JsonConverterAttribute

If the class you want to convert isn't your own and you're unable to use an attribute a JsonConverter can still be used by creating your own T:Newtonsoft.Json.Serialization.IContractResolver.

Use JsonConverter with IContractResolver

The IContractResolver in the example above will set all DateTimes to use the JavaScriptDateConverter.

# Manually Serialize

The absolute fastest way to read and write JSON is to use JsonTextReader/JsonTextWriter directly to manually serialize types. Using a reader or writer directly skips any of the overhead from a serializer such as reflection.

Manually serialize using JsonTextWriter

If performance is important and you don't mind more code to get it then this is your best choice. Read more about using JsonReader/JsonWriter here: Basic Reading and Writing JSON ReadingWritingJSON.aml _self

# Benchmarks

Json.NET Performance
T:Newtonsoft.Json.JsonSerializer
T:Newtonsoft.Json.JsonConverter
T:Newtonsoft.Json.JsonConverterAttribute
T:Newtonsoft.Json.JsonTextWriter
T:Newtonsoft.Json.JsonTextReader