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 partial class Welcome
[JsonConverter(typeof(JsonSingleOrEmptyArrayConverter<Dictionary<long, Result[]>>))]
public Dictionary<long, Result[]> Results { get; set; }
public partial class Result
public long Id { get; set; }
public string Name { get; set; }
public static void Test()
foreach (var json in GetJson())
var root = JsonConvert.DeserializeObject<Welcome>(json);
Console.WriteLine(JsonConvert.SerializeObject(root, Formatting.Indented));
static IEnumerable<string> GetJson()
var json = new [] { @"{""results"": {
public class JsonSingleOrEmptyArrayConverter<T> : JsonConverter where T : class
public override bool CanConvert(Type objectType)
return typeof(T).IsAssignableFrom(objectType);
public override bool CanWrite { get { return false; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
var contract = serializer.ContractResolver.ResolveContract(objectType);
if (!(contract is Newtonsoft.Json.Serialization.JsonObjectContract || contract is Newtonsoft.Json.Serialization.JsonDictionaryContract))
throw new JsonSerializationException(string.Format("Unsupported objectType {0} at {1}.", objectType, reader.Path));
switch (reader.SkipComments().TokenType)
case JsonToken.StartArray:
switch (reader.TokenType)
throw new JsonSerializationException(string.Format("Too many objects at path {0}.", reader.Path));
existingValue = existingValue ?? contract.DefaultCreator();
serializer.Populate(reader, existingValue);
throw new JsonSerializationException(string.Format("Unclosed array at path {0}.", reader.Path));
case JsonToken.StartObject:
existingValue = existingValue ?? contract.DefaultCreator();
serializer.Populate(reader, existingValue);
throw new InvalidOperationException("Unexpected token type " + reader.TokenType.ToString());
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public static partial class JsonExtensions
public static JsonReader SkipComments(this JsonReader reader)
while (reader.TokenType == JsonToken.Comment && reader.Read())
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: ");