0

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());
    }
}
4
  • JsonSting.Equals(LoadJsonLocal()) you are doing a string comparison, when your function LoadJsonLocal() is returning a deserialized object. Have you implemented overridden Equals for your json model class ? If not, then comparison is not going to work. Commented Feb 7, 2022 at 10:50
  • what do i need to add code wise then ? @AnandSowmithiran Commented Feb 7, 2022 at 10:53
  • JsonSting itself you got from the downloaded string from URL, so what is the point of comparing and deciding to again download or not ? Commented Feb 7, 2022 at 10:56
  • because I want if the json designed locally with the json online think for example text that he then download it again Commented Feb 7, 2022 at 11:02

1 Answer 1

0

You are comparing the downloaded json string to the already present local json deserialized to object form, they are not going to get compared correctly. You should override the Equals() method for your JSON model object[you named it rootObject] so that the comparison can be proper - between local json deserialized into object form and downloaded string deserialized into object form. To use Equals() or to implement custom equality comparer refer this SO answer

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.