6

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; }
        }
    }
}
0

1 Answer 1

8

You are trying to serialize/deserialize the Settings class which does not have any property, for the structure you created you must serialize/deserialize the class RootObject.

If you want to use the Settings class change it to this:

    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; }
            }

            [JsonProperty("webserver")]
            public Webserver webserver { get; set; }

            [JsonProperty("gameserver")]
            public Gameserver gameserver { get; set; }

        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That is exactly what I was trying to accomplish and seeing it now, it makes perfect sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.