I have a json that is read locally and is used in my program. Now I want the local json to be checked with the json from the api url and if it is not equal to the local json file it will be downloaded. Only the biggest problem is that it doesn't execute the code of the two if statements in the check! What is wrong with my code for this check?
This is what I tried to use
public static rootObject LoadJsonLocal()
{
rootObject rootObject = new rootObject();
var path = pathToJson();
string file;
using (StreamReader r = new StreamReader(path))
{
file = r.ReadToEnd();
rootObject = JsonSerializer.Deserialize<rootObject>(file);
}
return rootObject;
}
public static string pathToJson()
{
string extractPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string path = extractPath + "/" + "getlatest.json";
return path;
}
public static async Task UpdateOrDownloadJson()
{
try
{
string str = "url";
WebClient webClient = new WebClient();
webClient.Headers.Add("Authorization", await Header.getAuthorizationHeader());
string JsonSting = webClient.DownloadString(str);
if (JsonSting.Equals(LoadJsonLocal()))
{
Console.WriteLine("Json is equal");
}
// json is not equal download it
if (!JsonSting.Equals(LoadJsonLocal()))
{
await Downloader.DownloadJson();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
JsonSting.Equals(LoadJsonLocal())you are doing a string comparison, when your function LoadJsonLocal() is returning a deserialized object. Have you implementedoverridden Equalsfor your json model class ? If not, then comparison is not going to work.