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();
}
}
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;
}
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);
}
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();
}
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();
}
Aggregations