Home > ASP.NET > Enabling dual date format output for JSON.NET for .NET v 4.5

Enabling dual date format output for JSON.NET for .NET v 4.5

Adding on to https://jefferytay.wordpress.com/2012/05/09/enabling-dual-date-format-output-for-json-net/ 

 

You will find that the code segment is no longer found in JSON.NET for .NET v4.5. This is because things have been changed between .NET 4 and .Net4.5

 

You now need to find the function called SerializeObject

image

private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
    {
      contract.InvokeOnSerializing(value, Serializer.Context);

      _serializeStack.Add(value);

      WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);

      int initialDepth = writer.Top;

      foreach (JsonProperty property in contract.Properties)
      {
        try
        {
          object memberValue;
          JsonContract memberContract;

          if (!CalculatePropertyValues(writer, value, contract, member, property, out memberContract, out memberValue))
            continue;

          writer.WritePropertyName(property.PropertyName);
          SerializeValue(writer, memberValue, memberContract, property, contract, member);
        }
        catch (Exception ex)
        {
          if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex))
            HandleError(writer, initialDepth);
          else
            throw;
        }
      }

      writer.WriteEndObject();

      _serializeStack.RemoveAt(_serializeStack.Count – 1);

      contract.InvokeOnSerialized(value, Serializer.Context);
    }

Again we are looking at the code in bold, replace it with

if (memberValue != null && memberValue.GetType() == typeof(DateTime))
         {
             writer.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
             writer.WritePropertyName(property.PropertyName);
             SerializeValue(writer, memberValue, memberContract, property, contract, member);

             writer.DateFormatHandling = DateFormatHandling.IsoDateFormat;
             writer.WritePropertyName(property.PropertyName+"_js");
             SerializeValue(writer, memberValue, memberContract, property, contract, member);
         }
         else
         {
             writer.WritePropertyName(property.PropertyName);
             SerializeValue(writer, memberValue, memberContract, property, contract, member);
         }

Advertisements
Categories: ASP.NET
  1. No comments yet.
  1. No trackbacks yet.

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: