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

The following examples show how to use com.google.gson.stream.JsonWriter#nullValue() . 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: TypeAdapters.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void write(JsonWriter out, Calendar value) throws IOException {
  if (value == null) {
    out.nullValue();
    return;
  }
  out.beginObject();
  out.name(YEAR);
  out.value(value.get(Calendar.YEAR));
  out.name(MONTH);
  out.value(value.get(Calendar.MONTH));
  out.name(DAY_OF_MONTH);
  out.value(value.get(Calendar.DAY_OF_MONTH));
  out.name(HOUR_OF_DAY);
  out.value(value.get(Calendar.HOUR_OF_DAY));
  out.name(MINUTE);
  out.value(value.get(Calendar.MINUTE));
  out.name(SECOND);
  out.value(value.get(Calendar.SECOND));
  out.endObject();
}
 
Example 2
Source File: LazyJsonTypeAdapterFactory.java    From yawp with MIT License 6 votes vote down vote up
@Override
protected void write(JsonWriter out, LazyJson value, TypeAdapter<JsonElement> elementAdapter, TypeAdapter<LazyJson> delegate) throws IOException {
    if (value == null || value.getJson() == null) {
        out.nullValue();
        return;
    }

    String json = value.getJson();

    // This is done to avoid json serialize/deserialize to JsonObject since we already have the json as a String.
    // Could not find any better GSON API. JsonWriter wont let me write strings without escaping then.
    CustomJsonWriter customWriter = (CustomJsonWriter) out;

    begin(customWriter, value);
    customWriter.write(json.substring(1, json.length() - 1)); // Remove { and  }
    end(customWriter, value);
}
 
Example 3
Source File: Gson.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
private TypeAdapter<Number> doubleAdapter(boolean serializeSpecialFloatingPointValues) {
  if (serializeSpecialFloatingPointValues) {
    return TypeAdapters.DOUBLE;
  }
  return new TypeAdapter<Number>() {
    @Override public Double read(JsonReader in) throws IOException {
      if (in.peek() == JsonToken.NULL) {
        in.nextNull();
        return null;
      }
      return in.nextDouble();
    }
    @Override public void write(JsonWriter out, Number value) throws IOException {
      if (value == null) {
        out.nullValue();
        return;
      }
      double doubleValue = value.doubleValue();
      checkValidFloatingPoint(doubleValue);
      out.value(value);
    }
  };
}
 
Example 4
Source File: JSON.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, java.sql.Date date) throws IOException {
    if (date == null) {
        out.nullValue();
    } else {
        String value;
        if (dateFormat != null) {
            value = dateFormat.format(date);
        } else {
            value = date.toString();
        }
        out.value(value);
    }
}
 
Example 5
Source File: JSON.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, OffsetDateTime date) throws IOException {
    if (date == null) {
        out.nullValue();
    } else {
        out.value(formatter.format(date));
    }
}
 
Example 6
Source File: Excluder.java    From letv with Apache License 2.0 5 votes vote down vote up
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<?> rawType = type.getRawType();
    final boolean skipSerialize = excludeClass(rawType, true);
    final boolean skipDeserialize = excludeClass(rawType, false);
    if (!skipSerialize && !skipDeserialize) {
        return null;
    }
    final Gson gson2 = gson;
    final TypeToken<T> typeToken = type;
    return new TypeAdapter<T>() {
        private TypeAdapter<T> delegate;

        public T read(JsonReader in) throws IOException {
            if (!skipDeserialize) {
                return delegate().read(in);
            }
            in.skipValue();
            return null;
        }

        public void write(JsonWriter out, T value) throws IOException {
            if (skipSerialize) {
                out.nullValue();
            } else {
                delegate().write(out, value);
            }
        }

        private TypeAdapter<T> delegate() {
            TypeAdapter<T> d = this.delegate;
            if (d != null) {
                return d;
            }
            d = GsonInternalAccess.INSTANCE.getNextAdapter(gson2, Excluder.this, typeToken);
            this.delegate = d;
            return d;
        }
    };
}
 
Example 7
Source File: Excluder.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
  Class<?> rawType = type.getRawType();
  final boolean skipSerialize = excludeClass(rawType, true);
  final boolean skipDeserialize = excludeClass(rawType, false);

  if (!skipSerialize && !skipDeserialize) {
    return null;
  }

  return new TypeAdapter<T>() {
    /** The delegate is lazily created because it may not be needed, and creating it may fail. */
    private TypeAdapter<T> delegate;

    @Override public T read(JsonReader in) throws IOException {
      if (skipDeserialize) {
        in.skipValue();
        return null;
      }
      return delegate().read(in);
    }

    @Override public void write(JsonWriter out, T value) throws IOException {
      if (skipSerialize) {
        out.nullValue();
        return;
      }
      delegate().write(out, value);
    }

    private TypeAdapter<T> delegate() {
      TypeAdapter<T> d = delegate;
      return d != null
          ? d
          : (delegate = gson.getDelegateAdapter(Excluder.this, type));
    }
  };
}
 
Example 8
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 9
Source File: SampleHelper.java    From pxgrid-rest-ws with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, OffsetDateTime value) throws IOException {
	if (value == null) {
		out.nullValue();
		return;
	}
	out.value(formatter.format(value));
}
 
