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    
posted @   ChuckLu  阅读(403)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.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
点击右上角即可分享
微信分享提示