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

The following examples show how to use com.google.gson.stream.JsonWriter#getSerializeNulls() . 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: MessageBodyTypeAdapter.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public void write(JsonWriter out, MessageBody value) throws IOException {
	out.beginObject();
	out.name(ATTR_MESSAGE_TYPE);
	out.value(value.getMessageType());
	Map<String, Object> attributes = value.getAttributes();
	if(attributes == null || attributes.isEmpty()) {
		if(out.getSerializeNulls()) {
			out.name(ATTR_ATTRIBUTES);
			out.beginObject();
			out.endObject();
		}
	}
	else {
		out.name(ATTR_ATTRIBUTES);
		out.beginObject();
		for(Map.Entry<String, Object> e: attributes.entrySet()) {
			out.name(e.getKey());
			valueAdapter.write(out, e.getValue());
		}
		out.endObject();
	}
	out.endObject();
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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));
                }
            }
        };
    }