using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.Specialized;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static void Test()
HttpCookie MyCookie = new HttpCookie(null);
MyCookie.Name = "Cookie1";
MyCookie.Values["Val1"] = "1";
MyCookie.Values["Val2"] = "2";
MyCookie.Values["Val3"] = "3";
var settings = new JsonSerializerSettings
Converters = { new NameValueJsonConverter<NameValueCollection>(), new HttpCookieConverter() }
var json = JsonConvert.SerializeObject(MyCookie, Formatting.Indented, settings);
System.Threading.Thread.Sleep(10);
var cookie2 = new HttpCookie(null);
cookie2.Value = "Val1=1&Val2=2&Val3=3";
var json2 = JsonConvert.SerializeObject(cookie2, Formatting.Indented, settings);
Console.WriteLine(json2);
cookie2.Value = "Val1=1&Val2=2&Val3=3";
""Expires"": ""0001-01-01T00:00:00"",
//""Value"": ""Val1=1&Val2=2&Val3=3"",
public class HttpCookieConverter : CustomCreationConverter<HttpCookie>
public override HttpCookie Create(Type objectType)
Console.WriteLine("Called.");
return new HttpCookie(null);
public class NameValueCollectionDictionaryAdapter<TNameValueCollection> : IDictionary<string, string[]>
where TNameValueCollection : NameValueCollection, new()
readonly TNameValueCollection collection;
public NameValueCollectionDictionaryAdapter() : this(new TNameValueCollection()) { }
public NameValueCollectionDictionaryAdapter(TNameValueCollection collection)
this.collection = collection;
public TNameValueCollection GetCollection() { return collection; }
#region IDictionary<string,string[]> Members
public void Add(string key, string[] value)
if (collection.GetValues(key) != null)
throw new ArgumentException("Duplicate key " + key);
collection.Add(key, null);
foreach (var str in value)
collection.Add(key, str);
public bool ContainsKey(string key) { return collection.GetValues(key) != null; }
public ICollection<string> Keys { get { return collection.AllKeys; } }
public bool Remove(string key)
bool found = ContainsKey(key);
public bool TryGetValue(string key, out string[] value)
return (value = collection.GetValues(key)) != null;
public ICollection<string[]> Values
return new ReadOnlyCollectionAdapter<KeyValuePair<string, string[]>, string[]>(this, p => p.Value);
public string[] this[string key]
var value = collection.GetValues(key);
throw new KeyNotFoundException(key);
#region ICollection<KeyValuePair<string,string[]>> Members
public void Add(KeyValuePair<string, string[]> item) { Add(item.Key, item.Value); }
public void Clear() { collection.Clear(); }
public bool Contains(KeyValuePair<string, string[]> item)
if (!TryGetValue(item.Key, out value))
return EqualityComparer<string[]>.Default.Equals(item.Value, value);
public void CopyTo(KeyValuePair<string, string[]>[] array, int arrayIndex)
foreach (var item in this)
array[arrayIndex++] = item;
public int Count { get { return collection.Count; } }
public bool IsReadOnly { get { return false; } }
public bool Remove(KeyValuePair<string, string[]> item)
#region IEnumerable<KeyValuePair<string,string[]>> Members
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
foreach (string key in collection)
yield return new KeyValuePair<string, string[]>(key, collection.GetValues(key));
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
public static class NameValueCollectionExtensions
public static NameValueCollectionDictionaryAdapter<TNameValueCollection> ToDictionaryAdapter<TNameValueCollection>(this TNameValueCollection collection)
where TNameValueCollection : NameValueCollection, new()
throw new ArgumentNullException();
return new NameValueCollectionDictionaryAdapter<TNameValueCollection>(collection);
public class ReadOnlyCollectionAdapter<TIn, TOut> : CollectionAdapterBase<TIn, TOut, ICollection<TIn>>
public ReadOnlyCollectionAdapter(ICollection<TIn> collection, Func<TIn, TOut> toOuter)
: base(() => collection, toOuter)
public override void Add(TOut item) { throw new NotImplementedException(); }
public override void Clear() { throw new NotImplementedException(); }
public override bool IsReadOnly { get { return true; } }
public override bool Remove(TOut item) { throw new NotImplementedException(); }
public abstract class CollectionAdapterBase<TIn, TOut, TCollection> : ICollection<TOut>
where TCollection : ICollection<TIn>
readonly Func<TCollection> getCollection;
readonly Func<TIn, TOut> toOuter;
public CollectionAdapterBase(Func<TCollection> getCollection, Func<TIn, TOut> toOuter)
if (getCollection == null || toOuter == null)
throw new ArgumentNullException();
this.getCollection = getCollection;
protected TCollection Collection { get { return getCollection(); } }
protected TOut ToOuter(TIn inner) { return toOuter(inner); }
#region ICollection<TOut> Members
public abstract void Add(TOut item);
public abstract void Clear();
public virtual bool Contains(TOut item)
var comparer = EqualityComparer<TOut>.Default;
foreach (var member in Collection)
if (comparer.Equals(item, ToOuter(member)))
public void CopyTo(TOut[] array, int arrayIndex)
foreach (var item in this)
array[arrayIndex++] = item;
public int Count { get { return Collection.Count; } }
public abstract bool IsReadOnly { get; }
public abstract bool Remove(TOut item);
#region IEnumerable<TOut> Members
public IEnumerator<TOut> GetEnumerator()
foreach (var item in Collection)
yield return ToOuter(item);
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public class NameValueJsonConverter<TNameValueCollection> : JsonConverter
where TNameValueCollection : NameValueCollection, new()
public override bool CanConvert(Type objectType)
return typeof(TNameValueCollection).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.SkipComments().TokenType == JsonToken.Null)
var collection = (TNameValueCollection)existingValue ?? new TNameValueCollection();
var dictionaryWrapper = collection.ToDictionaryAdapter();
if (reader.TokenType != JsonToken.StartObject)
serializer.Populate(reader, dictionaryWrapper);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
var collection = (TNameValueCollection)value;
var dictionaryWrapper = new NameValueCollectionDictionaryAdapter<TNameValueCollection>(collection);
serializer.Serialize(writer, dictionaryWrapper);
public static partial class JsonExtensions
public static JsonReader SkipComments(this JsonReader reader)
while (reader.TokenType == JsonToken.Comment && reader.Read())
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");