Using JsonConvert.DeserializeObject on Nested Object
Asked Answered
K

1

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; }
        }
    }
}
Kasiekask answered 4/2, 2016 at 16:13 Comment(2)
But what exception is arisen? the code looks goot to me, it should workArgyres
nonono, I see the problemArgyres
A
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; }

        }
    }
Argyres answered 4/2, 2016 at 16:25 Comment(1)
Thank you! That is exactly what I was trying to accomplish and seeing it now, it makes perfect sense.Kasiekask

© 2022 - 2024 — McMap. All rights reserved.