Serialize object when the object inherits from list

gogcam
[DataContract]
public class A : List<B>
{
    [DataMember]
    public double TestA { get; set; }
}

[DataContract]
public class B
{
    [DataMember]
    public double TestB { get; set; }
}

With the model above I try to serialize the following object:

List<A> list = new List<A>()
{
    new A() { TestA = 1 },
    new A() { TestA = 3 }
};

json = JsonConvert.SerializeObject(list);
//json: [[],[]]

Where are my two values from TestA? It's possible duplicate from this thread (XML), but I want to know if there is no option to include those values by setting some JSON serialize option?

Note: Creating a property List<B> in class A instead of inheritance is no option for me.

gogcam

According to the comments above (thanks!) there are two ways to get a correct result:

  • Implementing a custom JsonConverter (see here)
  • Workarround: Create a property in the class which returns the items (see here)

Anyway, inherit from List<T> is rare to be a good solution (see here)

I've tried it with the workarround:

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class A : List<B>
{
    [JsonProperty]
    public double TestA { get; set; }

    [JsonProperty]
    public B[] Items
    {
        get
        {
            return this.ToArray();
        }
        set
        {
            if (value != null)
                this.AddRange(value);
        }
    }
}

public class B
{
    public double TestB { get; set; }
}

This works for serialization and deserialization. Important: Items must be an Array of B and no List<B>. Otherwise deserialization doesn't work for Items.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Serialize member as Object or List<Object>

From Dev

Serialize member as Object or List<Object>

From Dev

Serialize object from within

From Dev

Serialize object from within

From Dev

RavenDB issue storing object that inherits List<T>

From Dev

Serialize an object that contains a list of objects

From Dev

When Class Inherits from built-in List

From Dev

Error when passing an object that implements interface X to an interface Y that inherits from interface X. in C#

From Dev

Casting object list which inherits type to new interface type list

From Java

Python class inherits object

From Dev

custom object list json serialize in python

From Dev

JSON Serialize List<KeyValuePair<string, object>>

From Dev

C# Serialize an object with a list of objects in it

From Dev

custom object list json serialize in python

From Dev

How to serialize a List(of Object) in VB.NET?

From Dev

C++ serialize an object list to XML with for loop

From Dev

How can an object be accessed that inherits from a generic class?

From Dev

How to ensure a unique ID for each object that inherits from a prototype in JavaScript?

From Dev

Why Function's prototype is a function and why Object inherits from there?

From Dev

Polymorphism - Using an object that inherits from an interface rather than the interface directly

From Java

Visual Studio how to serialize object from debugger

From Dev

Unable to serialize JSON object from file

From Dev

Unable to serialize JSON object from MongoDB in python

From Dev

"it's not the same object as" error when trying to serialize an object

From Dev

Serialize(TextWriter, Object) vs Serialize(XmlWriter, Object)

From Dev

Create list of object members from object list

From Dev

Automapper from list of object

From Dev

How to serialize a List<object> to JSON set of properties (not JSON array)

From Dev

How to serialize object with List inside in C# using XElement?

Related Related

  1. 1

    Serialize member as Object or List<Object>

  2. 2

    Serialize member as Object or List<Object>

  3. 3

    Serialize object from within

  4. 4

    Serialize object from within

  5. 5

    RavenDB issue storing object that inherits List<T>

  6. 6

    Serialize an object that contains a list of objects

  7. 7

    When Class Inherits from built-in List

  8. 8

    Error when passing an object that implements interface X to an interface Y that inherits from interface X. in C#

  9. 9

    Casting object list which inherits type to new interface type list

  10. 10

    Python class inherits object

  11. 11

    custom object list json serialize in python

  12. 12

    JSON Serialize List<KeyValuePair<string, object>>

  13. 13

    C# Serialize an object with a list of objects in it

  14. 14

    custom object list json serialize in python

  15. 15

    How to serialize a List(of Object) in VB.NET?

  16. 16

    C++ serialize an object list to XML with for loop

  17. 17

    How can an object be accessed that inherits from a generic class?

  18. 18

    How to ensure a unique ID for each object that inherits from a prototype in JavaScript?

  19. 19

    Why Function's prototype is a function and why Object inherits from there?

  20. 20

    Polymorphism - Using an object that inherits from an interface rather than the interface directly

  21. 21

    Visual Studio how to serialize object from debugger

  22. 22

    Unable to serialize JSON object from file

  23. 23

    Unable to serialize JSON object from MongoDB in python

  24. 24

    "it's not the same object as" error when trying to serialize an object

  25. 25

    Serialize(TextWriter, Object) vs Serialize(XmlWriter, Object)

  26. 26

    Create list of object members from object list

  27. 27

    Automapper from list of object

  28. 28

    How to serialize a List<object> to JSON set of properties (not JSON array)

  29. 29

    How to serialize object with List inside in C# using XElement?

HotTag

Archive