using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public List<Variant> variants { get; set; } = new ();
public string name { get; set; }
public string description { get; set; }
[JsonConverter(typeof(JsonSingleOrEmptyArrayConverter<Dictionary<string, List<Image>>>))]
public Dictionary<string, List<Image>> images { get; set; } = new ();
public string id { get; set; }
public string product_id { get; set; }
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 Test()
Console.WriteLine("Input JSON:");
Console.WriteLine(JToken.Parse(GetJson()));
var root = JsonConvert.DeserializeObject<TestClass>(json);
var json2 = JsonConvert.SerializeObject(root, Formatting.Indented);
foreach (var variant in root.variants)
Console.WriteLine($"Images for {variant.name}:");
foreach (var pair in variant.images)
Console.WriteLine($" For color {pair.Key}:");
foreach (var image in pair.Value)
Console.WriteLine($" id = {image.id}");
Console.WriteLine("Re-serialized {0}:", root);
Console.WriteLine(json2);
static string GetJson() =>
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");