Search in sources :

Example 36 with JsonWriter

use of com.google.gson.stream.JsonWriter in project hadoop by apache.

the class RLESparseResourceAllocation method toMemJSONString.

/**
   * Returns the JSON string representation of the current resources allocated
   * over time.
   *
   * @return the JSON string representation of the current resources allocated
   *         over time
   */
public String toMemJSONString() {
    StringWriter json = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(json);
    readLock.lock();
    try {
        jsonWriter.beginObject();
        // jsonWriter.name("timestamp").value("resource");
        for (Map.Entry<Long, Resource> r : cumulativeCapacity.entrySet()) {
            jsonWriter.name(r.getKey().toString()).value(r.getValue().toString());
        }
        jsonWriter.endObject();
        jsonWriter.close();
        return json.toString();
    } catch (IOException e) {
        // This should not happen
        return "";
    } finally {
        readLock.unlock();
    }
}
Also used : StringWriter(java.io.StringWriter) Resource(org.apache.hadoop.yarn.api.records.Resource) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 37 with JsonWriter

use of com.google.gson.stream.JsonWriter in project intellij-community by JetBrains.

the class RestService method createJsonWriter.

@NotNull
protected static JsonWriter createJsonWriter(@NotNull OutputStream out) {
    JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, CharsetToolkit.UTF8_CHARSET));
    writer.setIndent("  ");
    return writer;
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(com.google.gson.stream.JsonWriter) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with JsonWriter

use of com.google.gson.stream.JsonWriter in project gerrit by GerritCodeReview.

the class RestApiServlet method stackJsonString.

private static BinaryResult stackJsonString(HttpServletResponse res, final BinaryResult src) throws IOException {
    TemporaryBuffer.Heap buf = heap(HEAP_EST_SIZE, Integer.MAX_VALUE);
    buf.write(JSON_MAGIC);
    try (Writer w = new BufferedWriter(new OutputStreamWriter(buf, UTF_8));
        JsonWriter json = new JsonWriter(w)) {
        json.setLenient(true);
        json.setHtmlSafe(true);
        json.value(src.asString());
        w.write('\n');
    }
    res.setHeader("X-FYI-Content-Encoding", "json");
    res.setHeader("X-FYI-Content-Type", src.getContentType());
    return asBinaryResult(buf).setContentType(JSON_TYPE).setCharacterEncoding(UTF_8);
}
Also used : Heap(org.eclipse.jgit.util.TemporaryBuffer.Heap) TemporaryBuffer(org.eclipse.jgit.util.TemporaryBuffer) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(com.google.gson.stream.JsonWriter) JsonWriter(com.google.gson.stream.JsonWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 39 with JsonWriter

use of com.google.gson.stream.JsonWriter in project intellij-community by JetBrains.

the class IpnbParser method newDocumentText.

@Nullable
public static String newDocumentText(@NotNull final IpnbFilePanel ipnbPanel) {
    final IpnbFile ipnbFile = ipnbPanel.getIpnbFile();
    if (ipnbFile == null)
        return null;
    for (IpnbEditablePanel panel : ipnbPanel.getIpnbPanels()) {
        if (panel.isModified()) {
            panel.updateCellSource();
        }
    }
    final IpnbFileRaw fileRaw = new IpnbFileRaw();
    fileRaw.metadata = ipnbFile.getMetadata();
    if (ipnbFile.getNbformat() == 4) {
        for (IpnbCell cell : ipnbFile.getCells()) {
            fileRaw.cells.add(IpnbCellRaw.fromCell(cell, ipnbFile.getNbformat()));
        }
    } else {
        final IpnbWorksheet worksheet = new IpnbWorksheet();
        worksheet.cells.clear();
        for (IpnbCell cell : ipnbFile.getCells()) {
            worksheet.cells.add(IpnbCellRaw.fromCell(cell, ipnbFile.getNbformat()));
        }
        fileRaw.worksheets = new IpnbWorksheet[] { worksheet };
    }
    final StringWriter stringWriter = new StringWriter();
    final JsonWriter writer = new JsonWriter(stringWriter);
    writer.setIndent(" ");
    gson.toJson(fileRaw, fileRaw.getClass(), writer);
    return stringWriter.toString();
}
Also used : JsonWriter(com.google.gson.stream.JsonWriter) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with JsonWriter

use of com.google.gson.stream.JsonWriter in project incubator-atlas by apache.

the class JsonSerializer method serialize.

public String serialize(Result result, UriInfo ui) {
    Writer json = new StringWriter();
    JsonWriter writer = new JsonWriter(json);
    writer.setIndent("    ");
    try {
        writeValue(writer, result.getPropertyMaps(), ui.getBaseUri().toASCIIString());
    } catch (IOException e) {
        throw new CatalogRuntimeException("Unable to write JSON response.", e);
    }
    return json.toString();
}
Also used : StringWriter(java.io.StringWriter) CatalogRuntimeException(org.apache.atlas.catalog.exception.CatalogRuntimeException) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) JsonWriter(com.google.gson.stream.JsonWriter)

Aggregations

JsonWriter (com.google.gson.stream.JsonWriter)46 StringWriter (java.io.StringWriter)21 Test (org.junit.Test)14 OutputStreamWriter (java.io.OutputStreamWriter)11 Gson (com.google.gson.Gson)10 JsonReader (com.google.gson.stream.JsonReader)10 IOException (java.io.IOException)10 Writer (java.io.Writer)7 StringReader (java.io.StringReader)5 Map (java.util.Map)5 NullOperationMonitor (org.eclipse.linuxtools.internal.mylyn.osio.rest.core.NullOperationMonitor)5 OSIORestClient (org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestClient)5 OSIORestConfiguration (org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestConfiguration)5 TestData (org.eclipse.linuxtools.mylyn.osio.rest.test.support.TestData)5 RepositoryLocation (org.eclipse.mylyn.commons.repositories.core.RepositoryLocation)5 AbstractTaskDataHandler (org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler)5 TaskAttributeMapper (org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper)5 TaskData (org.eclipse.mylyn.tasks.core.data.TaskData)5 JsonObject (com.google.gson.JsonObject)4 SuppressLint (android.annotation.SuppressLint)3