Home > AJAX, ASP.NET > Enabling dual date format output for JSON.NET

Enabling dual date format output for JSON.NET

 

By default the JSON.Net library will emit all dates using the ISO Date format e.g "2012-05-09T00:00:00Z", but Microsoft’s JSON formatter for date will emit using Microsoft’s own format “new Date(1234656000000)”

 

Problem comes in when you have previously been using Microsoft’s JSON formatter and now want to use JSON.NET’s implementation. You will then have to decide whether you want to use the new format and change all your existing apps, or use the old format and make it slightly less interoperable.

 

One other alternative which I’m going to suggest below is to emit both date types, and suffix the other date property with a “_js” to differentiate between the 2 dates

image

 

To get this to work, just follow the steps below

  1. Download the latest JSON.NET library at http://json.codeplex.com/
  2. Open the solution and goto the Newtonsoft.Json project
    image
  3. Open JsonSerializerInternalWriter.cs
  4. Search for the following function
    private void WriteMemberInfoProperty(JsonWriter writer, object memberValue, JsonProperty property, JsonContract contract, JsonContainerContract collectionContract)

    This function is the primary function that writes out the JSON output based what you passed in to serialize

  5. The function should look something like
    image
  6. What we are targeting is the last 2 lines of the function i.e

    writer.WritePropertyName(propertyName);
    SerializeValue(writer, memberValue, contract, property, collectionContract, null);

  7. Change these 2 lines of code to

    //Ensure membervalue is not null and is a datetime type

  8. if (memberValue != null && memberValue.GetType() == typeof(DateTime))
    {
        //Default property uses Microsoft Date Format
        writer.WritePropertyName(propertyName);
        writer.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
        SerializeValue(writer, memberValue, contract, property, collectionContract, null);

        //Same property with a _js returns a ISO Date Format
        writer.WritePropertyName(propertyName + "_js");
        writer.DateFormatHandling = DateFormatHandling.IsoDateFormat;
        SerializeValue(writer, memberValue, contract, property, collectionContract, null);
    }
    else
    {
        //Original implementation 
        writer.WritePropertyName(propertyName);
        SerializeValue(writer, memberValue, contract, property, collectionContract, null);
    }

  9. Compile the project and copy the new json.net dll to your server
  10. Test and make sure you see both dates being emitted
Categories: AJAX, ASP.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: