2

I can't find a way to serialize with JSON.NET a list of derived lists, and I'm starting to wonder if it's even possible. Let me explain the situation with some code.

I've created a new class which is derived from a list of a specific object (tried first with a generic one) and added a string property NomGroupe:

public class GroupeActes : List<Acte>
{
    public string NomGroupe { get; set; }

    public GroupeActes(string nom, List<Acte> liste)
    {
        NomGroupe = nom;

        foreach (var acte in liste)
        {
            this.Add(acte);
        }
    }
}

Then, in my code, I've declared a list of this class (List<GroupeActes> listOfGroupeActes) and I fill it with data. For the serialization, I use this code:

        JsonSerializer serializer = new JsonSerializer();
        serializer.TypeNameHandling = TypeNameHandling.All;
        using (StreamWriter sw = new StreamWriter(@"listOfGroupeActes.json"))
        using (JsonWriter writer = new JsonTextWriter(sw))
        {
            serializer.Serialize(writer, listOfGroupeActes);
        }

I've tried with and without the TypeNameHandling.All parameter and with several combination of Json.net properties and even with DataContract/DataMember.

So far, I only managed to get in my json file either the data of each nested List<Acte> without the NomGroupe property, or the other way around. But not both, which is what I'd like to have.

Two questions then:

  • Is it even possible?
  • If yes, how can I do it?

Thanks for your help!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
LaPointe
  • 23
  • 3
  • Can you not use JsonConvert.SerializeObject()? – gmn Sep 09 '16 at 08:56
  • No, the issue is the same sadly. The `NomGroupe` property is then ignored. – LaPointe Sep 09 '16 at 09:40
  • Looks like a duplicate of [JSON serialize properties on class inheriting list](https://stackoverflow.com/questions/35439335) and/or [How do I get json.net to serialize members of a class deriving from List?](https://stackoverflow.com/questions/21265629), both of which have workarounds. – dbc Sep 09 '16 at 10:07
  • Indeed, I haven't found these despite a lot of research... Thanks, that's interesting! – LaPointe Sep 09 '16 at 10:15

2 Answers2

1

You don't want to inherit from List<T>.

Create a list property instead:

public class GroupeActes
{
    public List<Acte> Actes { get; set; }

    public string NomGroupe { get; set; }

    public GroupeActes(string nom, List<Acte> liste)
    {
        NomGroupe = nom;

        Actes.AddRange(acte);           
    }
}

Lists (and other collection types) get special treatment while serializing. You don't want the collection type's public properties (such as Capacity and Count) in your output, so the property you added through inheritance won't be serialized either.

A collection is serialized like this:

if o is IEnumerable
    foreach object s in o
        serialize o

So the serializer won't even look at your enumerable's properties.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Sadly, I need to inherit from a list for grouping into a listview in a Xamarin.Forms project [link](https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/customizing-list-appearance/). So are you saying that serializing an object like this with JSON.NET is not possible at all? – LaPointe Sep 09 '16 at 09:43
  • Does your serialization have anything to do with the displaying on the list? If not, use the class above for serializing, and map it into the one from your question for displaying. – CodeCaster Sep 09 '16 at 09:45
  • I intend to use the serialization only to store the data. So, yes, if JSON.NET cannot do this, this is the solution I would use. Thanks, I will accept your answer. – LaPointe Sep 09 '16 at 09:49
0

Try to use Newtonsoft Json.NET

string itemToSend = JsonConvert.SerializeObject(dataModel);
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28