using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public string a { get; set; }
public string b { get; set; }
public string z { get; set; }
[JsonConverter(typeof(ListToDictionaryConverter<MyObject>))]
public List<MyObject> propertyNames { get; set; }
public string otherProperty { get; set; }
public List<Datum> data { get; set; }
public class ListToDictionaryConverter<T> : JsonConverter where T : class
public override bool CanConvert(Type objectType)
return typeof(List<T>).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.TokenType == JsonToken.Null)
var list = existingValue as List<T> ?? (List<T>)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
if (reader.TokenType == JsonToken.StartArray)
serializer.Populate(reader, list);
else if (reader.TokenType == JsonToken.StartObject)
var dict = serializer.Deserialize<Dictionary<int, T>>(reader);
foreach (var pair in dict)
list.SetOrAddAt(pair.Key, pair.Value, default(T));
throw new JsonSerializationException(string.Format("Invalid token {0}", reader.TokenType));
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
var list = (IList<T>)value;
writer.WriteStartObject();
for (int i = 0; i < list.Count; i++)
if (list[i] == default(T))
writer.WritePropertyName(((JValue)i).ToString());
serializer.Serialize(writer, list[i]);
public static class ListExtensions
public static void SetOrAddAt<T>(this IList<T> list, int index, T value, T defaultValue = default(T))
throw new ArgumentNullException("list");
list.EnsureCount(index + 1, defaultValue);
public static void EnsureCount<T>(this IList<T> list, int count, T defaultValue = default(T))
throw new ArgumentNullException("list");
int oldCount = list.Count;
for (int i = oldCount; i < count; i++)
public static void Test()
var root = JsonConvert.DeserializeObject<RootObject>(json);
var json2 = JsonConvert.SerializeObject(root, Formatting.Indented);
Console.WriteLine("Re-serialized {0}", root);
Console.WriteLine(json2);
""otherProperty"": ""abc""
""otherProperty"": ""bce""
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];