Example usage for org.json.simple JSONObject toJSONString

List of usage examples for org.json.simple JSONObject toJSONString

Introduction

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

Prototype

public String toJSONString() 

Source Link

Usage

From source file:com.conwet.silbops.connectors.comet.JSONUtils.java

@SuppressWarnings("unchecked")
public static String toJSONReply(String command, JSONObject parameters) {

    JSONObject json = new JSONObject();
    json.put("command", command);
    json.put("parameters", parameters);

    return json.toJSONString();
}

From source file:mml.handler.json.Dialect.java

/**
 * Wrap a dialect doc in some more JSON for storage
 * @param dialect the dialect file/*w ww  . j a  v a 2  s  .c o  m*/
 * @param docid its docid
 * @return a BSON basic document
 */
public static JSONObject wrap(JSONObject dialect, String docid) {
    JSONObject wrap = new JSONObject();
    wrap.put(JSONKeys.BODY, dialect.toJSONString());
    wrap.put(JSONKeys.DOCID, docid);
    return wrap;
}

From source file:edu.sjsu.cohort6.esp.common.CommonUtils.java

/**
 * This method parses the generic jersey response to a generic JSONObject and singles out the "entity" piece
 * from the generic response./*from  www.j a va2 s.c  o m*/
 *
 * @param response
 * @return
 * @throws org.json.simple.parser.ParseException
 */
private static String getJsonFromResponse(Response response) throws org.json.simple.parser.ParseException {
    String c = response.readEntity(String.class);
    log.info(c.toString());
    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(c);
    JSONObject jsonObject = (JSONObject) json.get("entity");
    return jsonObject.toJSONString();
}

From source file:co.edu.unal.arqdsoft.presentacion.JSON.java

/**
 * //from   w  w w .j ava  2  s.  co m
 * @param arg
 * @return JSON String del ojbeto parametro
 */
public static String toString(Respuesta arg) {
    JSONObject obj = new JSONObject();
    obj.put("error", arg.getError());
    obj.put("contenido", arg.getContenido().toJSON());
    return obj.toJSONString();
}

From source file:io.fabric8.jolokia.support.JolokiaHelpers.java

public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException {
    if (clazz.isArray()) {
        if (value instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) value;
            Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size());
            int idx = 0;
            for (Object element : jsonArray) {
                Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element));
            }//from   ww w . jav  a 2 s  .  c  o m
            return javaArray;
        } else {
            return null;
        }
    } else if (String.class.equals(clazz)) {
        return (value != null) ? value.toString() : null;
    } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
        Number number = asNumber(value);
        return number != null ? number.byteValue() : null;
    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
        Number number = asNumber(value);
        return number != null ? number.shortValue() : null;
    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
        Number number = asNumber(value);
        return number != null ? number.intValue() : null;
    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
        Number number = asNumber(value);
        return number != null ? number.longValue() : null;
    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
        Number number = asNumber(value);
        return number != null ? number.floatValue() : null;
    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
        Number number = asNumber(value);
        return number != null ? number.doubleValue() : null;
    } else if (value instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) value;
        if (!JSONObject.class.isAssignableFrom(clazz)) {
            String json = jsonObject.toJSONString();
            return getObjectMapper().readerFor(clazz).readValue(json);
        }
    }
    return value;
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.driver.couchdb.CouchDBUtil.java

/**
 * Add id and revision to the content of a given request
 *
 * @param request the given request//from www  . j a  v  a 2s  .  c o  m
 * @param id the id to add
 * @param revision the revision to add
 * @return the modified request
 */
