Skip to content

How to use the JSON packaging

Hk-tang edited this page May 21, 2020 · 6 revisions

The JSON that is outputted by the packaging part of the application looks like this:

This example is one part of the data/example-results.json file

[
{Tangled: "", 
"Comment": "Sure, I know these methods. But they only allow to do UI Changes **before** or **after** the task ran.
`onProgressUpdate()`can be uses **during** the execution but regarding to the docu it is not guranteed when this method will run.
The background thread will continue its work and not wait for `onProgressUpdate()` to return. But this is what I am looking for.
Thus in this special case these methods does not help, do they?", 
"Answer Id": 24548749, 
"Confirmed": "1", 
"Tags": " <java><android><multithreading><android-asynct...", 
"After": [
"    public class TestTask extends AsyncTask{\\n
        private boolean intermediary;\\n
        @Override\\n
        protected Object doInBackground(Object... arg0) {\\n
            Log.d(\\\"First\\\", \\\"First\\\");\\n
            onProgressUpdate(null);\\n
            while(!intermediary){ try{Thread.sleep(1000);} catch(Exception e){} }\\n
            Log.d(\\\"Second\\\", \\\"Second\\\");\\n
            return null;\\n
        }\\n
        @Override \\n
        protected void onProgressUpdate(Object... args){       \\n
            // Check stuff on the UI\\n
            intermediary = true;\\n
        }\\n
    }
"],
"Category": "", 
"Question Id": 24548624, 
"Comment Id": 38018547, 
"Edit Id": 68570698, 
"Useful": "0", 
"Before": []
}
]

The packaging puts each result from running the program as a single element of this list. i.e., the JSON file is a single JSON array where each element is a JSON Object representing a single result. The fields of the JSON Object are described here:

[
{Tangled: "1/0",   Tangled is either 1 or 0. 1 if it was rated as tangled and 0 if not
"Comment": "",     The Comment that triggered the edit 
"Answer Id": "",   The answer id can be used to find the answer on the website or in the database
"Confirmed": "",   1 or 0 depending on what it was rated
"Tags": "",        This will be a string of all the tags the question was tagged as on Stack Overflow
"After": [],       This will be a string containing the code snippet after the change including the surrounding code for context
"Category": "",    This is the category that is assigned
"Question Id": "", The question id can be used to find the question on the website or in the database
"Comment Id": "",  The comment id can be used to find the comment only in the database
"Edit Id": "",     The edit id can be used to find the comment only in the database
"Useful": "",      1 or 0 depending on what it was rated
"Before": []       This is a string that contains the code snippet before the change including the context code 
}
]

Before and After are both lists in the even that there are multiple relevant code snippets that changed. Each code snippet will be its own element in the Before and After lists.

A general idea of how to work with this JSON will be to open the file and read the contents as a single JSON array. Then if you'd like you can loop through each element or turn it into a data frame Python: df = pandas.DataFrame(JSONArray). You have the ability to filter based on tags, or on useful, confirmed, tangled, etc. and can calculate statistics based on the filtered results. You can also use the filtered results as a way to train a program on the Before and After values so when applied to some test set it can match similarities to the Before set and try and recommend code similar to what is in the After set. The results can also be put into a database and the ids provided can be used to retrieve information needed that is not provided in the JSON Objects.

Clone this wiki locally