using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
[JsonConverter(typeof(ArrayListConverter<string []>))]
public ArrayList Items { get; private set; }
this.Items = new ArrayList();
public static void Main()
var myObject = new MyTestClass();
myObject.Items.Add(new string[] { "Test1", "Test2" });
var jsonResult = JsonConvert.SerializeObject(myObject);
Console.WriteLine("Initial JSON: ");
Console.WriteLine(jsonResult);
var tempMyObject = JsonConvert.DeserializeObject<MyTestClass>(jsonResult);
var reserializedJson = JsonConvert.SerializeObject(tempMyObject,
new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.All });
Console.WriteLine("Reserialized JSON: ");
Console.WriteLine(reserializedJson);
Assert.Equal((myObject.Items[0] as string[])[0], (tempMyObject.Items[0] as string[])[0]);
public class ArrayListConverter<TItem> : JsonConverter
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.TokenType == JsonToken.Null)
var list = serializer.Deserialize<List<TItem>>(reader);
var arrayList = existingValue as ArrayList ?? new ArrayList(list.Count);
arrayList.AddRange(list);
public override bool CanConvert(Type objectType)
return objectType == typeof(ArrayList);
public static class Assert
public static void Equal<T>(T expected, T actual)
if (null != actual && !actual.Equals(expected))
throw new InvalidOperationException("Assert Failed");
else if (!expected.Equals(actual))
throw new InvalidOperationException("Assert Failed");