Json ignore on class level
Exclude all instances of a class from serialization in Newtonsoft.Json
Every custom type can opt how it will be serialized.
To example, mark the type with [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
and then you have to mark something with [JsonProperty]
otherwise nothing will be serialized. So even if property of custom type is serializable the type may produce nothing ({}
) to serialize:
public class A
{
public string Test { get; set; } = "Test";
public B B { get; set; } = new B();
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class B
{
public string Foo { get; set; } = "Foo";
}
and then
Console.WriteLine(JsonConvert.SerializeObject(new A()));
will produce
{"Test":"Test","B":{}}"
With this approach you will have problems to serialize B
at all. Which is not very bright idea, don't you think?
MemberSerialization枚举类型的官方解释Specifies the member serialization options for the Newtonsoft.Json.JsonSerializer.
public enum MemberSerialization
{
//
// Summary:
// All public members are serialized by default. Members can be excluded using Newtonsoft.Json.JsonIgnoreAttribute
// or System.NonSerializedAttribute. This is the default member serialization mode.
OptOut = ,
//
// Summary:
// Only members marked with Newtonsoft.Json.JsonPropertyAttribute or System.Runtime.Serialization.DataMemberAttribute
// are serialized. This member serialization mode can also be set by marking the
// class with System.Runtime.Serialization.DataContractAttribute.
OptIn = ,
//
// Summary:
// All public and private fields are serialized. Members can be excluded using Newtonsoft.Json.JsonIgnoreAttribute
// or System.NonSerializedAttribute. This member serialization mode can also be
// set by marking the class with System.SerializableAttribute and setting IgnoreSerializableAttribute
// on Newtonsoft.Json.Serialization.DefaultContractResolver to false.
Fields =
}
JSON.Net Self referencing loop detected
The fix is to ignore loop references and not to serialize them. This behaviour is specified in JsonSerializerSettings
.
Single JsonConvert
with an overload:
JsonConvert.SerializeObject((from a in db.Events where a.Active select a).ToList(), Formatting.Indented,
new JsonSerializerSettings() {
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
}
);
If you'd like to make this the default behaviour, add a Global Setting with code in Application_Start()
in Global.asax.cs:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings {
Formatting = Newtonsoft.Json.Formatting.Indented,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};
Reference: https://github.com/JamesNK/Newtonsoft.Json/issues/78
Json ignore on class level的更多相关文章
- Query a JSON array in SQL
sql 中存的json 为数组: [{"Level":1,"Memo":"新用户"},{"Level":2," ...
- 【转】Qt之JSON保存与读取
简述 许多游戏提供保存功能,使得玩家在游戏中的进度可以被保存,并在以后再玩的时候进行加载.保存游戏的过程通常涉及将每个游戏对象的成员变量序列化为文件.要实现这个功能,可以采取许多格式,其中之一就是 J ...
- 【Python学习笔记】Coursera课程《Using Python to Access Web Data》 密歇根大学 Charles Severance——Week6 JSON and the REST Architecture课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week6 JSON and the REST Architecture 13.5 Ja ...
- Unity Json解析IPA
今天看到一个unity 自带的解析json的IPA,感觉比litjson好用很多,废话不多,上代码 using System.Collections; using System.Collections ...
- 创建 Web 前端开发环境
Web 前端开发涉及多种工具,这里将常用工具的安装和配置进行说明,提供了详细的说明,为后继的开发创建一个坚实的基础. 本文介绍的工具有:NodeJS, NPM, Bower, Git 和 Grunt. ...
- bower
1. bower介绍 Bower 是 twitter 推出的一款包管理工具,基于nodejs的模块化思想,把功能分散到各个模块中,让模块和模块之间存在联系,通过 Bower 来管理模块间的这种联系. ...
- crossplatform---bower解决js的依赖管理
从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...
- Bayeux协议
Bayeux 协议-- Bayeux 1.0草案1 本备忘录状态 This document specifies a protocol for the Internet community, and ...
- bower解决js库的依赖管理
从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...
随机推荐
- word定义多级列表
1.单击开始选项卡里的多级列表按钮,在下拉列表中选择定义新的多级列表 2.先设置第一级编号,选择阿拉伯数字1,2,3,...,并在自动编号“1”的左右分别输入“第”“章”,级别链接到样式选择标题一 3 ...
- 洛谷 P1541 乌龟棋 & [NOIP2010提高组](dp)
传送门 解题思路 一道裸的dp. 用dp[i][j][k][kk]表示用i个1步,j个2步,k个3步,kk个4步所获得的最大价值,然后状态转移方程就要分情况讨论了(详见代码) 然后就是一开始统计一下几 ...
- 1. Docker快速入门(仓库,镜像,容器)
参考阿里云文档:https://help.aliyun.com/document_detail/51853.html?spm=a2c4g.11186623.6.820.RaToNY 参考菜鸟教程文档: ...
- Linux文档整理之【Nginx安装Docker】
这次整理的文档是Docker安装 先说明下我用的系统是Linux Centos,不同的Linux版本差别不大. 1.安装依赖包 sudo yum install -y yum-utils device ...
- IDEA闪退问题
这段时间经常遇到IDEA闪退的问题,在网上搜了一大堆的博客,无外乎是说让修改下面两个文件,但是改来改去没什么卵用,最后重装IDEA,一样的,没什么用.持续时间有几个月了,内心也有点崩溃,昨天下午彻底心 ...
- 使用pdfjs插件在线预览PDF文件
前言 本文介绍在html中使用 pdfjs插件在线预览PDF文件的方法. 实现步骤 下载 pdfjs 并引入项目中 到PDFJS官网 http://mozilla.github.io/pdf.js/g ...
- Django框架简易图
- JAVA中关于日期的最常见的操作
//获取当前系统的时间戳 long times = System.currentTimeMillis(); //创建一个当前日期对象 Date now = new Date(); //基于指定的时间戳 ...
- JAVA(-Xms,Xmx,Xmn-XX:newSize,-XX:MaxnewSize,-XX:PermSize,-XX:MaxPermSize)区别
1.-Xms:表示java虚拟机堆区内存初始内存分配的大小,通常为操作系统可用内存的1/64大小即可,但仍需按照实际情况进行分配.2.-Xmx:表示java虚拟机堆区内存可被分配的最大上限,通常为操作 ...
- Android PdfViewer案例使用
今天按项目要求找了一个android的PDF控件,各种操作效果都非常好,在这里和大家分享一下. com.joanzapata.pdfview:android-pdfview 该PDF控件加载大存储的 ...