1

I deserialize a json string that was created by the serialize method. The JSON (from the Newtonsoft library) looks like this (a little bit formatted for better reading):

{"AreaKindList":
[
    {"ID":1,"Kind":"Lioncage"},
    {"ID":2,"Kind":"Open-Air Enclosure"}
],
"AnimalRaceList":
[
    {"ID":1,"Race":"Lion","AllowedAreaKinds":[1]},
    {"ID":2,"Race":"Gazelle","AllowedAreaKinds":[2]},
    {"ID":3,"Race":"Antelope","AllowedAreaKinds":[2]}
],
"AreaList":
[
    {"ID":1,"AreaKindId":1,"Size":2,"AnimalList":
                                    [
                                        {"Name":"Leo","RaceId":1}
                                    ]
    }
]}

And the classes look like this:

public class Zoo
{
    public List<AreaKind> AreaKindList { get; set; }
    public List<AnimalRace> AnimalRaceList { get; set; }
    public List<Area> AreaList { get; set; }
}

public class AreaKind
{
    public int ID { get; private set; }
    public string Kind { get; private set; }
    public AreaKind(int id, string kind)
    {
        ID = id;
        Kind = kind;
    }
}

public class AnimalRace
{
    public int ID { get; private set; }
    public string Race { get; private set; }
    public List<int> AllowedAreaKinds { get; private set; }
    public AnimalRace (int id, string race, List<int> areaKindIds)
    {
        ID = id;
        Race = race;
        AllowedAreaKinds = areaKindIds;
    }
}

public class Area
{
    public int ID { get; private set; }
    public int AreaKindId { get; set; }
    public int Size { get; set; }
    public List<Animal> AnimalList { get; set; }
    public Area(int size, int areaKindId, int id)
    {
        ID = id;
        AreaKindId = areaKindId;
        Size = size;
    }
}

As you can see there is a list of numbers inside the AnimalRaceList, named AllowedAreaKinds. At this moment there is only 1 entry inside. When I use the following code to deserialize the zoo-object the zoo is completly filled (including the animal Leo with the ID of 1), but the "List AllowedAreaKinds" is completly set to "NULL". Why?

using (StreamReader reader = File.OpenText(Program.JsonFile))
{
    JsonSerializer serializer = new JsonSerializer();
    Zoo tempZoo = (Zoo) serializer.Deserialize(reader, typeof(Zoo));
}

I even changed the serializer against a Populate(), but that gave the same result. Anyone an idea what it could be?

Marcel Grüger
  • 885
  • 1
  • 9
  • 25
  • 2
    The name of the constructor argument `areaKindIds` does not match the JSON property name `AllowedAreaKinds`, so the JSON is not bound successfully to the argument. See [JSON.net: how to deserialize without using the default constructor?](https://stackoverflow.com/a/23017892) which states *It is important that the constructor parameter names match the corresponding property names of the JSON object (ignoring case) for this to work correctly.* Also related or duplicate: [Avoiding default constructors and public property setters](https://stackoverflow.com/q/35245256). – dbc Apr 26 '18 at 08:39
  • You should have postet this as answer, not as comment, so I cannot close this question. But thanks :) – Marcel Grüger Apr 26 '18 at 08:44
  • Glad I could help. I'll mark it as a duplicate of those two. – dbc Apr 26 '18 at 08:45

0 Answers0