Help to compare Json objects in Java and get a Json of differences!

2

I have the following function that a Json generates with all the contents of a directory:

  public static void generateReport(File dir, JSONArray content) throws IOException, ParseException {
        File listFile[] = dir.listFiles();
        String md5;
        if (listFile != null && listFile.length > 0) {
            for (int i = 0; i < listFile.length; i++) {
                JSONObject obj = new JSONObject();
                content.add(obj);
                if (listFile[i].isDirectory()) {
                    md5 = md5OfString(listFile[i].getAbsolutePath());
                    obj.put("md5", md5);
                    obj.put("type", "folder");
                    obj.put("path", listFile[i].getAbsolutePath());
                    JSONArray contentSon = new JSONArray();
                    obj.put("Content", contentSon);
                    generateReport(listFile[i], contentSon);
                    //numDir++;
                } else if (listFile[i].isFile()) {
                    md5 = md5OfFile(listFile[i]);
                    obj.put("md5", md5);
                    obj.put("type", "file");
                    obj.put("path", listFile[i].getAbsolutePath());
                    //numFiles++;
                }
            }
        }
    }

Calling this function twice, with different parameters, would generate two JSONs with different data. What I need is to compare both generated JSONs and generate a new JSON with the differences between the other two.

Ex of report of differences:

{
    "root": "/u/data/Coldview/apps",
    "origin-report": "informe.json",
    "diff": [
        {
            "type": "folder",
            "path": "ldf.web.ejecuciondeprotestos",
            "origin-md5": ".........",
            "destination-md5": "........."
        },
        {
            "type": "folder",
            "path": "ldf.web.ejecuciondeprotestos/lib",
            "origin-md5": ".........",
            "destination-md5": "........."
        },
        {
            "type": "file", 
            "name": "AMCOX.Core.jar", 
            "origin-md5": ".........",
            "destination-md5": "........."
        }
    ]
}

What I do not know is how to compare my two JSON objects and get the differences between them.

I'm using the Json-Simple library

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

but it does not seem to have any method for what I need because of what I'm seeing if I have to change the library or that!

Thanks for the help, advice, etc.

Greetings!

    
asked by Nacho Zve De La Torre 24.09.2018 в 20:26
source

1 answer

0

One thing I did and that works wonders is to read both objects in HashMap and then compare them with a regular assertEquals (). It will call the equals () method of the hashmaps, which will recursively compare all the objects that are inside (they will be other hashmaps or some unique value object such as a string or an integer). This was done using the Codehaus' Jackson JSON parser ...

assertEquals (mapper.readValue (expectedJson, new TypeReference <HashMap <String, Object >> () {}), mapper.readValue (actualJson, nueva TypeReference <HashMap <String, Object >> () {}));

A similar approach can be used if the JSON object is a matrix instead.

    
answered by 24.09.2018 в 20:35