using System.Collections.Generic;
using System.Diagnostics;
using System.Collections;
using System.Runtime.Serialization;
using System.ComponentModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Bson;
public string AProperty { get; set; }
public List<int> Values { get; set; }
public static void Test()
var test = new TestClass { AProperty = "A Property", Values = new List<int> { 1, 2, 3 } };
Test<ExpandoObject>(test);
static void Test<TDynamic>(TestClass test)
using (var stream = new MemoryStream())
using (var writer = new BsonWriter(stream))
var serializer = JsonSerializer.CreateDefault();
serializer.Serialize(writer, test);
byteArray = stream.ToArray();
using (var stream = new MemoryStream(byteArray))
using (var reader = new BsonReader(stream))
var serializer = JsonSerializer.CreateDefault();
result = serializer.Deserialize<TDynamic>(reader);
Console.WriteLine(string.Format("Deserialized and re-serialized {0} result, of actual type {1}:", typeof(TDynamic), result.GetType()));
Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
if (result.AProperty != test.AProperty)
throw new InvalidOperationException("obj.AProperty != test.AProperty");
for (int i = 0; i < test.Values.Count; i++)
var resultValue = (int)result.Values[i];
if (test.Values[i] != resultValue)
throw new InvalidOperationException("test.Values[i] != resultValue");
public static void Main()
Console.WriteLine(string.Format("Json.NET version: {0}.\n", typeof(JsonSerializer).Assembly.FullName));