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;
public class JsonSingleOrEmptyArrayConverter<T> : JsonConverter where T : class
public override bool CanConvert(Type objectType)
return typeof(T).IsAssignableFrom(objectType);
public override bool CanWrite { get { return false; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
var contract = serializer.ContractResolver.ResolveContract(objectType);
if (!(contract is Newtonsoft.Json.Serialization.JsonObjectContract || contract is Newtonsoft.Json.Serialization.JsonDictionaryContract))
throw new JsonSerializationException(string.Format("Unsupported objectType {0} at {1}.", objectType, reader.Path));
switch (reader.SkipComments().TokenType)
case JsonToken.StartArray:
switch (reader.TokenType)
throw new JsonSerializationException(string.Format("Too many objects at path {0}.", reader.Path));
existingValue = existingValue ?? contract.DefaultCreator();
serializer.Populate(reader, existingValue);
throw new JsonSerializationException(string.Format("Unclosed array at path {0}.", reader.Path));
case JsonToken.StartObject:
existingValue = existingValue ?? contract.DefaultCreator();
serializer.Populate(reader, existingValue);
throw new InvalidOperationException("Unexpected token type " + reader.TokenType.ToString());
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())
[JsonConverter(typeof(JsonSingleOrEmptyArrayConverter<Personal>))]
public Personal personal { get; set; }
public byte political { get; set; }
public string[] langs { get; set; }
public string religion { get; set; }
public string inspired_by { get; set; }
public byte people_main { get; set; }
public byte life_main { get; set; }
public byte smoking { get; set; }
public byte alcohol { get; set; }
public static void Test()
string json0 = @"{ personal: null }";
string json1 = @"{ personal: [] }";
personal: [/* Some Comment */{
}/* Some Comment */] //when not empty.
Test(json0, p => Assert.IsTrue(p == null));
Test(json1, p => Assert.IsTrue(p == null));
Test(json2, p => Assert.IsTrue(p != null && p.religion == "Нет"));
Test(json3, p => Assert.IsTrue(p != null && p.religion == "Нет"));
Test(json4, p => { throw new AssertionFailedException("Should have failed"); } );
Console.WriteLine("Caught expected exception: " + ex.Message);
Test(json5, p => { throw new AssertionFailedException("Should have failed"); });
Console.WriteLine("Caught expected exception: " + ex.Message);
Console.WriteLine("Done with tests.");
static void Test(string json, Action<Personal> validate)
var user = JsonConvert.DeserializeObject<User>(json);
var newJson = JsonConvert.SerializeObject(user, Formatting.Indented);
Console.WriteLine(newJson);
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");