You can make your own custom ContractResolver
with a dictionary of override attributes by member, then override CreateProperty()
and apply the overrides to the JsonProperty
returned by the base class:
public class JsonPropertyOverride
{
public string PropertyName { get; set; }
public bool? Ignored { get; set; }
// Others as required from http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm
// Changing all value type properties to nullables.
}
public class OverrideContractResolver : DefaultContractResolver
{
readonly Dictionary<MemberInfo, JsonPropertyOverride> overrides; // A private copy for thread safety.
public OverrideContractResolver(IDictionary<MemberInfo, JsonPropertyOverride> overrides)
: base()
{
if (overrides == null)
throw new ArgumentNullException();
this.overrides = overrides.ToDictionary(p => p.Key, p => p.Value);
}
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property != null)
{
JsonPropertyOverride attr;
if (overrides.TryGetValue(member, out attr))
{
if (attr.PropertyName != null)
property.PropertyName = ResolvePropertyName(attr.PropertyName);
if (attr.Ignored != null)
property.Ignored = attr.Ignored.Value;
}
}
return property;
}
}
You could also inherit from CamelCasePropertyNamesContractResolver
if you prefer.
Then use it like:
public class Foo
{
public string SomeLengthyCSharpPropertyName { get; set; }
public string DefaultNotIgnored { get; set; }
[JsonIgnore]
public string DefaultIgnored { get; set; }
}
public class TestClass
{
public static void Test()
{
var foo = new Foo { SomeLengthyCSharpPropertyName = "SomeLengthyCSharpPropertyName", DefaultIgnored = "DefaultIgnored", DefaultNotIgnored = "DefaultNotIgnored" };
var resolver = new OverrideContractResolver(new Dictionary<MemberInfo, JsonPropertyOverride> {
{ typeof(Foo).GetProperty("SomeLengthyCSharpPropertyName"), new JsonPropertyOverride { PropertyName = "c" } },
{ typeof(Foo).GetProperty("DefaultNotIgnored"), new JsonPropertyOverride { Ignored = true } },
{ typeof(Foo).GetProperty("DefaultIgnored"), new JsonPropertyOverride { Ignored = false } },
});
var settings = new JsonSerializerSettings { ContractResolver = resolver };
var json = JsonConvert.SerializeObject(foo, settings); // Outputs {"c":"SomeLengthyCSharpPropertyName","DefaultIgnored":"DefaultIgnored"}
Debug.WriteLine(json);
var expectedJson = @"{ ""c"": ""SomeLengthyCSharpPropertyName"", ""DefaultIgnored"": ""DefaultIgnored"" }";
var ok = JToken.DeepEquals(JToken.Parse(json), JToken.Parse(expectedJson));
Debug.Assert(ok); // No assert
var foo2 = JsonConvert.DeserializeObject<Foo>(json, settings);
var ok2 = foo2.DefaultIgnored == foo.DefaultIgnored && foo2.SomeLengthyCSharpPropertyName == foo.SomeLengthyCSharpPropertyName;
Debug.Assert(ok2); // No assert
}
}