Search in sources :

Example 26 with JsonWriter

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");
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonDataSource(com.google.samples.apps.iosched.server.schedule.model.JsonDataSource) CloudFileManager(com.google.samples.apps.iosched.server.schedule.server.cloudstorage.CloudFileManager) Message(com.google.appengine.api.mail.MailService.Message) JsonDataSources(com.google.samples.apps.iosched.server.schedule.model.JsonDataSources) 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 27 with JsonWriter

use of com.google.gson.stream.JsonWriter in project Minechem by iopleke.

the class ResearchHandler method saveResearch.

/**
     * Save the current research map
     */
public static void saveResearch() {
    try {
        OutputStream outputStream = FileUtils.openOutputStream(new File(Minechem.proxy.getCurrentSaveDir() + "/data/" + Compendium.Config.playerResearchData));
        JsonWriter jWriter = new JsonWriter(new OutputStreamWriter(outputStream));
        jWriter.setIndent("    ");
        jWriter.beginObject();
        for (Map.Entry<UUID, Set<String>> entry : ResearchRegistry.getInstance().getPlayerResearchMap().entrySet()) {
            jWriter.name(entry.getKey().toString()).beginObject();
            // @TODO: maybe find a way to add this for readability
            //jWriter.name("displayName").value();
            jWriter.name("research").beginArray();
            for (String research : entry.getValue()) {
                jWriter.value(research);
            }
            jWriter.endArray();
            jWriter.endObject();
        }
        jWriter.endObject();
        jWriter.close();
        try {
            outputStream.close();
        } catch (IOException e) {
            LogHelper.exception("Cannot close stream!", e, Level.WARN);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Set(java.util.Set) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) UUID(java.util.UUID) File(java.io.File) JsonWriter(com.google.gson.stream.JsonWriter) Map(java.util.Map)

Example 28 with JsonWriter

use of com.google.gson.stream.JsonWriter in project hadoop by apache.

the class RLESparseResourceAllocation method toMemJSONString.

/**
   * Returns the JSON string representation of the current resources allocated
   * over time.
   *
   * @return the JSON string representation of the current resources allocated
   *         over time
   */
public String toMemJSONString() {
    StringWriter json = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(json);
    readLock.lock();
    try {
        jsonWriter.beginObject();
        // jsonWriter.name("timestamp").value("resource");
        for (Map.Entry<Long, Resource> r : cumulativeCapacity.entrySet()) {
            jsonWriter.name(r.getKey().toString()).value(r.getValue().toString());
        }
        jsonWriter.endObject();
        jsonWriter.close();
        return json.toString();
    } catch (IOException e) {
        // This should not happen
        return "";
    } finally {
        readLock.unlock();
    }
}
Also used : StringWriter(java.io.StringWriter) Resource(org.apache.hadoop.yarn.api.records.Resource) IOException(java.io.IOException) JsonWriter(com.google.gson.stream.JsonWriter) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 29 with JsonWriter

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());
}
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 30 with JsonWriter

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());
}
Also used : 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