如何防止被序列化整个类? [英] How to prevent an entire class from being serialized?
问题描述
我使用Newtonsoft.Json来序列化一个类及其所有成员。有一个特定的类,它的许多成员是一个实例,我只想告诉一个类不进行序列化,所以如果任何成员是该类型的实例被跳过。
I am using Newtonsoft.Json to serialize a class and all of its members. There is one particular class that many of its members are an instance of, I'd simply like to tell a class to not be serialized at all, so if any member that is an instance of that type is skipped.
在C#中,通过向类添加某种属性以将其标记为不可序列化,这是否可能?
Is this possible in C# by appending some sort of attribute to a class to mark it as non-serializable?
推荐答案
我不认为这可以使用类上的属性。但是你应该能够实现一个自定义的 JsonConverter
,它总是序列化和反序列化这个类的任何实例到 null
。这个代码实现了这样的行为:
I do not think this can be done using an attribute on the class. However you should be able to do it by implementing a custom JsonConverter
which always serializes and deserializes any instance of this class to null
. This code implements such behavior:
class IgnoringConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteNull();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return null;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ClassToIgnore);
}
}
在此示例中, ClassToIgnore
是您希望在序列化期间忽略的类。这些类应该用 JsonConverter
属性进行修饰:
In this example, ClassToIgnore
is the class you wish to ignore during serialization. Such classes should be decorated with the JsonConverter
attribute:
[JsonConverter(typeof(IgnoringConverter))]
class ClassToIgnore
您还可以将转换器注册为默认转换器,如果您使用ASP.NET Web API,这将非常有用。
You can also register the converter as a default converter which is useful if you're using ASP.NET Web API.
我已经添加了一个控制台应用程序示例来演示其功能:
I have included a Console application sample to demonstrate the functionality:
using System;
using Newtonsoft.Json;
/// <summary>
/// Class we want to serialize.
/// </summary>
class ClassToSerialize
{
public string MyString { get; set; } = "Hello, serializer!";
public int MyInt { get; set; } = 9;
/// <summary>
/// This will be null after serializing or deserializing.
/// </summary>
public ClassToIgnore IgnoredMember { get; set; } = new ClassToIgnore();
}
/// <summary>
/// Ignore instances of this class.
/// </summary>
[JsonConverter(typeof(IgnoringConverter))]
class ClassToIgnore
{
public string NonSerializedString { get; set; } = "This should not be serialized.";
}
class IgnoringConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteNull();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return null;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ClassToIgnore);
}
}
class Program
{
static void Main(string[] args)
{
var obj = new ClassToSerialize();
var json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
obj = JsonConvert.DeserializeObject<ClassToSerialize>(json);
// note that obj.IgnoredMember == null at this point
Console.ReadKey();
}
}
这篇关于如何防止被序列化整个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!