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?