配置 Json.net 对特定字段进行 URL 编码

des*_*lsj 4 c# json.net

假设我有一个这样的模型类:

public class MyModelClass
{
    [JsonProperty("first_field"]
    public string FirstField { get; set; }

    [JsonProperty("second_field"]
    public string SecondField { get; set; }

    public MyModelClass(string first, string second)
    {
        FirstField = first;
        SecondField = second;
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我有一个这种类型的实例:

var myObject = new MyModelClass("blablabla", "<>@%#^^@!%");
Run Code Online (Sandbox Code Playgroud)

当我使用 Json.net 将此对象转换为 Json 字符串时,我得到如下信息:

{ 'first_field': 'blablabla', 'second_field': '<>@%#^^@!%' }
Run Code Online (Sandbox Code Playgroud)

有没有办法配置 Json.net 以便“SecondField”的内容是 URL 编码的?我需要编写自己的自定义转换器还是有更简单的方法?

Bri*_*ers 6

如果它只是一个类中的一个字段,您可以简单地向您的类添加一个只读属性来处理编码,并对其进行注释,以便它在序列化过程中取代原始属性:

    public class MyModelClass
    {
        ...

        [JsonIgnore]
        public string SecondField { get; set; }

        [JsonProperty("second_field")]
        private string UrlEncodedSecondField
        {
            get { return System.Web.HttpUtility.UrlEncode(SecondField); }
        }

        ...
    }
Run Code Online (Sandbox Code Playgroud)

演示小提琴:https : //dotnetfiddle.net/MKVBVH


如果您需要跨多个类的多个字段,您可以使用类似于在反序列化期间选择性转义字符串中的 HTML 中的解决方案,但有一些小的更改:

  1. 创建自定义UrlEncode属性并让解析器查找具有该属性而不是缺少AllowHtml属性的属性。
  2. HtmlEncodingValueProvider将 a更改为 aUrlEncodingValueProvider并让它在GetValue而不是应用编码SetValue(以便它对序列化而不是反序列化进行编码)。

结果代码如下所示:

public class UrlEncodeAttribute : Attribute { }

public class CustomResolver : DefaultContractResolver
{
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);

        // Find all string properties that have a [UrlEncode] attribute applied
        // and attach an UrlEncodingValueProvider instance to them
        foreach (JsonProperty prop in props.Where(p => p.PropertyType == typeof(string)))
        {
            PropertyInfo pi = type.GetProperty(prop.UnderlyingName);
            if (pi != null && pi.GetCustomAttribute(typeof(UrlEncodeAttribute), true) != null)
            {
                prop.ValueProvider = new UrlEncodingValueProvider(pi);
            }
        }

        return props;
    }

    protected class UrlEncodingValueProvider : IValueProvider
    {
        PropertyInfo targetProperty;

        public UrlEncodingValueProvider(PropertyInfo targetProperty)
        {
            this.targetProperty = targetProperty;
        }

        // SetValue gets called by Json.Net during deserialization.
        // The value parameter has the original value read from the JSON;
        // target is the object on which to set the value.
        public void SetValue(object target, object value)
        {
            targetProperty.SetValue(target, (string)value);
        }

        // GetValue is called by Json.Net during serialization.
        // The target parameter has the object from which to read the string;
        // the return value is the string that gets written to the JSON
        public object GetValue(object target)
        {
            string value = (string)targetProperty.GetValue(target);
            return System.Web.HttpUtility.UrlEncode(value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用自定义解析器,首先要使用新[UrlEncode]属性装饰要进行 URL 编码的任何属性:

public class MyModelClass
{
    [JsonProperty("first_field")]
    public string FirstField { get; set; }

    [UrlEncode]
    [JsonProperty("second_field")]
    public string SecondField { get; set; }

    ...
}
Run Code Online (Sandbox Code Playgroud)

然后,像这样序列化您的模型:

var myObject = new MyModelClass("blablabla", "<>@%#^^@!%");

var settings = new JsonSerializerSettings
{
    ContractResolver = new CustomResolver(),
    Formatting = Formatting.Indented
};

string json = JsonConvert.SerializeObject(myObject, settings);
Run Code Online (Sandbox Code Playgroud)

演示小提琴:https : //dotnetfiddle.net/iOOzFr