Example usage for org.json JSONObject quote

List of usage examples for org.json JSONObject quote

Introduction

In this page you can find the example usage for org.json JSONObject quote.

Prototype

public static String quote(String string) 

Source Link

Document

Produce a string in double quotes with backslash sequences in all the right places.

Usage

From source file:edu.txstate.dmlab.clusteringwiki.rest.ClusterController.java

/**
 * Update paths for a given cluster edit
 * @param qid  Query id of the executed query
 * @param algo  Algorithm used for clustering
 * @param request  HttpServletRequest/*ww  w  .ja  v a  2  s.  co m*/
 * @param response  HttpServletResponse
 * @param model  Model
 * @throws Exception
 */
@RequestMapping("/clusterUpdate/{queryId}/{clusteringAlgo}")
public void savePaths(@PathVariable("queryId") String qid, @PathVariable("clusteringAlgo") String algo,
        HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

    try {

        Integer queryId = Integer.valueOf(_cleanExtensions(qid));
        Integer clusteringAlgo = Integer.valueOf(_cleanExtensions(algo));

        String data = null;
        InputStream is = request.getInputStream();

        if (is != null) {
            try {
                StringBuilder sb = new StringBuilder();
                String line;
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                data = sb.toString();
            } finally {
                is.close();
            }

        }

        if (data == null) {
            sendOutput(response, "{\"error\":\"No data received.\"}");
            return;
        }

        JSONArray paths = new JSONArray(data);

        for (int i = 0; i < paths.length(); i++) {
            JSONObject j = paths.getJSONObject(i);
            Integer cardinality = j.getInt("cardinality");
            JSONArray path = j.getJSONArray("lPath");

            clusterEditDao.updatePath(queryId, clusteringAlgo, path.optString(1).trim(),
                    path.optString(2).trim(), path.optString(3).trim(), path.optString(4).trim(),
                    path.optString(5).trim(), cardinality);
        }

        sendOutput(response, "{\"success\":true}");

    } catch (Exception e) {
        sendOutput(response, "{\"error\":" + JSONObject.quote(e.getMessage()) + "}");
        return;
    }
}

From source file:edu.txstate.dmlab.clusteringwiki.rest.ClusterController.java

/**
 * Delete all edits for a given executed query
 * @param qid/*from   w  w  w . ja  v  a 2 s.  c  o  m*/
 * @param algo
 * @param request
 * @param response
 * @param model
 * @throws Exception
 */
@RequestMapping("/deleteClusterEdits/{queryId}/{clusteringAlgo}")
public void deleteClusterEdits(@PathVariable("queryId") String qid, @PathVariable("clusteringAlgo") String algo,
        HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {

    try {

        Integer queryId = Integer.valueOf(_cleanExtensions(qid));
        Integer clusteringAlgo = Integer.valueOf(_cleanExtensions(algo));

        clusterEditDao.deleteClusterEditsForUserQuery(queryId, clusteringAlgo);

        sendOutput(response, "{\"success\":true}");

    } catch (Exception e) {
        sendOutput(response, "{\"error\":" + JSONObject.quote(e.getMessage()) + "}");
        return;
    }
}

From source file:edu.txstate.dmlab.clusteringwiki.rest.ClusterController.java

/**
 * Retrieve a set of up to 10 most popular (edited by most users) queries with default
 * parameters (google, 50, ffh)/*from w w w .  j a  v a 2  s . c o m*/
 * @param request
 * @param response
 * @param model
 * @throws Exception
 */
@RequestMapping("/getMostPopularQueries")
public void getMostPopularQueries(HttpServletRequest request, HttpServletResponse response, Model model)
        throws Exception {

    try {
        List<String> queries = queryDao.getMostEditedQueries();
        JSONArray qs = new JSONArray(queries);
        JSONObject j = new JSONObject();
        j.put("success", true);
        j.put("queries", qs);
        sendOutput(response, j.toString());

    } catch (Exception e) {
        sendOutput(response, "{\"error\":" + JSONObject.quote(e.getMessage()) + "}");
        return;
    }
}

From source file:nl.b3p.taglibs.js.QuoteTag.java

@Override
public int doEndTag() throws JspException {
    try {/*from w  w  w . jav  a2 s.c om*/
        if (output == null) {
            // JSONObject.quote(null) will return ""
            pageContext.getOut().print("null");
        } else {
            pageContext.getOut().print(JSONObject.quote(output));
        }
    } catch (IOException e) {
        throw new JspTagException(e);
    }
    return EVAL_PAGE;
}

From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java

private static void appendPluginResult(SQLitePLuginResult result, StringBuilder sb) throws JSONException {
    sb.append('[');
    if (result.error == null) {
        sb.append("null");
    } else {//w  ww . j  a v a 2s  . c o  m
        sb.append(JSONObject.quote(result.error.getMessage()));
    }
    sb.append(',').append(JSONObject.numberToString(result.insertId)).append(',')
            .append(JSONObject.numberToString(result.rowsAffected)).append(',');

    // column names
    sb.append('[');
    for (int i = 0; i < result.columns.length; i++) {
        if (i > 0) {
            sb.append(',');
        }
        sb.append(JSONObject.quote(result.columns[i]));
    }
    sb.append("],[");
    // rows
    for (int i = 0; i < result.rows.length; i++) {
        if (i > 0) {
            sb.append(',');
        }
        Object[] values = result.rows[i];
        // row content
        sb.append('[');
        for (int j = 0; j < values.length; j++) {
            if (j > 0) {
                sb.append(',');
            }
            Object value = values[j];
            if (value == null) {
                sb.append("null");
            } else if (value instanceof String) {
                sb.append(JSONObject.quote((String) value));
            } else if (value instanceof Boolean) {
                sb.append(value.toString());
            } else {
                sb.append(JSONObject.numberToString((Number) value));
            }
        }
        sb.append(']');
    }
    sb.append("]]");

    debug("returning json: %s", sb);
}