using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public Note Notes { get; set; }
[JsonConverter(typeof(DictionaryToDictionaryListConverter<string, Source>))]
public Dictionary<string, Source> Data { get; set; }
public IList<Item> Items { get; set; }
public string Page { get; set; }
public string Comment { get; set; }
public class DictionaryToDictionaryListConverter<TKey, TValue> : JsonConverter
bool Disabled { get { return disabled; } set { disabled = value; } }
public override bool CanWrite { get { return !Disabled; } }
public override bool CanRead { get { return !Disabled; } }
public override bool CanConvert(Type objectType)
return typeof(IDictionary<TKey, TValue>).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.TokenType == JsonToken.Null)
var token = JToken.Load(reader);
var dict = (IDictionary<TKey, TValue>)(existingValue as IDictionary<TKey, TValue> ?? serializer.ContractResolver.ResolveContract(objectType).DefaultCreator());
if (token.Type == JTokenType.Array)
foreach (var item in token)
using (var subReader = item.CreateReader())
serializer.Populate(subReader, dict);
else if (token.Type == JTokenType.Object)
using (var subReader = token.CreateReader())
serializer.Populate(subReader, dict);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
var dict = (IDictionary<TKey, TValue>)value;
using (new PushValue<bool>(true, () => Disabled, val => Disabled = val))
serializer.Serialize(writer, dict.Select(p => new[] { p }.ToDictionary(p2 => p2.Key, p2 => p2.Value)));
public struct PushValue<T> : IDisposable
public PushValue(T value, Func<T> getValue, Action<T> setValue)
if (getValue == null || setValue == null)
throw new ArgumentNullException();
this.setValue = setValue;
this.oldValue = getValue();
public static void Test()
var root = JsonConvert.DeserializeObject<TestParse>(json);
var settings = new JsonSerializerSettings
ContractResolver = new CamelCasePropertyNamesContractResolver(),
var json2 = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
Console.WriteLine("De-serialized and re-serialized {0}", root);
Console.WriteLine(json2);
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(json), JToken.Parse(json2)));
""comment"": ""Some notes""
""comment"": ""Some notes""
""comment"": ""Some notes""
""comment"": ""Some notes""
""comment"": ""Some notes""
""comment"": ""Some notes""
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");