示例#1
0
 /**
  * Writes out the equivalent JSON for a tree of {@link JsonElement}s.
  *
  * @param jsonElement root of a tree of {@link JsonElement}s
  * @param writer Writer to which the Json representation needs to be written
  * @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);
   }
 }
示例#2
0
 /**
  * This method serializes the specified object, including those of generic types, into its
  * equivalent Json representation. This method must be used if the specified object is a generic
  * type. For non-generic objects, use {@link #toJson(Object, Appendable)} instead.
  *
  * @param src the object for which JSON representation is to be created
  * @param typeOfSrc The specific genericized type of src. You can obtain this type by using the
  *     {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for {@code
  *     Collection<Foo>}, you should use:
  *     <pre>
  * Type typeOfSrc = new TypeToken&lt;Collection&lt;Foo&gt;&gt;(){}.getType();
  * </pre>
  *
  * @param writer Writer to which the Json representation of src needs to be written.
  * @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);
   }
 }
示例#3
0
 /**
  * Writes the JSON for {@code jsonElement} to {@code writer}.
  *
  * @throws JsonIOException if there was a problem writing to the writer
  */
 public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException {
   boolean oldLenient = writer.isLenient();
   writer.setLenient(true);
   boolean oldHtmlSafe = writer.isHtmlSafe();
   writer.setHtmlSafe(htmlSafe);
   boolean oldSerializeNulls = writer.getSerializeNulls();
   writer.setSerializeNulls(serializeNulls);
   try {
     Streams.write(jsonElement, writer);
   } catch (IOException e) {
     throw new JsonIOException(e);
   } finally {
     writer.setLenient(oldLenient);
     writer.setHtmlSafe(oldHtmlSafe);
     writer.setSerializeNulls(oldSerializeNulls);
   }
 }