using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Specialized;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public interface ISubStuff
string Item { get; set; }
public class SubStuff : ISubStuff
public string Item { get; set; }
public interface IMainStuff
Dictionary<string, ISubStuff> SubStuff { get; set; }
public class MainStuff : IMainStuff
[JsonProperty(ItemConverterType = typeof(TypeConverter<SubStuff>))]
public Dictionary<string, ISubStuff> SubStuff
public class TypeConverter<T> : JsonConverter
public override bool CanConvert(Type objectType)
var msg = string.Format("This converter should be applied directly with [JsonProperty(ItemConverterType = typeof(TypeConverter<{0}>))] or [JsonProperty(typeof(TypeConverter<{0}>))]",
throw new NotImplementedException(msg);
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
return serializer.Deserialize<T>(reader);
SubStuff = new Dictionary<string, ISubStuff>()
{ "key1", new SubStuff { Item = "hello 1" } },
{ "key2", new SubStuff { Item = "hello 2" } },
var json1 = JsonConvert.SerializeObject(root);
Console.WriteLine(json1);
var root2 = JsonConvert.DeserializeObject<MainStuff>(json1);
var json2 = JsonConvert.SerializeObject(root2);
Console.WriteLine(json2);
public static void Main()
public class UnrelatedSubStuff : ISubStuff
public string Item { get; set; }