How can I "un-JsonIgnore" an attribute in a derived class?

PLNech

I am using Newtonsoft's JsonSerializer to serialise some classes.

As I wanted to omit one field of my class in the serialisation process, I declared it as follow:

[JsonIgnore]
public int ParentId { get; set; }

This worked, but I am now facing a new problem : In a derived class, I would like this field to appear (and do so only in this specific derived class).

I have been looking through the documentation and on the Internet for a way to override this setting in child classes (I guess I need something like [JsonStopIgnore], but I couldn't find anything close).


  • Is there any way for me to force JsonSerializer to pick up again this attribute ?
  • Is it possible to explicitly mark an attribute as [JsonIgnore], but only in base class ?
Yuval Itzchakov

You can do this by creating a custom DefaultContractResolver and overriding its CreateProperty method.

For example, given a Foo base and a derived Bar:

public class Foo
{
    [JsonIgnore]
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Bar : Foo 
{ }

You can create the following contract resolver:

public class MyTypeContractResolver<T> : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member,
                                                   MemberSerialization
                                                       memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);

        property.Ignored = false;
        property.ShouldSerialize = propInstance => property.DeclaringType != typeof (T);
        return property;
    }
}

This will set all properties to Ignored = false, and then analyze them by the given predicate:

propInstance => property.DeclaringType != typeof (T);

Which in our case means "you should serialize only if they are not of type Foo" (since Foo is the DeclaryingType).

And then when you want to deserialize, you pass an instance of the contract resolver to JsonSerializerSettings:

var bar = new Bar();
var result = JsonConvert.SerializeObject(bar,
    new JsonSerializerSettings {ContractResolver = new MyTypeContractResolver<Bar>()});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I friend a derived class function in the base class?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How can I combine templated derived class in CRTP with derived class expression templates?

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

How can I do constructor overloading in a derived class in TypeScript?

From Dev

How can I retrieve the derived class from a list of abstract classes?

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

How can I retrieve the derived class from a list of abstract classes?

From Dev

C++ How can I return an unknown derived class?

From Dev

How can I access a method defined with new keyword in a derived class

From Dev

How can I call the constructor of the derived class when using CRTP?

From Dev

How do I change an element in a parent class into an attribute in a derived class with JAXB annotations?

From Dev

How can I store in a derived class information obtained during initialization of a base class?

From Dev

How can I create an instance of a derived class from an instance of a base class and include private fields?

From Dev

How can I initialize an instance of a derived class from an instance of the base class?

From Dev

How can I have an object of a derived class "reference" members of an instance of the base class?

From Dev

How can I store in a derived class information obtained during initialization of a base class?

From Dev

How can I specify a class attribute in new_tag()?

From Dev

How can I cache API data in a class attribute?

From Java

No attribute 'compile', how can I modify the class, so that it works?

From Dev

How can I type hint a dynamically set class attribute in a metaclass?

From Dev

How can I save EditText input as class attribute in Kotlin/AndroidStudio?

From Dev

How can I select same attribute without using class or id?

From Dev

Cython: how can I set the attribute of a cdef class?

From Dev

How can I use setAttribute on an input without an ID or class attribute?

From Dev

How can I add a class, id or attribute to a twig include?

Related Related

  1. 1

    How can I friend a derived class function in the base class?

  2. 2

    How can I implement a Singleton class that can be derived from in WPF?

  3. 3

    How can I implement a Singleton class that can be derived from in WPF?

  4. 4

    How can I combine templated derived class in CRTP with derived class expression templates?

  5. 5

    How can I find out which method of a derived class is not implemented?

  6. 6

    how can I obtain HWND of a class derived from QMainWindow

  7. 7

    How can I do constructor overloading in a derived class in TypeScript?

  8. 8

    How can I retrieve the derived class from a list of abstract classes?

  9. 9

    how can I obtain HWND of a class derived from QMainWindow

  10. 10

    How can I find out which method of a derived class is not implemented?

  11. 11

    How can I retrieve the derived class from a list of abstract classes?

  12. 12

    C++ How can I return an unknown derived class?

  13. 13

    How can I access a method defined with new keyword in a derived class

  14. 14

    How can I call the constructor of the derived class when using CRTP?

  15. 15

    How do I change an element in a parent class into an attribute in a derived class with JAXB annotations?

  16. 16

    How can I store in a derived class information obtained during initialization of a base class?

  17. 17

    How can I create an instance of a derived class from an instance of a base class and include private fields?

  18. 18

    How can I initialize an instance of a derived class from an instance of the base class?

  19. 19

    How can I have an object of a derived class "reference" members of an instance of the base class?

  20. 20

    How can I store in a derived class information obtained during initialization of a base class?

  21. 21

    How can I specify a class attribute in new_tag()?

  22. 22

    How can I cache API data in a class attribute?

  23. 23

    No attribute 'compile', how can I modify the class, so that it works?

  24. 24

    How can I type hint a dynamically set class attribute in a metaclass?

  25. 25

    How can I save EditText input as class attribute in Kotlin/AndroidStudio?

  26. 26

    How can I select same attribute without using class or id?

  27. 27

    Cython: how can I set the attribute of a cdef class?

  28. 28

    How can I use setAttribute on an input without an ID or class attribute?

  29. 29

    How can I add a class, id or attribute to a twig include?

HotTag

Archive