using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Specialized;
using System.Web.Routing;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
[JsonConverter(typeof(OverridableJsonConverterDecorator), typeof(IsoDateTimeConverter))]
public DateTime datum = new DateTime(1232, 3, 23);
public class OverridableJsonConverterDecorator : JsonConverterDecorator
public OverridableJsonConverterDecorator(Type jsonConverterType) : base(jsonConverterType) { }
public OverridableJsonConverterDecorator(JsonConverter converter) : base(converter) { }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
foreach (var converter in serializer.Converters)
Debug.WriteLine("Skipping identical " + converter.ToString());
if (converter.CanConvert(value.GetType()) && converter.CanWrite)
converter.WriteJson(writer, value, serializer);
base.WriteJson(writer, value, serializer);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
foreach (var converter in serializer.Converters)
Debug.WriteLine("Skipping identical " + converter.ToString());
if (converter.CanConvert(objectType) && converter.CanRead)
return converter.ReadJson(reader, objectType, existingValue, serializer);
return base.ReadJson(reader, objectType, existingValue, serializer);
public abstract class JsonConverterDecorator : JsonConverter
readonly JsonConverter converter;
public JsonConverterDecorator(Type jsonConverterType) : this((JsonConverter)Activator.CreateInstance(jsonConverterType)) { }
public JsonConverterDecorator(JsonConverter converter)
throw new ArgumentNullException();
this.converter = converter;
public override bool CanConvert(Type objectType)
return converter.CanConvert(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
return converter.ReadJson(reader, objectType, existingValue, serializer);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
converter.WriteJson(writer, value, serializer);
public override bool CanRead { get { return converter.CanRead; } }
public override bool CanWrite { get { return converter.CanWrite; } }
internal static void Test()
var dtc = new IsoDateTimeConverter();
dtc.DateTimeFormat = "yy";
var json1 = JsonConvert.SerializeObject(new x(), dtc);
Console.WriteLine("JSON with added converter: ");
Console.WriteLine(json1);
var json2 = JsonConvert.SerializeObject(new x());
Console.WriteLine("JSON without added converter: ");
Console.WriteLine(json2);
public static void Main()
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);