Java Code Examples for com.google.gson.internal.Streams#write()

The following examples show how to use com.google.gson.internal.Streams#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Gson.java    From letv with Apache License 2.0 6 votes vote down vote up
public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOException {
    boolean oldLenient = writer.isLenient();
    writer.setLenient(true);
    boolean oldHtmlSafe = writer.isHtmlSafe();
    writer.setHtmlSafe(this.htmlSafe);
    boolean oldSerializeNulls = writer.getSerializeNulls();
    writer.setSerializeNulls(this.serializeNulls);
    try {
        Streams.write(jsonElement, writer);
        writer.setLenient(oldLenient);
        writer.setHtmlSafe(oldHtmlSafe);
        writer.setSerializeNulls(oldSerializeNulls);
    } catch (Throwable e) {
        throw new JsonIOException(e);
    } catch (Throwable th) {
        writer.setLenient(oldLenient);
        writer.setHtmlSafe(oldHtmlSafe);
        writer.setSerializeNulls(oldSerializeNulls);
    }
}
 
Example 2
Source File: Gson.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example 3
Source File: JsonElement.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public String toString()
{
    String s;
    try
    {
        StringWriter stringwriter = new StringWriter();
        JsonWriter jsonwriter = new JsonWriter(stringwriter);
        jsonwriter.setLenient(true);
        Streams.write(this, jsonwriter);
        s = stringwriter.toString();
    }
    catch (IOException ioexception)
    {
        throw new AssertionError(ioexception);
    }
    return s;
}
 
Example 4
Source File: o.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public void write(JsonWriter jsonwriter, Object obj)
{
    if (a == null)
    {
        a().write(jsonwriter, obj);
        return;
    }
    if (obj == null)
    {
        jsonwriter.nullValue();
        return;
    } else
    {
        Streams.write(a.serialize(obj, d.getType(), c.c), jsonwriter);
        return;
    }
}
 
Example 5
Source File: JsonStringWrapperAdapterFactory.java    From devicehive-java-server with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, JsonStringWrapper value) throws IOException {
    if (value == null && out.getSerializeNulls()) {
        out.nullValue();
    } else if (value != null) {
        Streams.write(new JsonParser().parse(value.getJsonString()), out);
    }
}
 
Example 6
Source File: JsonUtil.java    From java-trader with Apache License 2.0 5 votes vote down vote up
public static String json2str(JsonElement json, Boolean pretty) {
   try {
       StringWriter stringWriter = new StringWriter(1024);
        JsonWriter jsonWriter = new JsonWriter(stringWriter);
        if ( pretty!=null && pretty ) {
            jsonWriter.setIndent("  ");
        }
        jsonWriter.setLenient(true);
        Streams.write(json, jsonWriter);
        return stringWriter.toString();
   }catch(Throwable t) {
       return json.toString();
   }
}
 
Example 7
Source File: JsonRenderer.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private void render(Writer writer, JsonElement json) throws IOException {
  JsonWriter jsonWriter = new JsonWriter(writer);
  jsonWriter.setLenient(false);
  jsonWriter.setIndent("");
  jsonWriter.setSerializeNulls(false);
  Streams.write(json, jsonWriter);
}
 
Example 8
Source File: JsonElement.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns a String representation of this element.
 */
@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);
  }
}
 
Example 9
Source File: TreeTypeAdapter.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public void write(JsonWriter out, T value) throws IOException {
  if (serializer == null) {
    delegate().write(out, value);
    return;
  }
  if (value == null) {
    out.nullValue();
    return;
  }
  JsonElement tree = serializer.serialize(value, typeToken.getType(), gson.serializationContext);
  Streams.write(tree, out);
}
 
Example 10
Source File: GsonInterfaceAdapter.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private <S> void writeObject(S value, JsonWriter out) throws IOException {
  if (value != null) {
    JsonObject jsonObject = new JsonObject();
    jsonObject.add(OBJECT_TYPE, new JsonPrimitive(value.getClass().getName()));
    TypeAdapter<S> delegate =
        (TypeAdapter<S>) this.gson.getDelegateAdapter(this.factory, TypeToken.get(value.getClass()));
    jsonObject.add(OBJECT_DATA, delegate.toJsonTree(value));
    Streams.write(jsonObject, out);
  } else {
    out.nullValue();
  }
}
 
