字面意义是忽略序列化,就是当字段在序列化时,被[JsonIgnore]标记了的字段将被忽略序列化

序列化输出中使用Id和Name属性,但我绝对不会对AlternateName和Color感兴趣.我用[JsonIgnore]标记了那些.我希望描述出现,但有时这可能会变得很长,所以我使用自定义JsonConverter来限制它的长度.我还想使用较短的属性名称来描述,所以我用[JsonProperty(“Desc”)]标记了它.

class Foo
{
public int Id { get; set; }
public string Name { get; set; }
[JsonIgnore]
public string AlternateName { get; set; }
[JsonProperty("Desc")]//替换序列化名
[JsonConverter(typeof(StringTruncatingConverter))]
public string Description { get; set; }
[JsonIgnore]
public string Color { get; set; }
}

当我序列化上面的一个实例时……

Foo foo = new Foo
{
Id = 1,
Name = "Thing 1",
AlternateName = "The First Thing",
Description = "This is some lengthy text describing Thing 1 which you'll no doubt find very interesting and useful.",
Color = "Yellow"
}; string json = JsonConvert.SerializeObject(foo, Formatting.Indented);

…我得到这个输出:

{
"Id": 1,
"Name": "Thing 1",
"Desc": "This is some lengthy text describing Thing 1 "
}

我有时想获得完整的JSON输出,忽略我的自定义.我可以使用自定义ContractResolver以编程方式“取消应用”类中的属性.这是解析器的代码:

class IgnoreJsonAttributesResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
foreach (var prop in props)
{
prop.Ignored = false; // Ignore [JsonIgnore]
prop.Converter = null; // Ignore [JsonConverter]
prop.PropertyName = prop.UnderlyingName; // restore original property name
}
return props;
}
}

要使用解析器,我将它添加到JsonSerializerSettings并将设置传递给序列化器,如下所示:

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new IgnoreJsonAttributesResolver();
settings.Formatting = Formatting.Indented; string json = JsonConvert.SerializeObject(foo, settings);

输出现在包括被忽略的属性,并且描述不再被截断:

{
"Id": 1,
"Name": "Thing 1",
"AlternateName": "The First Thing",
"Description": "This is some lengthy text describing Thing 1 which you'll no doubt find very interesting and useful.",
"Color": "Yellow"
} 原文:http://www.voidcn.com/article/p-uvkjwmre-bub.html
 
												

最新文章

  1. 基于canvas的陈列订货的分析
  2. Mysql 常用命令集
  3. strtok&amp;strsep
  4. YTKNetwork
  5. InnoDB主要数据结构及调用流程
  6. EasyUI - Draggable 拖动控件
  7. zlog学习随笔
  8. java 11 局部变量类型推断
  9. 显示Linux进程打开了哪些文件? 用lsof命令
  10. Error fetching command &#39;collectstatic&#39;: You&#39;re using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Command &#39;collectstatic&#39; skipped
  11. web@css高级选择器(after,befor用法),基本css样式
  12. AtCoder SoundHound Inc. Programming Contest 2018 E + Graph (soundhound2018_summer_qual_e)
  13. django中数据库操作有关部分
  14. DAY11 函数(二)
  15. postman接口测试实例
  16. &lt;string.h&gt;的学习
  17. Java基础学习篇---------this、object的学习
  18. poj1273(Edmonds-Karp)
  19. c#去除DataTable空列
  20. Hadoop系列-HDFS基础

热门文章

  1. maven scope provided和runtime的例子
  2. laravel 配置双模板引擎
  3. Java ——继承
  4. [ScreenOS] How to manually generate a new system self-signed certificate to replace the expired system self-signed certificate without resetting the firewall
  5. vue+egg.js+mysql一个前后端分离留言板项目
  6. dotnet sdk 的镜像tag 相关
  7. uva-796.critical links(连通图的桥)
  8. JS中创建10个a标签,点击弹出对应的序号
  9. go &amp; nssm
  10. LNMP环境搭建哈哈