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

The following examples show how to use com.google.gson.stream.JsonWriter#setSerializeNulls() . 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: MapedSql.java    From sumk with Apache License 2.0 6 votes vote down vote up
public String toJson(JsonWriterVisitor visitor) throws Exception {
	StringWriter stringWriter = new StringWriter();
	JsonWriter writer = new JsonWriter(stringWriter);
	writer.setSerializeNulls(true);
	writer.beginObject();
	writer.name("sql").value(sql);
	writer.name("hash").value(sql.hashCode());
	String params = DBGson.toJson(paramters);
	params = LogKits.shorterSubfix(params, DBSettings.maxSqlParamLength());
	writer.name("paramters").value(params);
	if (visitor != null) {
		visitor.visit(writer);
	}
	writer.endObject();
	writer.flush();
	writer.close();
	return stringWriter.toString();

}
 
Example 4
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 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: MessageTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Use this method to write a {@code null} value even if the JSON writer is set to not serialize {@code null}.
 */
protected void writeNullValue(JsonWriter out) throws IOException {
	boolean previousSerializeNulls = out.getSerializeNulls();
	out.setSerializeNulls(true);
	out.nullValue();
	out.setSerializeNulls(previousSerializeNulls);
}
 
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: GsonRequestBodyConverter.java    From proteus with Apache License 2.0 5 votes vote down vote up
@Override
public RequestBody convert(T value) throws IOException {
  TypeAdapter<T> adapter = getAdapter();
  Buffer buffer = new Buffer();
  Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
  JsonWriter jsonWriter = gson.newJsonWriter(writer);
  jsonWriter.setSerializeNulls(true);
  adapter.write(jsonWriter, value);
  jsonWriter.close();
  return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
 
Example 9
Source File: RequiredFieldTypeAdapterFactory.java    From plaid-java with MIT License 5 votes vote down vote up
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
  if (!type.getRawType().isAssignableFrom(RequiredField.class)) {
    return null;
  }

  final TypeAdapter delegateAdapter = gson.getAdapter(TypeToken.get(((ParameterizedType) type.getType()).getActualTypeArguments()[0]));

  return new TypeAdapter<T>() {
    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void write(JsonWriter out, T value) throws IOException {
      RequiredField requiredField = (RequiredField) value;

      if (requiredField == null || !requiredField.isPresent()) {
        boolean oldSerializeNulls = out.getSerializeNulls();
        out.setSerializeNulls(true);
        out.nullValue();
        out.setSerializeNulls(oldSerializeNulls);
      } else {
        delegateAdapter.write(out, requiredField.get());
      }
    }

    @Override
    @SuppressWarnings("unchecked")
    public T read(JsonReader reader) throws IOException {
      return (T) RequiredField.of(delegateAdapter.read(reader));
    }
  };
}
 
Example 10
Source File: Gson.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns a new JSON writer configured for this GSON and with the non-execute
 * prefix if that is configured.
 */
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;
}
 
Example 11
Source File: Gson.java    From gson with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new JSON writer configured for the settings on this Gson instance.
 */
public 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;
}
 
Example 12
Source File: Gson.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private JsonWriter a(Writer writer)
{
    if (k)
    {
        writer.write(")]}'\n");
    }
    JsonWriter jsonwriter = new JsonWriter(writer);
    if (l)
    {
        jsonwriter.setIndent("  ");
    }
    jsonwriter.setSerializeNulls(i);
    return jsonwriter;
}
 
Example 13
Source File: VersionedTextDocumentIdentifierTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
protected void writeVersion(final JsonWriter out, final Integer value) throws IOException {
  if ((value == null)) {
    final boolean previousSerializeNulls = out.getSerializeNulls();
    out.setSerializeNulls(true);
    out.nullValue();
    out.setSerializeNulls(previousSerializeNulls);
  } else {
    out.value(value);
  }
}
 
Example 14
Source File: InitializeParamsTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
protected void writeRootUri(final JsonWriter out, final String value) throws IOException {
  if ((value == null)) {
    final boolean previousSerializeNulls = out.getSerializeNulls();
    out.setSerializeNulls(true);
    out.nullValue();
    out.setSerializeNulls(previousSerializeNulls);
  } else {
    out.value(value);
  }
}
 
Example 15
Source File: InitializeParamsTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
protected void writeProcessId(final JsonWriter out, final Integer value) throws IOException {
  if ((value == null)) {
    final boolean previousSerializeNulls = out.getSerializeNulls();
    out.setSerializeNulls(true);
    out.nullValue();
    out.setSerializeNulls(previousSerializeNulls);
  } else {
    out.value(value);
  }
}
 
Example 16
Source File: MapTypeAdapterFactory.java    From nifi-swagger-client with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, Map<String,String> map) throws IOException {
    boolean oldSerializeNulls = out.getSerializeNulls();
    out.setSerializeNulls(true);
    mapAdapter.write(out, map);
    out.setSerializeNulls(oldSerializeNulls);
}
 
Example 17
Source File: Gson.java    From letv with Apache License 2.0 5 votes vote down vote up
private JsonWriter newJsonWriter(Writer writer) throws IOException {
    if (this.generateNonExecutableJson) {
        writer.write(JSON_NON_EXECUTABLE_PREFIX);
    }
    JsonWriter jsonWriter = new JsonWriter(writer);
    if (this.prettyPrinting) {
        jsonWriter.setIndent("  ");
    }
    jsonWriter.setSerializeNulls(this.serializeNulls);
    return jsonWriter;
}
 
Example 18
Source File: NullableTypeAdapterFactory.java    From v20-java with MIT License 4 votes vote down vote up
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

        if (type.getRawType() != NullableType.class) {
            return null;
        }

        if (getBaseType(type.getType()) != this.containedClass)
        {
            return null;
        }

        final TypeAdapter<C> delegate  = gson.getDelegateAdapter(this, TypeToken.get(this.containedClass));

        return new TypeAdapter<T>() {
            @SuppressWarnings("unchecked")
            @Override
            public void write(JsonWriter out, T value) throws IOException {
                if (value == null || ! ((NullableType<C>) value).isSet()) {
                    out.nullValue();
                }
                else if (((NullableType<C>) value).getValue() == null) {
                    boolean oldSerializeNulls = out.getSerializeNulls();
                    out.setSerializeNulls(true);
                    out.nullValue();
                    out.setSerializeNulls(oldSerializeNulls);
                }
                else {
                    try {
                        delegate.write(out, (C) (((NullableType<C>) value).getValue()));
                    } catch (IllegalArgumentException e) {
                        throw new IOException(e);
                    }
                }
            }

            @SuppressWarnings("unchecked")
            @Override
            public T read(JsonReader in) throws IOException {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                    return (T) new NullableType<C>();
                } else {
                    return (T) new NullableType<C>(delegate.read(in));
                }
            }
        };
    }
 
Example 19
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 ? "  " : "");
}