use of com.google.gson.stream.JsonWriter in project iosched by google.
the class CMSUpdateServlet method process.
private void process(HttpServletResponse resp, boolean showOnly) throws IOException {
// everything ok, let's update
StringBuilder summary = new StringBuilder();
JsonObject contents = new JsonObject();
JsonDataSources sources = new VendorDynamicInput().fetchAllDataSources();
for (String entity : sources) {
JsonArray array = new JsonArray();
JsonDataSource source = sources.getSource(entity);
for (JsonObject obj : source) {
array.add(obj);
}
summary.append(entity).append(": ").append(source.size()).append("\n");
contents.add(entity, array);
}
if (showOnly) {
// Show generated contents to the output
resp.setContentType("application/json");
Writer writer = Channels.newWriter(Channels.newChannel(resp.getOutputStream()), "UTF-8");
JsonWriter outputWriter = new JsonWriter(writer);
outputWriter.setIndent(" ");
new Gson().toJson(contents, outputWriter);
outputWriter.flush();
} else {
// Write file to cloud storage
CloudFileManager fileManager = new CloudFileManager();
fileManager.createOrUpdate("__raw_session_data.json", contents, true);
// send email
Message message = new Message();
message.setSender(Config.EMAIL_FROM);
message.setSubject("[iosched-data-update] Manual sync from CMS");
message.setTextBody("Hey,\n\n" + "(this message is autogenerated)\n" + "This is a heads up that " + userService.getCurrentUser().getEmail() + " has just updated the IOSched 2015 data from the Vendor CMS.\n\n" + "Here is a brief status of what has been extracted from the Vendor API:\n" + summary + "\n\n" + "If you want to check the most current data that will soon be sync'ed to the IOSched Android app, " + "check this link: http://storage.googleapis.com/iosched-updater-dev.appspot.com/__raw_session_data.json\n" + "This data will remain unchanged until someone with proper privileges updates it again on https://iosched-updater-dev.appspot.com/cmsupdate\n\n" + "Thanks!\n\n" + "A robot on behalf of the IOSched team!\n\n" + "PS: you are receiving this either because you are an admin of the IOSched project or " + "because you are in a hard-coded list of I/O organizers. If you don't want to " + "receive it anymore, pay me a beer and ask kindly.");
// TODO(arthurthompson): Reimplement mailing, it currently fails due to invalid sender.
//MailServiceFactory.getMailService().sendToAdmins(message);
resp.sendRedirect("/admin/schedule/updateok.html");
}
}
use of com.google.gson.stream.JsonWriter in project Fast-Android-Networking by amitshekhariitbhu.
the class GsonRequestBodyParser 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 useful-java-links by Vedenin.
the class StreamingAPI method writeJson.
/**
* Example to writeJson using StreamingAPI
*/
private static void writeJson() throws IOException {
OutputStream outputStream = new ByteArrayOutputStream();
JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, "UTF-8"));
// main object
writer.beginObject();
writer.name("message");
writer.value("Hi");
// save object Place
writer.name("place");
writer.beginObject();
writer.name("name");
writer.value("World!");
writer.endObject();
writer.endObject();
writer.close();
// print "{"message":"Hi","place":{"name":"World!"}}"
System.out.println(outputStream.toString());
}
use of com.google.gson.stream.JsonWriter in project commons by twitter.
the class TTextProtocol method pushWriter.
private void pushWriter(ByteArrayOutputStream baos) {
OutputStreamWriter osw = new OutputStreamWriter(baos, Charsets.UTF_8);
JsonWriter writer = new JsonWriter(osw);
// two spaces
writer.setIndent(" ");
WriterByteArrayOutputStream wbaos = new WriterByteArrayOutputStream(writer, baos);
writers.push(wbaos);
}
use of com.google.gson.stream.JsonWriter in project mortar by square.
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