Search in sources :

Example 1 with JsonWriter

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();
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter)

Example 2 with JsonWriter

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"));
}
Also used : TypeAdapter(com.google.gson.TypeAdapter) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Example 3 with JsonWriter

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JsonReader(com.google.gson.stream.JsonReader) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(com.google.gson.stream.JsonWriter) BufferedWriter(java.io.BufferedWriter)

Example 4 with JsonWriter

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));
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Example 5 with JsonWriter

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());
    }
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Aggregations

JsonWriter (com.google.gson.stream.JsonWriter)38 StringWriter (java.io.StringWriter)16 OutputStreamWriter (java.io.OutputStreamWriter)11 Gson (com.google.gson.Gson)9 IOException (java.io.IOException)9 Test (org.junit.Test)9 JsonReader (com.google.gson.stream.JsonReader)8 Writer (java.io.Writer)7 StringReader (java.io.StringReader)5 SuppressLint (android.annotation.SuppressLint)3 JsonObject (com.google.gson.JsonObject)3 JsonDataSources (com.google.samples.apps.iosched.server.schedule.model.JsonDataSources)3 Map (java.util.Map)3 JsonElement (com.google.gson.JsonElement)2 TypeAdapter (com.google.gson.TypeAdapter)2 DataExtractor (com.google.samples.apps.iosched.server.schedule.model.DataExtractor)2 CloudFileManager (com.google.samples.apps.iosched.server.schedule.server.cloudstorage.CloudFileManager)2 ExtraInput (com.google.samples.apps.iosched.server.schedule.server.input.ExtraInput)2 VendorDynamicInput (com.google.samples.apps.iosched.server.schedule.server.input.VendorDynamicInput)2 BufferedWriter (java.io.BufferedWriter)2