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 QueryResponseRoot<T>
[JsonProperty("QueryResponse")]
public QueryResponse<T> QueryResponse { get; set; }
[JsonProperty("time", Required = Required.Always)]
public DateTimeOffset Time { get; set; }
public class QueryResponse<T>
public IList<T> Results { get; set; }
[JsonProperty("startPosition")]
public Int64 StartPositions { get; set; }
[JsonProperty("maxResults")]
public Int64 MaxResults { get; set; }
public string GivenName { get; set; }
public string Name { get; set; }
public class GenericPropertyContractResolver : DefaultContractResolver
private readonly Type genericTypeDefinition;
public GenericPropertyContractResolver(Type genericTypeDefinition)
this.genericTypeDefinition = genericTypeDefinition;
protected override JsonProperty CreateProperty(
MemberInfo member, MemberSerialization memberSerialization)
JsonProperty baseProperty =
base.CreateProperty(member, memberSerialization);
Type declaringType = member.DeclaringType;
if (!declaringType.IsGenericType ||
declaringType.GetGenericTypeDefinition() != this.genericTypeDefinition)
Type declaringGenericType = declaringType.GetGenericArguments()[0];
if (IsGenericMember(member))
baseProperty.PropertyName =
this.ResolvePropertyName(declaringGenericType.Name);
public bool IsGenericMember(MemberInfo member)
MemberInfo genericMember =
this.genericTypeDefinition.GetMember(member.Name)[0];
if (genericMember != null)
if (genericMember.MemberType == MemberTypes.Field)
return ((FieldInfo)genericMember).FieldType.IsGenericParameter;
else if (genericMember.MemberType == MemberTypes.Property)
PropertyInfo property = (PropertyInfo)genericMember;
public static void Test()
""GivenName"": ""Test 2""
""time"": ""2018-08-11T16:12:10.808-07:00""
Test<Customer>(customerJson);
""Name"": ""Accounts Payable (A/P)""
""time"": ""2018-08-11T16:14:55.309-07:00""
Test<Account>(accountJson);
public static void Test<T>(string json)
Console.WriteLine("\nTesting round-trip of {0}", typeof(T));
var settings = new JsonSerializerSettings
ContractResolver = new GenericPropertyContractResolver(typeof(QueryResponse<>)),
var root = JsonConvert.DeserializeObject<QueryResponseRoot<T>>(json, settings);
var json2 = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
Console.WriteLine(json2);
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");