5
[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.

Community
  • 1
  • 1
gogcam
  • 149
  • 9
  • 2
    Out of curiosity, why are you making a class derived from list? It is extremely rare to ever need to do that. It makes much more sense to make a class that contains both a list and a double instead of extending list. See the SO question "[Why not inherit from List?](http://stackoverflow.com/questions/21692193/why-not-inherit-from-listt)" and the very good answer by Eric Lippert. – Scott Chamberlain May 03 '16 at 16:00
  • 1
    It turns out Json.NET doesn't support serialize collections as objects when `[DataContact]` is applied. See [Deserializing an `IEnumerable` with `[DataContract]` applied does not work](http://stackoverflow.com/questions/35778811). – dbc May 03 '16 at 16:21
  • @dbc Interesting answer, I notice you say *It would have been able to add members if your class implemented `ICollection`*, but List does implement that, a bug? – Alexander Derck May 03 '16 at 16:26
  • 2
    There's a second issue, namely that JSON has 2 different types of container, array: `[1, 2, ..., n]` and object: `{"prop1": 1, ..., "propN": n}`. If Json.NET serializes your type as an object you lose items. If as an array you lose properties. You need a special converter to get both. See http://stackoverflow.com/questions/35439335/json-serialize-properties-on-class-inheriting-list or http://stackoverflow.com/questions/21265629/how-do-i-get-json-net-to-serialize-members-of-a-class-deriving-from-listt. – dbc May 03 '16 at 16:30
  • @ScottChamberlain I need this for a complex grouping in Xamarin Forms in a ListView ([link](https://forums.xamarin.com/discussion/17976/listview-grouping-example-please)). I can't understand why Xamarin wants it in this "inherit way" and not with a property `List` (as usual). – gogcam May 03 '16 at 21:26

1 Answers1

2

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.

Community
  • 1
  • 1
gogcam
  • 149
  • 9