c#-配置Json.net以URL编码某些字段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-配置Json.net以URL编码某些字段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3468字,纯文字阅读大概需要5分钟。
内容图文
说我有一个像这样的模型类:
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;
}
}
假设我有一个这种类型的实例:
var myObject = new MyModelClass("blablabla", "<>@%#^^@!%");
当我使用Json.net将此对象转换为Json字符串时,会得到类似以下内容的信息:
{ 'first_field': 'blablabla', 'second_field': '<>@%#^^@!%' }
有没有一种方法可以配置Json.net,以便“ SecondField”的内容经过URL编码?我需要编写自己的自定义转换器,还是有更简单的方法?
解决方法:
如果它只是一个类中的一个字段,则可以简单地向类中添加一个只读属性来处理编码,并对其进行注释,以便在序列化过程中代替原始属性:
public class MyModelClass
{
...
[JsonIgnore]
public string SecondField { get; set; }
[JsonProperty("second_field")]
private string UrlEncodedSecondField
{
get { return System.Web.HttpUtility.UrlEncode(SecondField); }
}
...
}
演示小提琴:https://dotnetfiddle.net/MkVBVH
如果您需要跨多个类的多个字段使用此方法,则可以使用与Selectively escape HTML in strings during deserialization中的解决方案类似的解决方案,并进行一些小的更改:
>创建一个自定义的UrlEncode属性,并使解析器查找具有该属性的属性,而不是缺少AllowHtml属性的属性.
>将HtmlEncodingValueProvider更改为UrlEncodingValueProvider,并使其在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);
}
}
}
要使用自定义解析器,请首先使用新的[UrlEncode]属性修饰要进行URL编码的所有属性:
public class MyModelClass
{
[JsonProperty("first_field")]
public string FirstField { get; set; }
[UrlEncode]
[JsonProperty("second_field")]
public string SecondField { get; set; }
...
}
然后,像这样序列化模型:
var myObject = new MyModelClass("blablabla", "<>@%#^^@!%");
var settings = new JsonSerializerSettings
{
ContractResolver = new CustomResolver(),
Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(myObject, settings);
演示小提琴:https://dotnetfiddle.net/iOOzFr
内容总结
以上是互联网集市为您收集整理的c#-配置Json.net以URL编码某些字段全部内容,希望文章能够帮你解决c#-配置Json.net以URL编码某些字段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。