Package com.google.gson.stream

Examples of com.google.gson.stream.JsonWriter


  private String buildString() {
    if (type.isSimpleType()) {
      return '"' + type.name().toLowerCase() + '"';
    }
    StringBuilder builder = new StringBuilder();
    JsonWriter writer = new JsonWriter(CharStreams.asWriter(builder));
    try {
      new co.cask.cdap.internal.io.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


      UrlParams.Builder paramsBuilder = new UrlParams.Builder(spec);
      if (spec.hasParamsObject()) {
        Type paramType = spec.getType();
        FieldNavigator navigator = new FieldNavigator(paramType);
        StringWriter json = new StringWriter();
        final JsonWriter jsonWriter = new JsonWriter(json);
        jsonWriter.beginObject();
        ValueReceiver receiver = new ValueReceiver() {
          @SuppressWarnings({"unchecked", "rawtypes"})
          @Override
          public void put(String name, Type type, Object value) throws IOException {
            jsonWriter.name(name);
            if (type instanceof TypeVariable || type == Object.class) {
              // We can not use Gson to extract the value, so just use the specified value.
              jsonWriter.value((String)value);
            } else {
              TypeAdapter adapter = gson.getAdapter(TypeToken.get(type));
              adapter.write(jsonWriter, value);
            }
          }
        };
        for (Field f : navigator.getFields()) {
          extractUrlParam(f.getName(), f.getGenericType(), requestParams, receiver);
        }
        jsonWriter.endObject();
        jsonWriter.close();
        Object obj = gson.fromJson(json.toString(), paramType);
        paramsBuilder = new UrlParams.Builder(spec, obj);
      } else {
        paramsBuilder = new UrlParams.Builder(spec);
      }
View Full Code Here

    private final FileOfCallerConverter fileConverter = new FileOfCallerConverter();
    private final LineOfCallerConverter lineConverter = new LineOfCallerConverter();

    private String convert(ILoggingEvent event) {
      StringWriter result = new StringWriter();
      JsonWriter writer = new JsonWriter(result);

      try {
        try {
          writer.beginObject();
          writer.name("name").value(event.getLoggerName());
          writer.name("host").value(hostname);
          writer.name("timestamp").value(Long.toString(event.getTimeStamp()));
          writer.name("level").value(event.getLevel().toString());
          writer.name("className").value(classNameConverter.convert(event));
          writer.name("method").value(methodConverter.convert(event));
          writer.name("file").value(fileConverter.convert(event));
          writer.name("line").value(lineConverter.convert(event));
          writer.name("thread").value(event.getThreadName());
          writer.name("message").value(event.getFormattedMessage());
          writer.name("stackTraces");
          encodeStackTraces(event.getThrowableProxy(), writer);

          writer.endObject();
        } finally {
          writer.close();
        }
      } catch (IOException e) {
        throw Throwables.propagate(e);
      }
View Full Code Here

   * @throws JsonIOException if there was a problem writing to the writer
   * @since 1.2
   */
  public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOException {
    try {
      JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(writer));
      toJson(src, typeOfSrc, jsonWriter);
    } catch (IOException e) {
      throw new JsonIOException(e);
    }
  }
View Full Code Here

   * @throws JsonIOException if there was a problem writing to the writer
   * @since 1.4
   */
  public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOException {
    try {
      JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(writer));
      toJson(jsonElement, jsonWriter);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here

   */
  private JsonWriter newJsonWriter(Writer writer) throws IOException {
    if (generateNonExecutableJson) {
      writer.write(JSON_NON_EXECUTABLE_PREFIX);
    }
    JsonWriter jsonWriter = new JsonWriter(writer);
    if (prettyPrinting) {
      jsonWriter.setIndent("  ");
    }
    jsonWriter.setSerializeNulls(serializeNulls);
    return jsonWriter;
  }
View Full Code Here

   * @throws JsonIOException if there was a problem writing to the writer
   * @since 1.2
   */
  public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOException {
    try {
      JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(writer));
      toJson(src, typeOfSrc, jsonWriter);
    } catch (IOException e) {
      throw new JsonIOException(e);
    }
  }
View Full Code Here

   * @throws JsonIOException if there was a problem writing to the writer
   * @since 1.4
   */
  public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOException {
    try {
      JsonWriter jsonWriter = newJsonWriter(Streams.writerForAppendable(writer));
      toJson(jsonElement, jsonWriter);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here

   */
  private JsonWriter newJsonWriter(Writer writer) throws IOException {
    if (generateNonExecutableJson) {
      writer.write(JSON_NON_EXECUTABLE_PREFIX);
    }
    JsonWriter jsonWriter = new JsonWriter(writer);
    if (prettyPrinting) {
      jsonWriter.setIndent("  ");
    }
    jsonWriter.setSerializeNulls(serializeNulls);
    return jsonWriter;
  }
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

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.