public static Request addIdAndRevisionToContent(Request request, String id, String revision) {
    JSONParser parser = new JSONParser();
    try {
        JSONObject jsonObject = (JSONObject) parser.parse((String) request.getContent());
        jsonObject.put("_id", id);
        jsonObject.put("_rev", revision);
        String jsonString = jsonObject.toJSONString();
        log.info("Updated Request content with id and rev: " + jsonString);
        return new Request.Builder(request.getProtocolType(), request.getRequestType(), request.getUrl(),
                request.getHost(), request.getPort()).contentType(request.getContentType()).content(jsonString)
                        .build();
    } catch (ParseException e) {
        throw new DatastoreException("Error parsing JSON to add revision: " + e.getMessage());
    }
}

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a sample method that saves a handful of car instances as Serialized
 * objects, and as JSON objects./*from  w w w  .j  ava2  s .  c  om*/
 */
public static void doBuildIt2Output() {
    Car[] cars = { new Car("Honda", "Civic", 2001), new Car("Buick", "LeSabre", 2003),
            new Car("Toyota", "Camry", 2005) };

    // Saves as Serialized Objects
    try {
        FileOutputStream objFile = new FileOutputStream("cars.obj");
        ObjectOutputStream objStream = new ObjectOutputStream(objFile);

        for (Car car : cars) {
            objStream.writeObject(car);
        }

        objStream.close();
        objFile.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Saves as JSONObject
    try {
        PrintWriter jsonStream = new PrintWriter("cars.json");

        JSONArray arr = new JSONArray();
        for (Car car : cars) {
            arr.add(car.toJSON());
        }

        JSONObject json = new JSONObject();
        json.put("cars", arr);

        jsonStream.println(json.toJSONString());

        jsonStream.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.p000ison.dev.simpleclans2.util.JSONUtil.java

public static String mapToJSON(Map map) {
    JSONObject JSONMap = new JSONObject();

    JSONMap.putAll(map);//from  www . j av a2  s . com

    return JSONMap.toJSONString();
}

From source file:msuresh.raftdistdb.RaftCluster.java

public static void createDefaultGlobal() throws IOException {
    JSONObject obj = new JSONObject();
    obj.put("currentCount", 5000);
    try (FileWriter file = new FileWriter(Constants.STATE_LOCATION + "global.info")) {
        file.write(obj.toJSONString());
    }//  w w w. j  a  va 2s .  co  m
}

From source file:com.stratio.es.ESJavaRDDFT.java

/**
 * Imports dataset//from  w  ww  .  ja  va 2  s .c  om
 *
 * @throws java.io.IOException
 */
private static void dataSetImport()
        throws IOException, ExecutionException, IOException, InterruptedException, ParseException {

    JSONParser parser = new JSONParser();
    URL url = Resources.getResource(DATA_SET_NAME);
    Object obj = parser.parse(new FileReader(url.getFile()));

    JSONObject jsonObject = (JSONObject) obj;

    IndexResponse responseBook = client.prepareIndex(ES_INDEX_BOOK, ES_TYPE_INPUT, "id")
            .setSource(jsonObject.toJSONString()).execute().actionGet();

    String json2 = "{" +

            "\"message\":\"" + MESSAGE_TEST + "\"" + "}";

    IndexResponse response2 = client.prepareIndex(ES_INDEX_MESSAGE, ES_TYPE_MESSAGE).setCreate(true)
            .setSource(json2).setReplicationType(ReplicationType.ASYNC).execute().actionGet();

    String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\","
            + "\"message\":\"trying out Elasticsearch\"" + "}";

    IndexResponse response = client.prepareIndex(ES_INDEX, ES_TYPE).setCreate(true).setSource(json).execute()
            .actionGet();

    String index = response.getIndex();
    String _type = response.getType();
    String _id = response.getId();
    try {
        CountResponse countResponse = client.prepareCount(ES_INDEX).setTypes(ES_TYPE).execute().actionGet();

        SearchResponse searchResponse = client.prepareSearch(ES_INDEX_BOOK).setTypes(ES_TYPE_INPUT).execute()
                .actionGet();

        //searchResponse.getHits().hits();
        //assertEquals(searchResponse.getCount(), 1);
    } catch (AssertionError | Exception e) {
        cleanup();
        e.printStackTrace();
    }
}