Example usage for org.json.simple JSONStreamAware writeJSONString

List of usage examples for org.json.simple JSONStreamAware writeJSONString

Introduction

In this page you can find the example usage for org.json.simple JSONStreamAware writeJSONString.

Prototype

void writeJSONString(Writer out) throws IOException;

Source Link

Usage

From source file:com.cloudera.lib.wsrs.JSONProvider.java

@Override
public void writeTo(JSONStreamAware jsonStreamAware, Class<?> aClass, Type type, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap,
        OutputStream outputStream) throws IOException, WebApplicationException {
    Writer writer = new OutputStreamWriter(outputStream);
    jsonStreamAware.writeJSONString(writer);
    writer.write(ENTER);/*from w  w  w.jav  a 2 s  .  c o  m*/
    writer.flush();
}

From source file:org.apache.hadoop.lib.wsrs.JSONProvider.java

@Override
public void writeTo(JSONStreamAware jsonStreamAware, Class<?> aClass, Type type, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap,
        OutputStream outputStream) throws IOException, WebApplicationException {
    Writer writer = new OutputStreamWriter(outputStream, Charsets.UTF_8);
    jsonStreamAware.writeJSONString(writer);
    writer.write(ENTER);//from   w  w  w .j av  a  2s  . com
    writer.flush();
}

From source file:org.chromium.sdk.internal.JsonUtil.java

/**
 * Converts a JSONStreamAware into a String.
 *
 * @param object the object to convert//from   w  w  w .  j  ava  2  s.c o  m
 * @return a JSON String representation of the object
 */
public static String streamAwareToJson(JSONStreamAware object) {
    StringWriter out = new StringWriter();
    try {
        object.writeJSONString(out);
    } catch (IOException e) {
        return null;
    }
    return out.toString();
}

From source file:org.csml.tommo.sugar.analysis.JSONSerializationUtils.java

public static void writeJSONFile(File file, JSONStreamAware jsonObject) throws IOException {
    Writer writer = null;//from   w  ww.ja v a  2  s  .co m
    try {
        writer = new BufferedWriter(new FileWriter(file));
        jsonObject.writeJSONString(writer);
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:org.jolokia.jvmagent.handler.JolokiaHttpHandler.java

private void sendStreamingResponse(HttpExchange pExchange, ParsedUri pParsedUri, JSONStreamAware pJson)
        throws IOException {
    ChunkedWriter writer = null;/*from  w w w  .  j  a  va 2 s. c o  m*/
    try {
        Headers headers = pExchange.getResponseHeaders();
        if (pJson != null) {
            headers.set("Content-Type", getMimeType(pParsedUri) + "; charset=utf-8");
            String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue());
            pExchange.sendResponseHeaders(200, 0);
            writer = new ChunkedWriter(pExchange.getResponseBody(), "UTF-8");
            if (callback == null) {
                pJson.writeJSONString(writer);
            } else {
                writer.write("(");
                writer.write(callback);
                pJson.writeJSONString(writer);
                writer.write(");");
            }
        } else {
            headers.set("Content-Type", "text/plain");
            pExchange.sendResponseHeaders(200, -1);
        }
    } finally {
        if (writer != null) {
            // Always close in order to finish the request.
            // Otherwise the thread blocks.
            writer.flush();
            writer.close();
        }
    }
}