I want to use JSON.Net to handle parsing a configuration file when my application is loaded. Keeping all KVP's in the same scope works absolutely fine. I would however like to break it down into sub-categories such as Settings.WebServer, Settings.GameServer, etc.
I would like to be able to reference various settings in that manner for readability sake, such as Settings.WebServer.hostname. Currently trying to factor in WebServer/GameServer is throwing this off. Can anyone help with what can be done to get this working?
JSON
{
"webserver":
{
"hostname": "localhost",
"port": "8888"
},
"gameserver":
{
"hostname": "123.123.123.123",
"port": "27015",
"password": "as@c!qi$"
}
}
C# Main
Settings settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(@".\Configs\settings.cfg"));
C# Settings Class
namespace SourceMonitor
{
public class Settings
{
public class Webserver
{
[JsonProperty("hostname")]
public string hostname { get; set; }
[JsonProperty("port")]
public string port { get; set; }
}
public class Gameserver
{
[JsonProperty("hostname")]
public string hostname { get; set; }
[JsonProperty("port")]
public string port { get; set; }
[JsonProperty("password")]
public string password { get; set; }
}
public class RootObject
{
[JsonProperty("webserver")]
public Webserver webserver { get; set; }
[JsonProperty("gameserver")]
public Gameserver gameserver { get; set; }
}
}
}