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