using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
[JsonConverter(typeof(ValuesConverter))]
public int BestValue { get; set; }
public List<string> AllValues { get; set; }
internal class ValuesConverter : JsonConverter
public override bool CanConvert(Type objectType)
return typeof(Values).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
var tokenType = reader.SkipComments().TokenType;
if (tokenType == JsonToken.Null)
var value = existingValue as Values ?? (Values)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
if (tokenType == JsonToken.Date)
value.AllValues = new List<string> { JToken.Load(reader).ToString(Formatting.None).Trim('"') };
else if (tokenType.IsPrimitive())
value.AllValues = new List<string> { (string)JToken.Load(reader) };
serializer.Populate(reader, value);
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public static partial class JsonExtensions
public static JsonReader SkipComments(this JsonReader reader)
while (reader.TokenType == JsonToken.Comment && reader.Read())
public static bool IsPrimitive(this JsonToken tokenType)
case JsonToken.Undefined:
public class ValuesSubclass : Values
public string SomeOtherProperty { get; set; }
public static void Test()
Console.WriteLine("Input JSON: ");
Test<ValuesSubclass>(json);
public static void Test<TValues>(string json) where TValues : Values
Console.WriteLine("\nTesting List<{0}>: ", typeof(TValues));
var list = JsonConvert.DeserializeObject<List<TValues>>(json);
var json2 = JsonConvert.SerializeObject(list, Formatting.Indented);
Console.WriteLine("Re-serialized JSON: ");
Console.WriteLine(json2);
Assert.IsTrue(list.Count == JArray.Parse(json).Where(t => t.Type != JTokenType.Comment).Count());
""2009-02-15T00:00:00Z"" /* Make sure dates work */,
""MyFirstValue"" /* if only one value */,
{ ""BestValue"" : 0, ""AllValues"" : [""DefaultValue"", ""OtherValue""] },
null /* check that null works */,
""c75d06a8-a705-48ec-b6b3-9076becf20f4"" /* Make sure GUIDs work */
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: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");