Newtonsoft.Json序列化与反序列化时使用不同的属性名

更新时间:2023-08-03 13:23:15 阅读: 评论:0

Newtonsoft.Json序列化与反序列化时使⽤不同的属性名Newtonsoft.Json序列化与反序列化时使⽤不同的属性名称
⼀、需求
现有⼀个类IDCard:
public class IDCard<T>: WordsResult
{
public T address {get;t;}
public T name {get;t;}
public T birth {get;t;}
public T idno {get;t;}
public T gender {get;t;}
天津到济南public T nation {get;t;}
}
以及⼀段json字符串:
{"住址":"⼴东省深圳市福岗区笋岗西路30号","姓名":"栾韶","出⽣":"19660909","公民⾝份号码":"","性别":"男","民族":"回"}
我需要将字符串反序列化为IDCard对象,并且在⼀定的其它操作后再次将这个对象序列化后输出,同时输出的属性名⽤我⾃⼰定义的属性名。
我期望的输出结果:
{"address":"⼴东省深圳市福岗区笋岗西路30号","name":"栾韶","birth":"19660909","idno":"","gender":"男","nation":"回"}
⼆、JsonProperty
使⽤Newtonsoft.Json的JsonProperty特性
public class IDCard<T>: WordsResult
{
[JsonProperty(propertyName:"住址")]
public T address {get;t;}
93年什么命
[JsonProperty(propertyName:"姓名")]
public T name {get;t;}
[JsonProperty(propertyName:"出⽣")]
public T birth {get;t;}
[JsonProperty(propertyName:"公民⾝份号码")]
public T idno {get;t;}
[JsonProperty(propertyName:"性别")]
public T gender {get;t;}
[JsonProperty(propertyName:"民族")]
public T nation {get;t;}
}
反序列化时确实有效:
但重新将其序列化时:
{"住址":"⼴东省深圳市福岗区笋岗西路30号","姓名":"栾韶","出⽣":"19660909","公民⾝份号码":"","性别":"男","民族":"回"}返回的并不是我期望的结果
三、⾃定义Attribute
定义Attribute类的派⽣类OcrAttribute:
public class OcrAttribute : Attribute
掩耳盗铃什么意思{
public string ReadName {get;t;}
public string WriteName {get;t;}
public bool Readable {get;t;}
public bool Writable {get;t;}
public OcrAttribute()
{
this.Readable =true;
this.Writable =true;白唇鹿
}
}
并添加到IDCard中:
public class IDCard<T>: WordsResult
{
[Ocr(WriteName ="住址")]
public T address {get;t;}
[Ocr(WriteName ="姓名")]
public T name {get;t;}
[Ocr(WriteName ="出⽣")]
public T birth {get;t;}
[Ocr(WriteName ="公民⾝份号码")]
public T idno {get;t;}
[Ocr(WriteName ="性别")]
public T gender {get;t;}
[Ocr(WriteName ="民族")]
public T nation {get;t;}
}
四、⾃定义ContractResolver
定义DefaultContractResolver的派⽣类,⽐如OcrWriteResolver和OcrReadResolver:
public class OcrWriteResolver: DefaultContractResolver
{
养了一条蛇protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) {
var property = ba.CreateProperty(member, memberSerialization);
var attr = member.GetCustomAttribute(typeof(OcrAttribute))as OcrAttribute;
电力基础知识if(attr !=null)
{
property.PropertyName = attr.WriteName ?? property.PropertyName;
穿上衣服的英文
property.Writable = attr.Writable;
}
return property;
}
}
public class OcrReadResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) {
var property = ba.CreateProperty(member, memberSerialization);
var attr = member.GetCustomAttribute(typeof(OcrAttribute))as OcrAttribute;
if(attr !=null)
{
property.PropertyName = attr.ReadName ?? property.PropertyName;
property.Readable = attr.Readable;
}
return property;
}
}
重写CreateProperty⽅法,动态地修改JsonProperty的相关属性
五、反序列化与序列化
反序列化:
var ttings =new JsonSerializerSettings(){ ContractResolver =new OcrWriteResolver()};
var result = JsonConvert.DerializeObject<IDCard<string>>(content);
序列化:
var ttings =new JsonSerializerSettings(){ ContractResolver =new OcrReadResolver()};
var str = JsonConvert.SerializeObject(result, ttings);
六、Readable与Writeable
JsonProperty.Readable:属性是否可以被序列化
JsonProperty.Writeable:属性是否可以被反序列化
在某些特定场景下,⽐如:
从某个上级接⼝中返回了很多参数,在经过⼀系列处理后需要返回给下级接⼝其中⼀部分参数。
如果单纯为属性添加JsonIgnore特性,会导致我们在反序列化上级接⼝返回的json字符串时也⽆法获取到这个参数(⽐如上级平台返回的errcode我们不需要返回给下级平台,但是⾃⾝⼜需要根据这个属
性来判断请求是否成功)。
在OcrAttribute中新增Writeable与Readable属性,并在OcrWriteResolver与OcrReadResolver的CreateProperty⽅法中动态修改property.Writable与property.Readable。
修改IDCard类,使得序列化时隐藏地址:
public class IDCard<T>: WordsResult
企业英文{
[Ocr(WriteName ="住址", Readable =fal)]
public T address {get;t;}
[Ocr(WriteName ="姓名")]
public T name {get;t;}
[Ocr(WriteName ="出⽣")]
public T birth {get;t;}
[Ocr(WriteName ="公民⾝份号码")]
public T idno {get;t;}
[Ocr(WriteName ="性别")]
public T gender {get;t;}
[Ocr(WriteName ="民族")]
public T nation {get;t;}
}
进⾏序列化操作:
⽬的达成

本文发布于:2023-08-03 13:23:15,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1127696.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:序列化   属性   返回   输出
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图