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 = 0, // // 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 = 1, // // 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 = 2 }
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
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-06-24 How does it work in C#? - Part 3 (C# LINQ in detail)
2015-06-24 Counting sheep...
2015-06-24 Convert boolean values to strings 'Yes' or 'No'.
2015-06-24 Function 1 - hello world
2015-06-24 Return Negative
2015-06-24 CodeWars题目筛选
2015-06-24 Miles per gallon to kilometers per liter