Package com.google.gson.stream

Examples of com.google.gson.stream.JsonWriter


      if (!Strings.isNullOrEmpty(jsonpMethod)) {
        writer.write(jsonpMethod);
        writer.write("(");
      }

      jsonWriter = new JsonWriter(writer);
      jsonWriter.setHtmlSafe(false);
      jsonWriter.setIndent("  ");

      jsonWriter.beginObject(); // Main holder.
View Full Code Here


      do {
        event = events.peekFirst();

        try {
          JsonWriter jsonWriter = new JsonWriter(writer);
          jsonWriter.setIndent("  ");
          jsonWriter.beginArray();
          jsonWriter.value(event.getType().name());
          gson.toJson(event, event.getClass(), jsonWriter);
          jsonWriter.endArray();
        } catch (Throwable t) {
          Closeables.close(writer, false);
          writer = null;

          SlaveMain.warn("Unhandled exception in event serialization.", t);
View Full Code Here

      if (!Strings.isNullOrEmpty(jsonpMethod)) {
        writer.write(jsonpMethod);
        writer.write("(");
      }

      jsonWriter = new JsonWriter(writer);
      jsonWriter.setHtmlSafe(false);
      jsonWriter.setIndent("  ");

      jsonWriter.beginObject(); // Main holder.
View Full Code Here

      if (!Strings.isNullOrEmpty(jsonpMethod)) {
        writer.write(jsonpMethod);
        writer.write("(");
      }

      jsonWriter = new JsonWriter(writer);
      jsonWriter.setHtmlSafe(false);
      jsonWriter.setIndent("  ");

      jsonWriter.beginObject(); // Main holder.
View Full Code Here

        handleFailure(httpResponse);
        return;
      }

      ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
      JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(new ChannelBufferOutputStream(channelBuffer),
                                                                    Charsets.UTF_8));
      new Gson().toJson(object, object.getClass(), jsonWriter);
      jsonWriter.close();

      httpResponse.setContent(channelBuffer);
      httpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json");
      httpResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, channelBuffer.readableBytes());
      ChannelFuture result = channel.write(httpResponse);
View Full Code Here

  private String buildString() {
    if (type.isSimpleType()) {
      return '"' + type.name().toLowerCase() + '"';
    }
    StringBuilder builder = new StringBuilder();
    JsonWriter writer = new JsonWriter(CharStreams.asWriter(builder));
    try {
      new SchemaTypeAdapter().write(writer, this);
      writer.close();
      return builder.toString();
    } catch (IOException e) {
      // It should never throw IOException on the StringBuilder Writer, if it does, something very wrong.
      throw Throwables.propagate(e);
    }
View Full Code Here

   */
  @Override
  public void sendJson(HttpResponseStatus status, Object object, Type type, Gson gson) {
    try {
      ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
      JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(new ChannelBufferOutputStream(channelBuffer),
                                                                    Charsets.UTF_8));
      try {
        gson.toJson(object, type, jsonWriter);
      } finally {
        jsonWriter.close();
      }

      sendContent(status, channelBuffer, "application/json", ImmutableMultimap.<String, String>of());
    } catch (IOException e) {
      throw Throwables.propagate(e);
View Full Code Here

      do {
        event = events.peekFirst();

        try {
          JsonWriter jsonWriter = new JsonWriter(writer);
          jsonWriter.setIndent("  ");
          jsonWriter.beginArray();
          jsonWriter.value(event.getType().name());
          gson.toJson(event, event.getClass(), jsonWriter);
          jsonWriter.endArray();
          jsonWriter.flush();
        } catch (Throwable t) {
          SlaveMain.warn("Unhandled exception in event serialization.", t);

          Closeables.closeQuietly(writer);
          writer = null;
View Full Code Here

   */
  @Override
  public String toString() {
    try {
      StringWriter stringWriter = new StringWriter();
      JsonWriter jsonWriter = new JsonWriter(stringWriter);
      jsonWriter.setLenient(true);
      Streams.write(this, jsonWriter);
      return stringWriter.toString();
    } catch (IOException e) {
      throw new AssertionError(e);
    }
View Full Code Here

        Multimap<String, Variable> variables = HashMultimap.create();
        for (Variable variable : _snitch.getVariables()) {
            variables.put(variable.getName(), variable);
        }

        JsonWriter writer = new JsonWriter(new BufferedWriter(response.getWriter()));
        writer.setIndent("  ")// Pretty print by default

        try {
            writer.beginObject();
            for (String name : variables.keySet()) {
                Collection<Variable> vars = variables.get(name);

                writer.name(name);
                if (vars.size() > 1) {
                    // Only render as an array if we have a name collision
                    writer.beginArray();
                }
                for (Variable variable : vars) {
                    Formatter formatter = _snitch.getFormatter(variable);
                    formatter.format(variable.getValue(), writer);
                }
                if (vars.size() > 1) {
                    writer.endArray();
                }
            }
            writer.endObject();
        } finally {
            Closeables.closeQuietly(writer);
        }
    }
View Full Code Here

TOP

Related Classes of com.google.gson.stream.JsonWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.