I have a dictionary that looks like this that gets Deserialized from a json file to a C# Dictionary
{
"Name": "option1",
"Settings": {
"setting1": 20,
"setting2": 2
}
}
So I Deserialize this to an object which works but when i try to get that value out of the dictionary it becomes a long instead of an int
if (!settings.TryGetValue("setting1", out object setting))
{
setting = 10;
}
Then somewhere else I do something like this:
if ((int)setting > 10) {//do something}
Then it gives me the error that an int64 cant be converted to an int32
Deserialization process:
using (StreamReader reader = new StreamReader(Startup.SettingsPath))
{
SettingsModel settings = JsonConvert.DeserializeObject<SettingsModel>(reader.ReadToEnd()).Applications;
return settings
}
SettingsModel:
public class SettingsModel
{
public string Name { get; set; }
public IDictionary<string, object> Settings { get; set; }
}