How to prevent serialization of an entire class?

I am using Newtonsoft.Json to serialize a class and all its members. There is one particular class where many of its members are instances, I would just like to say that the class will not be serialized at all, so if any member that is an instance of that type is missing.

Is it possible in C # by adding some attribute to the class to mark it as non-serializable?

+3


source to share


1 answer


I don't think it can be done using a class attribute. However, you must do this by implementing a custom JsonConverter

one that always serializes and deserializes any instance of that class to null

. This code implements this 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);
    }
}

      

In this example ClassToIgnore

, this is the class you want to ignore during serialization. Such classes should be decorated with the attribute JsonConverter

:

[JsonConverter(typeof(IgnoringConverter))]
class ClassToIgnore

      



You can also register the converter as the default converter , which is useful if you are using ASP.NET Web API.

I've included a sample console app 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();
    }
}

      

+3


source







All Articles