Archives / 2008 / October
  • Json.NET 3.5 Beta 1 – Big performance improvements, Compact Framework support and more

    I am starting to get organised towards releasing Json.NET 3.5. There are a lot of good new features coming in this version, almost all of them driven by user requests.

    Compact Framework Support

    Json.NET 3.5 includes a build for the Compact Framework 3.5. It supports all the major features of Json.NET.

    Performance Improvements

    A lot of features have been added to Json.NET over the past couple of years and it has been a long time since I had done any performance tuning.

    I busted out a profiler and found some big gains in the JsonSerializer by doing some basic caching of type data. I managed to eek out a 400% increase in performance. Not bad for 20 minutes work.

    Below is a comparison of Json.NET 3.0, Json.NET 3.5 Beta 1, the WCF DataContractJsonSerializer and the JavaScriptSerializer. The code for the results is in the PerformanceTests class and is over 5000 iterations.

    Okay Marge, its your child against my child. The winner will be showered with praise. The loser will be taunted and booed until my throat is sore.


    There are a lot of settings on JsonSerializer but only a few of them are exposed through the JavaScriptConvert.SerializeObject/DeserializeObject helper methods. To make those options available through the easy to use helper methods I have added the JsonSerializerSettings class along with overloads using it to the helper methods.

    public class Invoice
      public string Company { get; set; }
      public decimal Amount { get; set; }
      public bool Paid { get; set; }
      public DateTime? PaidDate { get; set; }

    This example of the JsonSerializerSettings also shows another new feature of Json.NET 3.5: DefaultValueHandling. If the property value is the same as the default value then those values will be skipped when writing JSON.

    public void SerializeInvoice()
      Invoice invoice = new Invoice
                          Company = "Acme Ltd.",
                          Amount = 50.0m,
                          Paid = false
      string json = JavaScriptConvert.SerializeObject(invoice);
      // {"Company":"Acme Ltd.","Amount":50.0,"Paid":false,"PaidDate":null}
      json = JavaScriptConvert.SerializeObject(invoice,
        new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
      // {"Company":"Acme Ltd.","Amount":50.0}

    Writing Raw JSON

    For a long time JsonWriter has had a WriteRaw method. I can proudly say that now it actually works. Serializing raw JSON is also much improved with the new JsonRaw object.


    Here is a complete list of what has changed.

    • New feature - Compact framework support
    • New feature - Big serializer performance improvements through caching of static type data
    • New feature - Added DefaultValueHandling option to JsonSerializer
    • New feature - JsonSerializer better supports deserializing into ICollection<T> objects
    • New feature - Added JsonSerializerSettings class along with overloads to JavaScriptConvert serialize/deserialize methods
    • New feature - IsoDateTimeConverter and JavaScriptDateTimeConverter now support nullable DateTimes
    • New feature - Added JsonWriter.WriteValue overloads for nullable types
    • New feature - Newtonsoft.Json.dll is now signed
    • New feature - Much better support for reading, writing and serializing raw JSON
    • New feature - Added JsonWriter.WriteRawValue
    • Change - Renamed Identifier to JsonRaw
    • Change - JSON date constructors deserialize to a date
    • Fix - JavaScriptConvert.DeserializeObject checks for addition content after deserializing an object
    • Fix - Changed JsonSerializer.Deserialize to take a TextReader instead of a StringReader
    • Fix - Changed JsonTextWriter.WriteValue(string) to write null instead of an empty string for a null value
    • Fix - JsonWriter.WriteValue(object) no longer errors on a null value
    • Fix - Corrected JContainer child ordering when adding multi values


    Json.NET CodePlex Project

    Json.NET 3.5 Beta 1 Download – Json.NET source code, documentation and binaries

    kick it on

  • Speeding up ASP.NET with the Runtime Page Optimizer

    I am sick of running away. Did 'brave heart' run away? Did 'payback' run away?

    Earlier this year I worked on an exciting product called the Runtime Page Optimizer (RPO). Originally written by the guys at ActionThis to solve their website’s performance issues, I had the opportunity to develop it with them and help turn the RPO into a product that works on any ASP.NET website.

    How RPO works

    At its heart the RPO is a HttpModule that intercepts page content at runtime, inspects it, and then rewrites the page so that is optimized to be downloaded to the client. Because the RPO is a module it can quickly be added to any existing ASP.NET website, creating instant performance improvements.

    What RPO optimizes

    A gun is not a weapon Marge, it's a tool. Like a butcher knife, or a harpoon, or... or an alligator.The RPO does three things to speed up a web page:

    • Reduces HTTP requests  Reducing the number of resources on a page often produces the most dramatic improvement in performance. A modest sized web page can still take a significant amount of time to load if it contains many stylesheets, scripts and image files. The reason for this is most of the time spent waiting for a web page to load is not for large files to download but from HTTP requests bouncing between the browser and the server. The RPO fixes this by intelligently combining CSS and JavaScript text files together and merging images through CSS spriting. Fewer resources means less time wasted from HTTP requests and faster page loads.
    • Compresses Content RPO minifies (whitespace removal) and zip compresses JavaScript and CSS files, as well as the ASP.NET page itself. Compression reduces page size, saves bandwidth and further decreasing page load times.
    • Caching The RPO ensures all static content has the correct HTTP headers to be cached on a user’s browser. This further decreases "warm" page load times and saves even more bandwidth. When resources change on the server, the client-side cache is automatically refreshed.

    If you really want something in life you have to work for it. Now quiet, they're about to announce the lottery numbers.Other stuff

    • Every site is different so the RPO has extensive configuration options to customize it to best optimize your ASP.NET application. RPO has been used on ASP.NET AJAX, MVC, SharePoint, CRM, EpiServer and DotNetNuke websites.
    • Lots of thought has been put into performance. The RPO caches all combined content and supports load balancing. Once combined content has been cached the only thing that happens at runtime is lightweight parsing of HTML, which is darn quick. I know because I wrote it that way [:)]
    • The RPO supports all browsers, including new kid on the block: Chrome. RPO is even smart enough to take advantage of features only available in certain browsers and supply specially customized content to further improve page load times.
    • The industrious people at RPO are making a version for Apache.

    Working on the RPO was a great experience. Some very smart people are behind it and everyone contributed something unique to make RPO the awesome tool that it is today.

    RPO is out now and a fully featured trial is available at Check it out!

    kick it on


    I noticed there is a question about the RPO on StackOverflow (I've been using StackOverflow a lot lately, excellent resource). The question discussion has some positive comments from users who have tried the RPO out.

  • ASP.NET MVC and Json.NET

    This is an ActionResult I wrote to return JSON from ASP.NET MVC to the browser using Json.NET.

    The benefit of using JsonNetResult over the built in JsonResult is you get a better serializer (IMO [:)]) and all the other benefits of Json.NET like nicely formatted JSON text.

    public class JsonNetResult : ActionResult
      public Encoding ContentEncoding { get; set; }
      public string ContentType { get; set; }
      public object Data { get; set; }
      public JsonSerializerSettings SerializerSettings { get; set; }
      public Formatting Formatting { get; set; }
      public JsonNetResult()
        SerializerSettings = new JsonSerializerSettings();
      public override void ExecuteResult(ControllerContext context)
        if (context == null)
          throw new ArgumentNullException("context");
        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = !string.IsNullOrEmpty(ContentType)
          ? ContentType
          : "application/json";
        if (ContentEncoding != null)
          response.ContentEncoding = ContentEncoding;
        if (Data != null)
          JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
          JsonSerializer serializer = JsonSerializer.Create(SerializerSettings);
          serializer.Serialize(writer, Data);

    Using JsonNetResult within your application is pretty simple. The example below serializes the NumberFormatInfo settings for the .NET invariant culture.

    public ActionResult GetNumberFormatting()
      JsonNetResult jsonNetResult = new JsonNetResult();
      jsonNetResult.Formatting = Formatting.Indented;
      jsonNetResult.Data = CultureInfo.InvariantCulture.NumberFormat;
      return jsonNetResult;

    And here is the nicely formatted result.

      "CurrencyDecimalDigits": 2,
      "CurrencyDecimalSeparator": ".",
      "IsReadOnly": true,
      "CurrencyGroupSizes": [
      "NumberGroupSizes": [
      "PercentGroupSizes": [
      "CurrencyGroupSeparator": ",",
      "CurrencySymbol": "¤",
      "NaNSymbol": "NaN",
      "CurrencyNegativePattern": 0,
      "NumberNegativePattern": 1,
      "PercentPositivePattern": 0,
      "PercentNegativePattern": 0,
      "NegativeInfinitySymbol": "-Infinity",
      "NegativeSign": "-",
      "NumberDecimalDigits": 2,
      "NumberDecimalSeparator": ".",
      "NumberGroupSeparator": ",",
      "CurrencyPositivePattern": 0,
      "PositiveInfinitySymbol": "Infinity",
      "PositiveSign": "+",
      "PercentDecimalDigits": 2,
      "PercentDecimalSeparator": ".",
      "PercentGroupSeparator": ",",
      "PercentSymbol": "%",
      "PerMilleSymbol": "‰",
      "NativeDigits": [
      "DigitSubstitution": 1

    Download source

    kick it on