using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Runtime.Serialization.Formatters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
namespace Question43032552.V1
public partial class AClass : ISomeBase
public AClass([JsonProperty("Genres")] IList<SysType> SysTypes, IList<ProductionCountry> production_countries, [JsonProperty("spoken_languages")] IList<SpokenLanguage> SpokenLanguages)
this.Genres = SysTypes == null ? null : SysTypes.Cast<IGenre>().ToList();
this.ProductionCountries = production_countries == null ? null : production_countries.Cast<IProductionCountry>().ToList();
this.SpokenLanguages = SpokenLanguages == null ? null : SpokenLanguages.Cast<ISpokenLanguage>().ToList();
public int Id { get; set; }
public IList<IGenre> Genres { get; set; }
[JsonProperty("production_countries")]
public IList<IProductionCountry> ProductionCountries { get; set; }
[JsonProperty("spoken_languages")]
public IList<ISpokenLanguage> SpokenLanguages { get; set; }
namespace Question43032552.V2
public partial class AClass : ISomeBase
public int Id { get; set; }
[JsonProperty(ItemConverterType = typeof(CustomCreationConverter<IGenre, SysType>))]
public IList<IGenre> Genres { get; set; }
[JsonProperty("production_countries", ItemConverterType = typeof(CustomCreationConverter<IProductionCountry, ProductionCountry>))]
public IList<IProductionCountry> ProductionCountries { get; set; }
[JsonProperty("spoken_languages", ItemConverterType = typeof(CustomCreationConverter<ISpokenLanguage, SpokenLanguage>))]
public IList<ISpokenLanguage> SpokenLanguages { get; set; }
public class CustomCreationConverter<T, TSerialized> : CustomCreationConverter<T> where TSerialized : T, new()
public override T Create(Type objectType)
return new TSerialized();
namespace Question43032552
public interface ISomeBase
public class SysType : IGenre
public interface IProductionCountry
public class ProductionCountry : IProductionCountry
public interface ISpokenLanguage
public class SpokenLanguage : ISpokenLanguage
public string Name { get; set; }
public class TestContractResolver : DefaultContractResolver
protected override JsonObjectContract CreateObjectContract(Type objectType)
var contract = base.CreateObjectContract(objectType);
public decimal Result { get; private set; }
public Sum(decimal first, decimal second)
this.Result = first + second;
public static void TestSum()
var json = @"{""first"": 1, ""second"" : 2}";
Console.WriteLine("Deserializing: " + json);
var sum = JsonConvert.DeserializeObject<Sum>(json);
var jsonResult = JsonConvert.SerializeObject(sum);
Console.WriteLine("Result: " + jsonResult + "\n");
public static void Test()
Genres = new List<IGenre>() { new SysType() },
SpokenLanguages = new List<ISpokenLanguage>() { new SpokenLanguage { Name = "Akkadian" } },
ProductionCountries = new List<IProductionCountry>() { new ProductionCountry() },
var settings = new JsonSerializerSettings()
ContractResolver = new TestContractResolver(),
var json = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
TestDeserialization<V1.AClass>(settings, json);
TestDeserialization<V2.AClass>(settings, json);
private static void TestDeserialization<TACLass>(JsonSerializerSettings settings, string json)
Console.WriteLine("Testing {0}", typeof(TACLass));
Console.WriteLine("Input JSON: ");
var root2 = JsonConvert.DeserializeObject<TACLass>(json, settings);
var json2 = JsonConvert.SerializeObject(root2, Formatting.Indented, settings);
Console.WriteLine("Output JSON: ");
Console.WriteLine(json2);
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(json), JToken.Parse(json2)));
Console.WriteLine("Input and output are equivalent.\n");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
throw new AssertionFailedException("failed");
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Unhandled exception: ");