use of com.google.gson.stream.JsonWriter in project che by eclipse.
the class DtoImpl method quoteStringLiteral.
/**
* Create a textual representation of a string literal that evaluates to the given value.
*/
protected String quoteStringLiteral(String value) {
StringWriter sw = new StringWriter();
try (JsonWriter writer = new JsonWriter(sw)) {
writer.setLenient(true);
writer.value(value);
writer.flush();
} catch (IOException ex) {
throw new RuntimeException("Unexpected I/O failure: " + ex.getLocalizedMessage(), ex);
}
return sw.toString();
}
use of com.google.gson.stream.JsonWriter in project elastic-job by dangdangdotcom.
the class GsonFactoryTest method assertRegisterTypeAdapter.
@Test
public void assertRegisterTypeAdapter() {
Gson beforeRegisterGson = GsonFactory.getGson();
GsonFactory.registerTypeAdapter(GsonFactoryTest.class, new TypeAdapter() {
@Override
public Object read(final JsonReader in) throws IOException {
return null;
}
@Override
public void write(final JsonWriter out, final Object value) throws IOException {
out.jsonValue("test");
}
});
assertThat(beforeRegisterGson.toJson(new GsonFactoryTest()), is("{}"));
assertThat(GsonFactory.getGson().toJson(new GsonFactoryTest()), is("test"));
}
use of com.google.gson.stream.JsonWriter in project buck by facebook.
the class WorkerProcess method ensureLaunchAndHandshake.
public synchronized void ensureLaunchAndHandshake() throws IOException {
if (handshakePerformed) {
return;
}
LOG.debug("Starting up process %d using command: \'%s\'", this.hashCode(), Joiner.on(' ').join(processParams.getCommand()));
launchedProcess = executor.launchProcess(processParams);
JsonWriter processStdinWriter = new JsonWriter(new BufferedWriter(new OutputStreamWriter(launchedProcess.getOutputStream())));
JsonReader processStdoutReader = new JsonReader(new BufferedReader(new InputStreamReader(launchedProcess.getInputStream())));
protocol = new WorkerProcessProtocolZero(executor, launchedProcess, processStdinWriter, processStdoutReader, stdErr);
int messageID = currentMessageID.getAndAdd(1);
LOG.debug("Sending handshake to process %d", this.hashCode());
protocol.sendHandshake(messageID);
LOG.debug("Receiving handshake from process %d", this.hashCode());
protocol.receiveHandshake(messageID);
handshakePerformed = true;
}
use of com.google.gson.stream.JsonWriter in project buck by facebook.
the class WorkerProcessProtocolZeroTest method testSendCommandResponse.
@Test
public void testSendCommandResponse() throws IOException {
StringWriter jsonSentToWorkerProcess = new StringWriter();
WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, new JsonWriter(jsonSentToWorkerProcess), dummyJsonReader, newTempFile());
int messageID = 123;
protocol.sendCommandResponse(messageID, "result", 0);
String expectedJson = String.format("{\"id\":%d,\"type\":\"result\",\"exit_code\":0}", messageID);
assertThat(jsonSentToWorkerProcess.toString(), Matchers.containsString(expectedJson));
}
use of com.google.gson.stream.JsonWriter in project buck by facebook.
the class WorkerProcessProtocolZeroTest method testProcessIsStillDestroyedEvenIfErrorOccursWhileClosingStreams.
@Test
public void testProcessIsStillDestroyedEvenIfErrorOccursWhileClosingStreams() throws IOException {
JsonWriter writer = new JsonWriter(new StringWriter());
// write an opening bracket now, so the writer doesn't throw due to invalid JSON when it goes
// to write the closing bracket
writer.beginArray();
JsonReader reader = new JsonReader(new StringReader("invalid JSON"));
WorkerProcessProtocol protocol = new WorkerProcessProtocolZero(fakeProcessExecutor, fakeLaunchedProcess, writer, reader, newTempFile());
try {
protocol.close();
} catch (IOException e) {
assertThat(e.getMessage(), Matchers.containsString("malformed JSON"));
// assert that process was still destroyed despite the exception
assertTrue(fakeProcess.isDestroyed());
}
}
Aggregations