using System.Collections.Generic;
using Newtonsoft.Json.Serialization;
public Phi[] Tags { get; set; }
public string Internal_Name { get; set; }
public string Name { get; set; }
public string[] Nutty { get; set; }
public static void Main()
Internal_Name = "Some text"
Console.WriteLine(JsonConvert.SerializeObject(ep, new JsonSerializerSettings { ContractResolver = new AddJsonTypenameContractResolver() }));
[AttributeUsage(AttributeTargets.Class| AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
public class AddJsonTypenameAttribute : Attribute
public class AddJsonTypenameContractResolver : DefaultContractResolver
static AddJsonTypenameContractResolver instance;
static AddJsonTypenameContractResolver() { instance = new AddJsonTypenameContractResolver(); }
public static AddJsonTypenameContractResolver Instance { get { return instance; } }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
return base.CreateProperty(member, memberSerialization)
.ApplyAddTypenameAttribute();
protected override JsonArrayContract CreateArrayContract(Type objectType)
return base.CreateArrayContract(objectType)
.ApplyAddTypenameAttribute();
public static class ContractResolverExtensions
public static JsonProperty ApplyAddTypenameAttribute(this JsonProperty jsonProperty)
if (jsonProperty.TypeNameHandling == null)
if (jsonProperty.PropertyType.GetCustomAttribute<AddJsonTypenameAttribute>(false) != null)
jsonProperty.TypeNameHandling = TypeNameHandling.All;
public static JsonArrayContract ApplyAddTypenameAttribute(this JsonArrayContract contract)
if (contract.ItemTypeNameHandling == null)
if (contract.CollectionItemType.GetCustomAttribute<AddJsonTypenameAttribute>(false) != null)
contract.ItemTypeNameHandling = TypeNameHandling.All;