Actually going schemaless in C#

JSON Image
Image from json.org

Back in August I talked about going schemaless and I’ve made some progress since then. I’ve created a number of C# classes, many of which include single and in one cases a 2D array of things I want persisted to disk.

I use JSON (JavaScript Object Notation) files to hold the data.  So long as I’ve defined those classes with Properties using Get and Set, the JSON converters in .NET handles it all very nicely.

For example, in my game I have a CityArea class which includes a List<BuildingType> as well as several public properties.

This saved out as JSON like this:

[
  [
    {
      "buildings": [
        {
          "Name": "College",
          "Size": 2,
          "Percent": 60,
          "Cost": 50,
          "Type": 11
        },
        {
          "Name": "Gallery",
          "Size": 2,
          "Percent": 40,
          "Cost": 150,
          "Type": 23
        },
and ends...
      ],
      "State": "R",
      "Type": "F",
      "Density": "L",
      "Points": 46,
      "AreaName": "Newfane"
    },

The whole JSON file has 1500 buildings scattered amongst 100 areas (10 x 10) and is 11422 lines long. The file is 234Kb long. Though that’s with indentations. I could remove that but for now, during development, it makes the file easier to read.

These are the two class definitions (I’ve removed extraneous stuff from them),

class BuildingType : ICloneable {
        public string Name { get; set; } = "";
        public BuildingSize Size { get; set; } = BuildingSize.bsNone;
        public int Percent { get; set; }
        public int Cost { get; set; }
        public BuildType Type { get; set; }
}

class CityArea {
        public char State { get; set; }
        public char Type { get; set; }
        public char Density { get; set; }
        public int Points { get; set; }
        public string AreaName { get; set; }
        public static List AllBuildings = new List();		
}

This is the code to save out areas (which is List<CityArea> areas to the file. I found this by reading the Microsoft documentation about Serialization and deserialization. It’s pretty good stuff and the code for saving these structures are these pretty short methods.

        public bool SaveToUTF8(string filename)
        {
            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };
            var bytes = JsonSerializer.SerializeToUtf8Bytes(areas, options);
            File.WriteAllBytes(filename, bytes);
            return true;
        }

and this is the code to Load it.

        public bool Load(string filename)
        {
            try
            {
                var bytes = File.ReadAllText(filename);
                areas = JsonSerializer.Deserialize<CityArea[][]>(bytes);
            }catch(Exception e)
            {
                Error = $"Failed to load {filename} because {e.Message}";
                return false;
            }
            return true;
        }

I haven’t timed either Load or Save but they are fractions of a second.