Search in sources :

Example 96 with JSONObject

use of org.json.JSONObject in project OpenRefine by OpenRefine.

the class ColumnSplitOperation method reconstruct.

public static AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
    JSONObject engineConfig = obj.getJSONObject("engineConfig");
    String mode = obj.getString("mode");
    if ("separator".equals(mode)) {
        return new ColumnSplitOperation(engineConfig, obj.getString("columnName"), obj.getBoolean("guessCellType"), obj.getBoolean("removeOriginalColumn"), obj.getString("separator"), obj.getBoolean("regex"), obj.getInt("maxColumns"));
    } else {
        return new ColumnSplitOperation(engineConfig, obj.getString("columnName"), obj.getBoolean("guessCellType"), obj.getBoolean("removeOriginalColumn"), JSONUtilities.getIntArray(obj, "fieldLengths"));
    }
}
Also used : JSONObject(org.json.JSONObject)

Example 97 with JSONObject

use of org.json.JSONObject in project OpenRefine by OpenRefine.

the class StandardReconConfig method batchRecon.

@Override
public List<Recon> batchRecon(List<ReconJob> jobs, long historyEntryID) {
    List<Recon> recons = new ArrayList<Recon>(jobs.size());
    StringWriter stringWriter = new StringWriter();
    stringWriter.write("{");
    for (int i = 0; i < jobs.size(); i++) {
        StandardReconJob job = (StandardReconJob) jobs.get(i);
        if (i > 0) {
            stringWriter.write(",");
        }
        stringWriter.write("\"q" + i + "\":");
        stringWriter.write(job.code);
    }
    stringWriter.write("}");
    String queriesString = stringWriter.toString();
    try {
        URL url = new URL(service);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        {
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            connection.setConnectTimeout(30000);
            connection.setDoOutput(true);
            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
            try {
                String body = "queries=" + ParsingUtilities.encode(queriesString);
                dos.writeBytes(body);
            } finally {
                dos.flush();
                dos.close();
            }
            connection.connect();
        }
        if (connection.getResponseCode() >= 400) {
            InputStream is = connection.getErrorStream();
            logger.error("Failed  - code:" + Integer.toString(connection.getResponseCode()) + " message: " + is == null ? "" : ParsingUtilities.inputStreamToString(is));
        } else {
            InputStream is = connection.getInputStream();
            try {
                String s = ParsingUtilities.inputStreamToString(is);
                JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s);
                for (int i = 0; i < jobs.size(); i++) {
                    StandardReconJob job = (StandardReconJob) jobs.get(i);
                    Recon recon = null;
                    String text = job.text;
                    String key = "q" + i;
                    if (o.has(key)) {
                        JSONObject o2 = o.getJSONObject(key);
                        if (o2.has("result")) {
                            JSONArray results = o2.getJSONArray("result");
                            recon = createReconServiceResults(text, results, historyEntryID);
                        } else {
                            logger.warn("Service error for text: " + text + "\n  Job code: " + job.code + "\n  Response: " + o2.toString());
                        }
                    } else {
                        logger.warn("Service error for text: " + text + "\n  Job code: " + job.code);
                    }
                    if (recon != null) {
                        recon.service = service;
                    }
                    recons.add(recon);
                }
            } finally {
                is.close();
            }
        }
    } catch (Exception e) {
        logger.error("Failed to batch recon with load:\n" + queriesString, e);
    }
    while (recons.size() < jobs.size()) {
        Recon recon = new Recon(historyEntryID, identifierSpace, schemaSpace);
        recon.service = service;
        recon.identifierSpace = identifierSpace;
        recon.schemaSpace = schemaSpace;
        recons.add(recon);
    }
    return recons;
}
Also used : DataOutputStream(java.io.DataOutputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) URL(java.net.URL) JSONException(org.json.JSONException) HttpURLConnection(java.net.HttpURLConnection) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) Recon(com.google.refine.model.Recon)

Example 98 with JSONObject

use of org.json.JSONObject in project OpenRefine by OpenRefine.

the class ReconJudgeSimilarCellsOperation method reconstruct.

public static AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
    JSONObject engineConfig = obj.getJSONObject("engineConfig");
    ReconCandidate match = null;
    if (obj.has("match")) {
        JSONObject matchObj = obj.getJSONObject("match");
        JSONArray types = matchObj.getJSONArray("types");
        String[] typeIDs = new String[types.length()];
        for (int i = 0; i < typeIDs.length; i++) {
            typeIDs[i] = types.getString(i);
        }
        match = new ReconCandidate(matchObj.getString("id"), matchObj.getString("name"), typeIDs, matchObj.getDouble("score"));
    }
    Judgment judgment = Judgment.None;
    if (obj.has("judgment")) {
        judgment = Recon.stringToJudgment(obj.getString("judgment"));
    }
    return new ReconJudgeSimilarCellsOperation(engineConfig, obj.getString("columnName"), obj.getString("similarValue"), judgment, match, obj.has("shareNewTopics") ? obj.getBoolean("shareNewTopics") : false);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ReconCandidate(com.google.refine.model.ReconCandidate) Judgment(com.google.refine.model.Recon.Judgment)

Example 99 with JSONObject

use of org.json.JSONObject in project OpenRefine by OpenRefine.

the class ReconMatchBestCandidatesOperation method reconstruct.

public static AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
    JSONObject engineConfig = obj.getJSONObject("engineConfig");
    String columnName = obj.getString("columnName");
    return new ReconMatchBestCandidatesOperation(engineConfig, columnName);
}
Also used : JSONObject(org.json.JSONObject)

Example 100 with JSONObject

use of org.json.JSONObject in project OpenRefine by OpenRefine.

the class CommandTests method getJsonParameterRegressionTest.

@Test
public void getJsonParameterRegressionTest() {
    when(request.getParameter("test")).thenReturn("{\"foo\":\"bar\"}");
    JSONObject o = SUT.wrapGetJsonParameter(request, "test");
    Assert.assertNotNull(o);
    try {
        Assert.assertEquals("bar", o.getString("foo"));
    } catch (JSONException e) {
        Assert.fail();
    }
    verify(request, times(1)).getParameter("test");
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Aggregations

JSONObject (org.json.JSONObject)3855 JSONException (org.json.JSONException)1520 JSONArray (org.json.JSONArray)1084 Test (org.junit.Test)494 IOException (java.io.IOException)425 ArrayList (java.util.ArrayList)375 HashMap (java.util.HashMap)282 Test (org.testng.annotations.Test)173 File (java.io.File)137 Date (java.util.Date)137 ServiceException (org.b3log.latke.service.ServiceException)125 VolleyError (com.android.volley.VolleyError)103 Bundle (android.os.Bundle)102 Map (java.util.Map)95 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)92 InputStreamReader (java.io.InputStreamReader)84 Response (com.android.volley.Response)83 BufferedReader (java.io.BufferedReader)83 List (java.util.List)76 InputStream (java.io.InputStream)70