✅ Newtonsoft.JSON correctly serializes data but erroneously leaves out arrays during deserialization

My simplified code is as follows:

public class Building
    {        
        public string Key;

        public Building(string key)
        {
            Key = key;
        }
    }


public class Game
    {
        public Game(object _)
                {
                        buildings = new Building[]
            {
                new Building("extractor")
            };
        }
        private Building[] buildings;

        public Building[] Buildings => buildings;
    }


After serializing the
Game
class, I end up with:

{
  "Buildings": [
    {
      "Key": "extractor"
    }
  ]
}


This is correct, but deserializing the
Game
class from this json results in this data:

Game
  -> buildings: null


The buildings array has been lost during deserialization. This same behaviour happens with the built-in
JsonSerializer
.
Was this page helpful?