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的更多相关文章

  1. Query a JSON array in SQL

    sql 中存的json 为数组: [{"Level":1,"Memo":"新用户"},{"Level":2," ...

  2. 【转】Qt之JSON保存与读取

    简述 许多游戏提供保存功能,使得玩家在游戏中的进度可以被保存,并在以后再玩的时候进行加载.保存游戏的过程通常涉及将每个游戏对象的成员变量序列化为文件.要实现这个功能,可以采取许多格式,其中之一就是 J ...

  3. 【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 ...

  4. Unity Json解析IPA

    今天看到一个unity 自带的解析json的IPA,感觉比litjson好用很多,废话不多,上代码 using System.Collections; using System.Collections ...

  5. 创建 Web 前端开发环境

    Web 前端开发涉及多种工具,这里将常用工具的安装和配置进行说明,提供了详细的说明,为后继的开发创建一个坚实的基础. 本文介绍的工具有:NodeJS, NPM, Bower, Git 和 Grunt. ...

  6. bower

    1. bower介绍 Bower 是 twitter 推出的一款包管理工具,基于nodejs的模块化思想,把功能分散到各个模块中,让模块和模块之间存在联系,通过 Bower 来管理模块间的这种联系. ...

  7. crossplatform---bower解决js的依赖管理

    从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...

  8. Bayeux协议

    Bayeux 协议-- Bayeux 1.0草案1 本备忘录状态 This document specifies a protocol for the Internet community, and ...

  9. bower解决js库的依赖管理

    从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...

随机推荐

  1. PhoneGap学习网址

    官网:http://app-framework-software.intel.com/ 下载地址:http://download.csdn.net/download/haozq2012/7635951

  2. JavaScript ES6 class指南

    前言 EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建立在旧的原型系统上的语法糖,所以它们并没有带来任何的 ...

  3. java学习day1

    一.常用的DOS命令 1.打开cmd 窗口键+r --> 输入cmd --> 确认 2.常用的dos命令 dir:列出当前目录下的所有文件及文件夹 md:创建一个新的目录 rd:删除目录 ...

  4. 由于;引发的Oracle的BadSqlExecption

    一.在使用单引号进行各种转换合并的时候发生的异常: 1:首先单引号的表示符为 ' 在Oracle中如果SQL语句带上了分号是会报错的, 在SQL传入进行执行的时候会报BadSQLExecption,这 ...

  5. windows上Appium安装和使用

    1.Appium安装相关依赖工具: Android Studio或者Android SDK:https://developer.android.com/studio/Appium Desktop: h ...

  6. 洛谷P3412 仓鼠找$Sugar\ II$题解(期望+统计论?)

    洛谷P3412 仓鼠找\(Sugar\ II\)题解(期望+统计论?) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1327573 原题链接:洛谷P3412 ...

  7. Nginx工作机制

    Nginx分为单工作进程和多工作进程两种模式.通常采用1个master+多个worker进程配合异步非阻塞的工作机制.master进程主要负责管理自身和下属的worker进程,worker负责处理请求 ...

  8. win10上的Django项目实现内网映射

    网上有很多关于django项目发布uwsgi模块在 windows上的下载以及安装过程, 可是他们也并没有真正的在windows上使用到这个模块(至少目前在网上还没看到过), 而大部分人就是在Djan ...

  9. git合并丢失代码问题分析与解决(错误操作导致)

    问题描述 我们在主干dev和branch1分支上进行并行开发.当要把branch1功能的代码合并到dev上时,发现dev上开发的部分功能代码找不到了. 那么,是在branch1上,作了删除提交导致的吗 ...

  10. oracle中用case when查询列表

    查询sql语句如下 SELECT * FROM ( SELECT * ,ROW_NUMBER() OVER ( PARTITION BY scene_code ORDER BY (CASE statu ...