Example 10
Source File: OptionalAdapterFactory.java    From devicehive-java-server with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, Optional<?> value) throws IOException {
    if (value != null && value.isPresent()) {
        gson.toJson(value.get(), internalType, out);
    } else {
        out.nullValue();
    }
}
 
Example 11
Source File: JSON.java    From java with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, byte[] value) throws IOException {
    boolean oldHtmlSafe = out.isHtmlSafe();
    out.setHtmlSafe(false);
    if (value == null) {
        out.nullValue();
    } else {
        out.value(ByteString.of(value).base64());
    }
    out.setHtmlSafe(oldHtmlSafe);
}
 
Example 12
Source File: GsonSerDeProvider.java    From zerocode with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter writer, Headers value) throws IOException {
    if (value == null || !value.iterator().hasNext()) {
        writer.nullValue();
    } else {
        Map<String, String> headers = new HashMap<>();
        value.forEach(header -> headers.put(header.key(), new String(header.value())));
        gson.getAdapter(Map.class).write(writer, headers);
    }
}
 
Example 13
Source File: JSON.java    From huaweicloud-cs-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, LocalDate date) throws IOException {
    if (date == null) {
        out.nullValue();
    } else {
        out.value(formatter.format(date));
    }
}
 
Example 14
Source File: JSON.java    From eve-esi with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, byte[] value) throws IOException {
    if (value == null) {
        out.nullValue();
    } else {
        out.value(ByteString.of(value).base64());
    }
}
 
Example 15
Source File: StartTimeAdapter.java    From salt-netapi-client with MIT License 5 votes vote down vote up
@Override
public void write(JsonWriter jsonWriter, StartTime date) throws IOException {
    if (date == null) {
        jsonWriter.nullValue();
    } else {
        jsonWriter.value(date.toString());
    }
}
 
Example 16
Source File: BaseTypeAdapter.java    From protools with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter jsonWriter, T value) throws IOException {
    if (value == null) {
        jsonWriter.nullValue();
    } else {
        this.writing(jsonWriter, value);
    }
}
 
Example 17
Source File: DataTypeAdaptor.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, JsContractParamsModel value) throws IOException {
    if (value == null) {
        out.nullValue();
        return;
    }
    out.beginObject();
    out.name("nameOrId");
    gson.getAdapter(String.class).write(out, value.nameOrId);
    out.name("functionName");
    gson.getAdapter(String.class).write(out, value.functionName);
    out.name("valueList");
    gson.getAdapter(List.class).write(out, value.valueList);
    out.endObject();
}
 
Example 18
Source File: JSON.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public void write(JsonWriter out, byte[] value) throws IOException {
    if (value == null) {
        out.nullValue();
    } else {
        out.value(ByteString.of(value).base64());
    }
}
 
Example 19
Source File: SentryEnvelopeHeaderAdapter.java    From sentry-android with MIT License 5 votes vote down vote up
@Override
public void write(JsonWriter writer, SentryEnvelopeHeader value) throws IOException {
  if (value == null) {
    writer.nullValue();
    return;
  }
  writer.beginObject();

  if (value.getEventId() != null) {
    writer.name("event_id");
    writer.value(value.getEventId().toString());
  }

  writer.endObject();
}
 
Example 20
Source File: SessionAdapter.java    From sentry-android with MIT License 4 votes vote down vote up
@Override
public void write(JsonWriter writer, Session value) throws IOException {
  if (value == null) {
    writer.nullValue();
    return;
  }
  writer.beginObject();

  if (value.getSessionId() != null) {
    writer.name("sid").value(value.getSessionId().toString());
  }

  if (value.getDistinctId() != null) {
    writer.name("did").value(value.getDistinctId());
  }

  if (value.getInit() != null) {
    writer.name("init").value(value.getInit());
  }

  writer.name("started").value(DateUtils.getTimestamp(value.getStarted()));
  writer.name("status").value(value.getStatus().name().toLowerCase(Locale.ROOT));

  if (value.getSequence() != null) {
    writer.name("seq").value(value.getSequence());
  }

  int errorCount = value.errorCount();
  if (errorCount > 0) {
    writer.name("errors").value(errorCount);
  }

  if (value.getDuration() != null) {
    writer.name("duration").value(value.getDuration());
  }

  if (value.getTimestamp() != null) {
    writer.name("timestamp").value(DateUtils.getTimestamp(value.getTimestamp()));
  }

  boolean hasInitAttrs = false;
  hasInitAttrs = initAttrs(writer, hasInitAttrs);

  writer.name("release").value(value.getRelease());

  if (value.getEnvironment() != null) {
    hasInitAttrs = initAttrs(writer, hasInitAttrs);

    writer.name("environment").value(value.getEnvironment());
  }

  if (value.getIpAddress() != null) {
    hasInitAttrs = initAttrs(writer, hasInitAttrs);

    writer.name("ip_address").value(value.getIpAddress());
  }

  if (value.getUserAgent() != null) {
    hasInitAttrs = initAttrs(writer, hasInitAttrs);

    writer.name("user_agent").value(value.getUserAgent());
  }

  if (hasInitAttrs) {
    writer.endObject();
  }

  writer.endObject();
}