Example 11
Source File: CustomProblemAdapter.java    From problem with MIT License 5 votes vote down vote up
@Override
public void write(final JsonWriter out, final T value) throws IOException {
    final JsonElement element = delegate.toJsonTree(value);
    final JsonObject object = element.getAsJsonObject();

    final URI problemType = URITypeAdapter.TYPE.fromJsonTree(object.remove("type"));
    object.add("type", URITypeAdapter.TYPE.toJsonTree(problemType));

    if (value instanceof AbstractThrowableProblem) {
        flattenParameters(object);
    }

    if (value instanceof Throwable) {
        // Get rid of unwanted fields.
        object.remove("detailMessage");
        object.remove("suppressedExceptions");
        object.remove("stackTrace");

        if (stackTraces) {
            object.add("stacktrace", gson.getAdapter(String[].class)
                    .toJsonTree(stream(((Throwable) value).getStackTrace())
                            .map(Object::toString)
                            .toArray(String[]::new)));
        }
    }

    Streams.write(element, out);
}
 
Example 12
Source File: JsonElement.java    From gson with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a String representation of this element.
 */
@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);
  }
}
 
Example 13
Source File: TreeTypeAdapter.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override public void write(JsonWriter out, T value) throws IOException {
  if (serializer == null) {
    delegate().write(out, value);
    return;
  }
  if (value == null) {
    out.nullValue();
    return;
  }
  JsonElement tree = serializer.serialize(value, typeToken.getType(), context);
  Streams.write(tree, out);
}
 
Example 14
Source File: JsonElementTypeHandler.java    From mybatis-gson with MIT License 5 votes vote down vote up
@Override
public void setNonNullParameter(PreparedStatement ps, int i, JsonElement parameter, JdbcType jdbcType) throws SQLException {
    try {
        StringWriter sw = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(sw);
        jsonWriter.setLenient(false);
        Streams.write(parameter, jsonWriter);
        ps.setString(i, sw.toString());
    } catch (IOException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}
 
Example 15
Source File: ManifestUtils.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void writeJsonToFile(JsonObject object, File file) {
	try (JsonWriter writer = new JsonWriter(Files.newWriter(file, Charset.defaultCharset()))) {
		Streams.write(object, writer);
	} catch (IOException e) {
		Wizardry.LOGGER.error("    > SOMETHING WENT WRONG! Could not create or write to file! Customizations to recipes and modules will be reset every time you load the game!");
		e.printStackTrace();
	}
}
 
Example 16
Source File: JsonElement.java    From letv with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 17
Source File: TreeTypeAdapter.java    From letv with Apache License 2.0 5 votes vote down vote up
public void write(JsonWriter out, T value) throws IOException {
    if (this.serializer == null) {
        delegate().write(out, value);
    } else if (value == null) {
        out.nullValue();
    } else {
        Streams.write(this.serializer.serialize(value, this.typeToken.getType(), this.gson.serializationContext), out);
    }
}
 
Example 18
Source File: MapTypeAdapterFactory.java    From letv with Apache License 2.0 4 votes vote down vote up
public void write(JsonWriter out, Map<K, V> map) throws IOException {
    if (map == null) {
        out.nullValue();
    } else if (MapTypeAdapterFactory.this.complexMapKeySerialization) {
        boolean hasComplexKeys = false;
        List<JsonElement> keys = new ArrayList(map.size());
        List<V> values = new ArrayList(map.size());
        for (Entry<K, V> entry : map.entrySet()) {
            JsonElement keyElement = MapTypeAdapterFactory.toJsonTree(this.keyTypeAdapter, entry.getKey());
            keys.add(keyElement);
            values.add(entry.getValue());
            int i = (keyElement.isJsonArray() || keyElement.isJsonObject()) ? 1 : 0;
            hasComplexKeys |= i;
        }
        int i2;
        if (hasComplexKeys) {
            out.beginArray();
            for (i2 = 0; i2 < keys.size(); i2++) {
                out.beginArray();
                Streams.write((JsonElement) keys.get(i2), out);
                this.valueTypeAdapter.write(out, values.get(i2));
                out.endArray();
            }
            out.endArray();
            return;
        }
        out.beginObject();
        for (i2 = 0; i2 < keys.size(); i2++) {
            out.name(keyToString((JsonElement) keys.get(i2)));
            this.valueTypeAdapter.write(out, values.get(i2));
        }
        out.endObject();
    } else {
        out.beginObject();
        for (Entry<K, V> entry2 : map.entrySet()) {
            out.name(String.valueOf(entry2.getKey()));
            this.valueTypeAdapter.write(out, entry2.getValue());
        }
        out.endObject();
    }
}