Java Code Examples for com.google.gson.stream.JsonWriter#setLenient()

The following examples show how to use com.google.gson.stream.JsonWriter#setLenient() . 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(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException {
    TypeAdapter<?> adapter = getAdapter(TypeToken.get(typeOfSrc));
    boolean oldLenient = writer.isLenient();
    writer.setLenient(true);
    boolean oldHtmlSafe = writer.isHtmlSafe();
    writer.setHtmlSafe(this.htmlSafe);
    boolean oldSerializeNulls = writer.getSerializeNulls();
    writer.setSerializeNulls(this.serializeNulls);
    try {
        adapter.write(writer, src);
        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 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 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: MixedStreamTest.java    From gson with Apache License 2.0 6 votes vote down vote up
public void testWriteDoesNotMutateState() throws IOException {
  Gson gson = new Gson();
  JsonWriter jsonWriter = new JsonWriter(new StringWriter());
  jsonWriter.beginArray();

  jsonWriter.setHtmlSafe(true);
  jsonWriter.setLenient(true);
  gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter);
  assertTrue(jsonWriter.isHtmlSafe());
  assertTrue(jsonWriter.isLenient());

  jsonWriter.setHtmlSafe(false);
  jsonWriter.setLenient(false);
  gson.toJson(BLUE_MUSTANG, Car.class, jsonWriter);
  assertFalse(jsonWriter.isHtmlSafe());
  assertFalse(jsonWriter.isLenient());
}
 
Example 5
Source File: Gson.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Writes the JSON representation of {@code src} of type {@code typeOfSrc} to
 * {@code writer}.
 * @throws JsonIOException if there was a problem writing to the writer
 */
@SuppressWarnings("unchecked")
public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException {
  TypeAdapter<?> adapter = getAdapter(TypeToken.get(typeOfSrc));
  boolean oldLenient = writer.isLenient();
  writer.setLenient(true);
  boolean oldHtmlSafe = writer.isHtmlSafe();
  writer.setHtmlSafe(htmlSafe);
  boolean oldSerializeNulls = writer.getSerializeNulls();
  writer.setSerializeNulls(serializeNulls);
  try {
    ((TypeAdapter<Object>) adapter).write(writer, src);
  } catch (IOException e) {
    throw new JsonIOException(e);
  } finally {
    writer.setLenient(oldLenient);
    writer.setHtmlSafe(oldHtmlSafe);
    writer.setSerializeNulls(oldSerializeNulls);
  }
}
 
Example 6
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 7
Source File: LenientGsonRequestBodyConverter.java    From youqu_master with Apache License 2.0 5 votes vote down vote up
@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);
    jsonWriter.setLenient(true);
    adapter.write(jsonWriter, value);
    jsonWriter.close();
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
 
Example 8
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 9
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 10
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 11
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 12
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 13
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 14
Source File: GsonGenerator.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
GsonGenerator(GsonFactory factory, JsonWriter writer) {
  this.factory = factory;
  this.writer = writer;
  // lenient to allow top-level values of any type
  writer.setLenient(true);
}
 
Example 15
Source File: GsonMessageBodyProvider.java    From immutables with Apache License 2.0 4 votes vote down vote up
void setWriterOptions(JsonWriter writer) {
  writer.setSerializeNulls(serializeNulls);
  writer.setLenient(lenient);
  writer.setHtmlSafe(htmlSafe);
  writer.setIndent(prettyPrinting ? "  " : "");
}