using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
class ListCollection : List<int>
[JsonConverter(typeof(PrivateListCollectionConverter))]
class PrivateListCollection: ISerializable
private List<int> list = new List<int>();
public void Add(int value)
public void GetObjectData(SerializationInfo info, StreamingContext context)
info.AddValue("items", list);
class PrivateListCollectionConverter : JsonConverter<PrivateListCollection>
const string itemsName = "items";
public override PrivateListCollection ReadJson(JsonReader reader, Type objectType, PrivateListCollection existingValue, bool hasExistingValue, JsonSerializer serializer)
var list = serializer.Deserialize<List<int>>(reader);
existingValue = existingValue ?? new PrivateListCollection();
foreach (var item in list)
public override void WriteJson(JsonWriter writer, PrivateListCollection value, JsonSerializer serializer)
ISerializable serializable = value;
var info = new SerializationInfo(value.GetType(), new FormatterConverter());
serializable.GetObjectData(info, serializer.Context);
var list = info.GetValue(itemsName, typeof(IEnumerable<int>));
serializer.Serialize(writer, list);
public static void Test()
var list1 = new ListCollection { 1, 2, 3 };
var list2 = new PrivateListCollection();
foreach (var item in list1)
var settings = new JsonSerializerSettings
Converters = { new PrivateListCollectionConverter() },
var json1 = JsonConvert.SerializeObject(list1, settings);
var json2 = JsonConvert.SerializeObject(list2, settings);
Console.WriteLine(json2);
Assert.AreEqual(json1, json2);
var list22 = JsonConvert.DeserializeObject<PrivateListCollection>(json1, settings);
var json22 = JsonConvert.SerializeObject(list22, settings);
Assert.AreEqual(json2, json22);
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.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];