0

I want to convert Deserialize my static class with json setting file.

public class Welcome
{
    public ConfigurationProvider ConfigurationProvider { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class ConfigurationProvider
{
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    public static AppConfig Rcom { get;  }
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    public static AppConfig Ccst { get;  }
    
    static ConfigurationProvider()
    {
        Rcom=new AppConfig("RCOM");
        Ccst=new AppConfig("CCST");
    }
    

}

public class AppConfig
{
    public GarConfiguration GarConfiguration { get;  }
    public VacationRentalRedisKeysConfiguration VacationRentalRedisKeysConfiguration { get;  }

    public AppConfig(string type)
    {
        if (type == "RCOM")
        {
            GarConfiguration = new GarConfiguration(type);
            VacationRentalRedisKeysConfiguration = new VacationRentalRedisKeysConfiguration
            {
                DefaultVacationRentalThemeKey = "RcomKey",
                 CheckVacationRentalThemeKey=true
            };
        }
        else
        {
            GarConfiguration = new GarConfiguration(type);
            VacationRentalRedisKeysConfiguration = new VacationRentalRedisKeysConfiguration
            {
                DefaultVacationRentalThemeKey = "CCSTKey",
                CheckVacationRentalThemeKey=false
            };
        }
       
    }
}
public class GarConfiguration
{
    public GarSearch GarSearch { get; }

    public GarConfiguration(string type)
    {
        if (type == "RCOM")
        {
            GarSearch = new GarSearch
            {
                RequestSplitedMinCount = 40,
                RequestSplitedCount = 5,
                RequestSplitedMaxTimeOutInMilliSecond = 3000,
                RequiredSplitedRequest = true,
                RequiredSplitedRequestMaxTimeOut = true,
                AppName = type
            };
        }
        else
        {
            GarSearch = new GarSearch
            {
                RequestSplitedMinCount = 40,
                RequestSplitedCount = 5,
                RequestSplitedMaxTimeOutInMilliSecond = 5000,
                RequiredSplitedRequest = true,
                RequiredSplitedRequestMaxTimeOut = true,
                AppName = type
            };
        }
    }
}
public class GarSearch
{
    public long RequestSplitedMinCount { get; set; }
    public long RequestSplitedCount { get; set; }
    public long RequestSplitedMaxTimeOutInMilliSecond { get; set; }
    public bool RequiredSplitedRequest { get; set; }
    public bool RequiredSplitedRequestMaxTimeOut { get; set; }
    public string AppName { get; set; }
}


public class VacationRentalRedisKeysConfiguration
{
    public bool CheckVacationRentalThemeKey { get; set; }
    public string DefaultVacationRentalThemeKey { get; set; }
}

Above is my sample class.

I need this config because if I am not getting the JSON setting file then it runs from this default setting else it will run from that JSON setting file.

I need this static because not want to create multiple instance for this.

when I Deserialize my class I get result like class Deserialize proeprly but not able to access static member Not able to get static memeber values

Chirag Patel
  • 3
  • 1
  • 5

1 Answers1

0

I may have misunderstood your question because your title says "DeserializeObject With Static Class" but the body says "Deserialize my static class"

If you meant to deserialize a static class, you can't deserialize or serialize a static class because There are never any instances of static class if you want more explanation go here

If you meant you want to convert an object to a JSON string (you can use the static method below)

using Newtonsoft.Json;

        public static string ConvertObjectToJson<T>(T Ls)
        {
            if (Ls == null)
                return null;
            return JsonConvert.SerializeObject(Ls);


        }

If you meant you want to convert a JSON string to an object (you can use the static method below)

        public static T JasonConvertToObj<T>(string JsonStr , T FalseReturn)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(JsonStr);
            }
            catch (Exception e)
            {
                string erorstr = e.Message;
                return FalseReturn;
            }
        }

If I still didn't answer your question let me know. and if there are errors in my code I would like to know although if I remember correctly I used it.

Yovel
  • 93
  • 7