Search in sources :

Example 11 with JsonWriter

use of com.google.gson.stream.JsonWriter in project immutables by immutables.

the class JaxrsTest method propagateGsonAttributes.

@Test
public void propagateGsonAttributes() {
    Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().setPrettyPrinting().create();
    GsonOptions options = new GsonOptions(gson, true);
    JsonReader reader = new JsonReader(new StringReader(""));
    options.setReaderOptions(reader);
    check(reader.isLenient());
    JsonWriter writer = new JsonWriter(new StringWriter());
    options.setWriterOptions(writer);
    check(writer.isLenient());
    check(!writer.isHtmlSafe());
    check(writer.getSerializeNulls());
    // checks pretty printing
    check(gson.toJson(Collections.singletonMap("k", "v"))).is("{\n  \"k\": \"v\"\n}");
}
Also used : GsonOptions(org.immutables.gson.stream.GsonMessageBodyProvider.GsonOptions) StringWriter(java.io.StringWriter) GsonBuilder(com.google.gson.GsonBuilder) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Example 12 with JsonWriter

use of com.google.gson.stream.JsonWriter in project pinpoint by naver.

the class GsonIT method testFromV1_6.

@Test
public void testFromV1_6() throws Exception {
    if (!v1_6) {
        return;
    }
    final Gson gson = new Gson();
    final JsonElement jsonElement = getParseElements();
    /**
         * @see Gson#fromJson(JsonReader, InterceptPoint)
         */
    gson.fromJson(new JsonReader(new StringReader(json)), String.class);
    Method fromJson5 = Gson.class.getDeclaredMethod("fromJson", JsonReader.class, Type.class);
    /**
         * @see Gson#toJson(Object, InterceptPoint, JsonWriter)
         * @see Gson#toJson(JsonElement, JsonWriter)
         */
    gson.toJson(java, String.class, new JsonWriter(new StringWriter()));
    gson.toJson(jsonElement, new JsonWriter(new StringWriter()));
    Method toJson5 = Gson.class.getDeclaredMethod("toJson", Object.class, Type.class, JsonWriter.class);
    Method toJson8 = Gson.class.getDeclaredMethod("toJson", JsonElement.class, JsonWriter.class);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(event(serviceType, fromJson5));
    verifier.verifyTrace(event(serviceType, toJson5), event(serviceType, toJson8));
    // No more traces
    verifier.verifyTraceCount(0);
}
Also used : StringWriter(java.io.StringWriter) JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) Method(java.lang.reflect.Method) JsonWriter(com.google.gson.stream.JsonWriter) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 13 with JsonWriter

use of com.google.gson.stream.JsonWriter in project retrofit by square.

the class GsonRequestBodyConverter method convert.

@Override
public RequestBody convert(T value) throws IOException {
    Buffer buffer = new Buffer();
    Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
    JsonWriter jsonWriter = gson.newJsonWriter(writer);
    adapter.write(jsonWriter, value);
    jsonWriter.close();
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
Also used : Buffer(okio.Buffer) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(com.google.gson.stream.JsonWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(com.google.gson.stream.JsonWriter)

Example 14 with JsonWriter

use of com.google.gson.stream.JsonWriter in project iosched by google.

the class APIExtractor method run.

public void run(OutputStream optionalOutput, boolean extractUnpublished) throws IOException {
    // fill sources with extra input:
    JsonDataSources sources = new ExtraInput().fetchAllDataSources();
    // fill sources with vendor API input:
    VendorDynamicInput vendorInput = new VendorDynamicInput();
    vendorInput.setExtractUnpublished(extractUnpublished);
    sources.putAll(vendorInput.fetchAllDataSources());
    // extract session data from inputs:
    JsonObject newData = new DataExtractor(false).extractFromDataSources(sources);
    // send data to the outputstream
    Writer writer = Channels.newWriter(Channels.newChannel(optionalOutput), "UTF-8");
    JsonWriter optionalOutputWriter = new JsonWriter(writer);
    optionalOutputWriter.setIndent("  ");
    new Gson().toJson(newData, optionalOutputWriter);
    optionalOutputWriter.flush();
}
Also used : JsonDataSources(com.google.samples.apps.iosched.server.schedule.model.JsonDataSources) ExtraInput(com.google.samples.apps.iosched.server.schedule.server.input.ExtraInput) DataExtractor(com.google.samples.apps.iosched.server.schedule.model.DataExtractor) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) VendorDynamicInput(com.google.samples.apps.iosched.server.schedule.server.input.VendorDynamicInput) JsonWriter(com.google.gson.stream.JsonWriter) Writer(java.io.Writer) JsonWriter(com.google.gson.stream.JsonWriter)

Example 15 with JsonWriter

use of com.google.gson.stream.JsonWriter in project simple-stack by Zhuinden.

the class GsonParceler method encode.

private String encode(Object instance) throws IOException {
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    try {
        Class<?> type = instance.getClass();
        writer.beginObject();
        writer.name(type.getName());
        gson.toJson(instance, type, writer);
        writer.endObject();
        return stringWriter.toString();
    } finally {
        writer.close();
    }
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(com.google.gson.stream.JsonWriter)

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