none
Problems serialising JSON to file RRS feed

  • Question

  • I have my JSON file containing my configuration data.

    {
        "ACLSettings": {
            "UserId": "user1",
            "UserPassword": "password",
            "WebApiVersion": "1.0",
            "WebApiEndpointName": "XApi",
            "WebApiHostName": "localhost",
            "UseHTTPS": "N",
            "LogFilePath": "./ACLWebApiLogs/",
        }
    }

    When the user clicks Save, the button handler saves thedata to a JSON string and writes it to a file with this code:

           private void btnSaveConfig_Click(object sender, EventArgs e)
            {
                aCLSettings.UserId = txtUserId.Text;
                aCLSettings.UserPassword = txtPassword.Text;
                aCLSettings.WebApiVersion = txtWebApiVersion.Text;
                aCLSettings.WebApiEndpointName = txtWebApiEndpoint.Text;
                aCLSettings.WebApiHostName = txtWebApiHostname.Text;
                aCLSettings.UseHTTPS = txtUseHTTPS.Text;
                aCLSettings.LogFilePath = txtLogFilePath.Text;
    
                var outputFile = JsonConvert.SerializeObject(aCLSettings);
                File.WriteAllText(fileName, outputFile);
            }

    The above code saves the data to the open file in this format:

    {"UserId":"user1","UserPassword":"password123","WebApiVersion":"1.0","WebApiEndpointName":"WebApi","WebApiHostName":"localhost","UseHTTPS":"N","LogFilePath":"./ACLWebApiLogs/"}

    If I try and reload the JSON it fails because the ACLSettings is missing in the file. If I want the serialiser to output it so that is is the same format as the initial file, how can I do that?

    Monday, July 23, 2018 9:07 AM

Answers

  • It strongly depends on what you haven't posted, the Aclsettings classes. When using Edit\Paste Special\Paste JSON As Classes, then you'll get:

    public class Rootobject
    {
    	public Aclsettings ACLSettings { get; set; }
    }
    
    public class Aclsettings
    {
    	public string UserId { get; set; }
    	public string UserPassword { get; set; }
    	public string WebApiVersion { get; set; }
    	public string WebApiEndpointName { get; set; }
    	public string WebApiHostName { get; set; }
    	public string UseHTTPS { get; set; }
    	public string LogFilePath { get; set; }
    }

    I guess you don't have the correct class hierarchy. E.g.

    namespace ConsoleCS
    {
        using Newtonsoft.Json;
        using System;
        using System.IO;
    
        public class Rootobject
        {
            private AclSettings aclSettings;
            [JsonProperty("ACLSettings")]
            public AclSettings AclSettings
            {
                get
                {
                    if (this.aclSettings == null) { this.aclSettings = new AclSettings(); }
                    return this.aclSettings;
                }
                private set => aclSettings = value;
            }
        }
    
        [JsonObject(ItemTypeNameHandling = TypeNameHandling.Objects)]
        public class AclSettings
        {
            public string UserId { get; set; }
            public string UserPassword { get; set; }
            public string WebApiVersion { get; set; }
            public string WebApiEndpointName { get; set; }
            public string WebApiHostName { get; set; }
            public string UseHTTPS { get; set; }
            public string LogFilePath { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Rootobject rootObject = new Rootobject();
                rootObject.AclSettings.UserId = "txtUserId.Text";
                rootObject.AclSettings.UserPassword = "txtPassword.Text";
                rootObject.AclSettings.WebApiVersion = "txtWebApiVersion.Text";
                rootObject.AclSettings.WebApiEndpointName = "txtWebApiEndpoint.Text";
                rootObject.AclSettings.WebApiHostName = "txtWebApiHostname.Text";
                rootObject.AclSettings.UseHTTPS = "txtUseHTTPS.Text";
                rootObject.AclSettings.LogFilePath = "txtLogFilePath.Text";
                File.WriteAllText(@"C:\Temp\Test.json", JsonConvert.SerializeObject(rootObject));
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    }
    

    Monday, July 23, 2018 10:14 AM

All replies

  • It strongly depends on what you haven't posted, the Aclsettings classes. When using Edit\Paste Special\Paste JSON As Classes, then you'll get:

    public class Rootobject
    {
    	public Aclsettings ACLSettings { get; set; }
    }
    
    public class Aclsettings
    {
    	public string UserId { get; set; }
    	public string UserPassword { get; set; }
    	public string WebApiVersion { get; set; }
    	public string WebApiEndpointName { get; set; }
    	public string WebApiHostName { get; set; }
    	public string UseHTTPS { get; set; }
    	public string LogFilePath { get; set; }
    }

    I guess you don't have the correct class hierarchy. E.g.

    namespace ConsoleCS
    {
        using Newtonsoft.Json;
        using System;
        using System.IO;
    
        public class Rootobject
        {
            private AclSettings aclSettings;
            [JsonProperty("ACLSettings")]
            public AclSettings AclSettings
            {
                get
                {
                    if (this.aclSettings == null) { this.aclSettings = new AclSettings(); }
                    return this.aclSettings;
                }
                private set => aclSettings = value;
            }
        }
    
        [JsonObject(ItemTypeNameHandling = TypeNameHandling.Objects)]
        public class AclSettings
        {
            public string UserId { get; set; }
            public string UserPassword { get; set; }
            public string WebApiVersion { get; set; }
            public string WebApiEndpointName { get; set; }
            public string WebApiHostName { get; set; }
            public string UseHTTPS { get; set; }
            public string LogFilePath { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Rootobject rootObject = new Rootobject();
                rootObject.AclSettings.UserId = "txtUserId.Text";
                rootObject.AclSettings.UserPassword = "txtPassword.Text";
                rootObject.AclSettings.WebApiVersion = "txtWebApiVersion.Text";
                rootObject.AclSettings.WebApiEndpointName = "txtWebApiEndpoint.Text";
                rootObject.AclSettings.WebApiHostName = "txtWebApiHostname.Text";
                rootObject.AclSettings.UseHTTPS = "txtUseHTTPS.Text";
                rootObject.AclSettings.LogFilePath = "txtLogFilePath.Text";
                File.WriteAllText(@"C:\Temp\Test.json", JsonConvert.SerializeObject(rootObject));
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
        }
    }
    

    Monday, July 23, 2018 10:14 AM
  • Hi Isavidge,

    In addition, you could refer to a simple way to get the classes associated to the JSON data.

    Copy your JSON data> right click you project>Paste Special>Paste JSON As classes

    After that you could get the correct classes correctly.

    Best Regard,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 24, 2018 8:23 AM
    Moderator
  • Exactly, wrote that also :)
    Tuesday, July 24, 2018 8:38 AM
  • Yes, I know about that and that is what I used to get the class in the first place.
    Thursday, July 26, 2018 8:09 AM
  • As I already wrote, you have the correct class hierarchy, but you're serializing the wrong object.

    Thursday, July 26, 2018 12:06 PM