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浏 ...
随机推荐
- 本地项目代码上传至github
初始化本地目录:git init cd到个人本地项目代码文件目录下,执行git init命令 添加项目文件到本地仓库:git add . git commit -m "提交说明" ...
- SSM框架之AOP、动态代理、事务处理相关随笔
AOP: 原理:底层利用动态代理(两种动态代理技术都使用了) 两种实现方案: 第一种:JDK动态代理技术 实现的InvocationHandler接口,要想实现某个类的动态代理对象,必须有接口有实现类 ...
- 对C++拷贝构造函数的一点理解
一. 什么是拷贝构造函数 先看一个简单的例子: #include <iostream> using namespace std; class CExample { private: int ...
- 14-jquery元素节点操作
**创建节点** ```var Div = $('<div>');var Div2 = $('<div>这是一个div元素</div>');``` **插入节点** ...
- http协议是无状态协议,它的无状态指的是什么,如何解决这种情况
http是无状态的协议,也是不安全的协议, 它的无状态是指对于事务处理没有记忆能力,缺少状态意味着后续的操作需要前面的信息. 解决办法:1,通过cookie解决,2,通过session会话保存.
- CSS行内框(内联元素)
行内框在一行中水平布置.可以使用水平内边距.边框和外边距调整它们的间距.但是,垂直内边距.边框和外边距不影响行内框的高度.由一行形成的水平框称为行框(Line Box),行框的高度总是足以容纳它包含的 ...
- [IIS]修改MaxFieldLength与MaxRequestBytes彻底解决Request Too Long的问题
当 IIS7/7.5 收到的请求头的长度超过16K(默认值),就会引发"Bad Request - Request Too Long. HTTP Error 400. The size of ...
- MySql二进制版安装教程
1.检查是否已安装过mariadb,若有便删除(linux系统自带的) [root@localhost /]# rpm -qa | grep mariadb [root@localhost /]# r ...
- C功能模块集锦
1. offsetof #include <stddef.h> size_t offsetof(type, member); The macro offsetof() returns th ...
- rest_framework框架的认证、权限
REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": ["app01.utils.TokenAuth", ] ...