替换JSON序列化上的敏感数据值 [英] Replace sensitive data value on JSON serialization

查看:116
本文介绍了替换JSON序列化上的敏感数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要序列化为JSON的对象.但是,某些对象具有通过属性视为"SensitiveData"的属性.

I have some objects that I want to serialize as JSON. However, some of the objects have properties which are deemed 'SensitiveData' via attributes.

[SensitiveDataAttribute]
public string SomeSensitiveProperty {get; set;}

此刻,我覆盖了序列化程序上的'CreateProperty'方法,以便我可以根据属性是否具有'SensitiveData'属性来更改是否应该序列化该属性:

At the moment, I am overriding the 'CreateProperty' method on the serializer so that I can alter whether or not a property should be serialized dependent upon whether it has this 'SensitiveData' attribute:

public class SensitiveDataResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);
            property.ShouldSerialize = instance =>
            {
                if (member is PropertyInfo)
                {
                    var prop = (PropertyInfo) member;
                    var isSensitiveData = Attribute.IsDefined(prop, typeof (SensitiveDataAttribute));
                    return !isSensitiveData;
                }
                return false;
            };
            return property;
        }
    }
}

序列化时,然后使用该解析器作为序列化器的设置:

When I serialize, I then use that resolver as settings for the serializer:

var settings = new JsonSerializerSettings() { ContractResolver = new SensitiveDataResolver() };
var requestString = JsonConvert.SerializeObject(someObject, settings);

我的问题是,我不希望将属性排除在序列化之外.我希望对它们进行序列化,但对它们设置默认值"SensitiveData".

My problem is, I don't want the properties to be excluded from the serialization. I want them to be serialized but with the default value 'SensitiveData' set against them.

有没有一种方法可以使用属性来实现呢?

Is there a way I can achieve this using Attributes?

推荐答案

如果成员具有属性,则可以覆盖属性值,而不必使用ShouldSerialize方法.为此,您需要为Json.NET提供自定义IValueProvider以便在序列化时使用.

Instead of using the ShouldSerialize method, you can just override the property value if the member has the attribute. To do this, you need to supply a custom IValueProvider for Json.NET to use when serializing.

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

    if (member is PropertyInfo)
    {
        var prop = (PropertyInfo)member;
        var isSensitiveData = Attribute.IsDefined(prop, typeof (SensitiveDataAttribute));

        if (isSensitiveData)
            property.ValueProvider = new StringValueProvider("SensitiveData");
    }

    return property;
}

StringValueProviderIValueProvider接口的自定义实现.

StringValueProvider is a custom implementation of the IValueProvider interface.

public class StringValueProvider : IValueProvider
{
    private readonly string _value;

    public StringValueProvider(string value)
    {
        _value = value;
    }

    public void SetValue(object target, object value)
    {
        throw new NotSupportedException();
    }

    public object GetValue(object target)
    {
        return _value;
    }
}

这篇关于替换JSON序列化上的敏感数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