Rico Suter's blog.
 


This article describes how to implement a custom contract resolver, so that you can ignore or rename serialized JSON properties where the serialization happens and without changing the serialized classes. I use this resolver in my project NSwag, a Swagger/Open API toolchain: A Swagger specification uses JSON Schemas to describe types, but it is not exactly the same model - some properties of a schema are not allowed and some must be renamed. Because I cannot change the JSON Schema implementation, I had to ignore or rename properties in the Swagger serializer. Another use case of this contract resolver is, if you want to conditionally remove or rename properties - for example you want to serialize a property in a test environment but for security reasons you want to remove it in the production deployment (e.g. the stack trace of an exception).

Let’s have a look at the standard way to ignore or rename a property with Newtonsoft.Json:

public class Person
{
    // ignore property
    [JsonIgnore]
    public string Title { get; set; }

    // rename property
    [JsonProperty("firstName")]
    public string FirstName { get; set; }
}

As you can see, the property Title is always ignored and the property FirstName is always serialized as firstName. Also you need to add attributes to the original Person class. To avoid this inflexible approach, you need a custom resolver which decides at runtime how/if to serialize a property. This will look as following:

var person = new Person();

var jsonResolver = new PropertyRenameAndIgnoreSerializerContractResolver();
jsonResolver.IgnoreProperty(typeof(Person), "Title");
jsonResolver.RenameProperty(typeof(Person), "FirstName", "firstName");

var serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = jsonResolver;

var json = JsonConvert.SerializeObject(person, serializerSettings);

As you can see, we instantiate a new custom contract resolver PropertyRenameAndIgnoreSerializerContractResolver, register the property ignores and renames and pass the resolver to the serializer.

Finally, let’s have a look at the actual contract resolver implementation:

public class PropertyRenameAndIgnoreSerializerContractResolver : DefaultContractResolver
{
    private readonly Dictionary<Type, HashSet<string>> _ignores;
    private readonly Dictionary<Type, Dictionary<string, string>> _renames;

    public PropertyRenameAndIgnoreSerializerContractResolver()
    {
        _ignores = new Dictionary<Type, HashSet<string>>();
        _renames = new Dictionary<Type, Dictionary<string, string>>();
    }

    public void IgnoreProperty(Type type, params string[] jsonPropertyNames)
    {
        if (!_ignores.ContainsKey(type))
            _ignores[type] = new HashSet<string>();

        foreach (var prop in jsonPropertyNames)
            _ignores[type].Add(prop);
    }

    public void RenameProperty(Type type, string propertyName, string newJsonPropertyName)
    {
        if (!_renames.ContainsKey(type))
            _renames[type] = new Dictionary<string, string>();

        _renames[type][propertyName] = newJsonPropertyName;
    }

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);

        if (IsIgnored(property.DeclaringType, property.PropertyName))
        {
            property.ShouldSerialize = i => false;
            property.Ignored = true;
        }

        if (IsRenamed(property.DeclaringType, property.PropertyName, out var newJsonPropertyName))
            property.PropertyName = newJsonPropertyName;

        return property;
    }

    private bool IsIgnored(Type type, string jsonPropertyName)
    {
        if (!_ignores.ContainsKey(type))
            return false;

        return _ignores[type].Contains(jsonPropertyName);
    }

    private bool IsRenamed(Type type, string jsonPropertyName, out string newJsonPropertyName)
    {
        Dictionary<string, string> renames;

        if (!_renames.TryGetValue(type, out renames) || !renames.TryGetValue(jsonPropertyName, out newJsonPropertyName))
        {
            newJsonPropertyName = null;
            return false;
        }

        return true;
    }
}

As you can see, the implementation is very simple: We just save the ignore and rename registrations and modify the property contract in the CreateProperty(...) method if necessary.

The original implementation can be found in this GitHub repositiory.



Discussion