Java Code Examples for com.squareup.moshi.JsonWriter#nullValue()

The following examples show how to use com.squareup.moshi.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: WrappedJsonAdapter.java    From moshi-lazy-adapters with Apache License 2.0 6 votes vote down vote up
/**
 * Recursively writes the respective roots forming a json object that resembles the {@code path}
 * wrapping the type of the {@code adapter}.
 */
private static <T> void toJson(JsonAdapter<T> adapter, JsonWriter writer, T value,
    String[] path, int index) throws IOException {
  if (value != null || writer.getSerializeNulls()) {
    if (index == path.length) {
      adapter.toJson(writer, value);
    } else {
      writer.beginObject();
      writer.name(path[index]);
      toJson(adapter, writer, value, path, ++index);
      writer.endObject();
    }
  } else {
    // If we don't propagate the null value the writer will throw.
    writer.nullValue();
  }
}
 
Example 2
Source File: MoshiHelper.java    From moshi-jsonapi with MIT License 5 votes vote down vote up
public static void writeNull(JsonWriter writer, boolean enforced) throws IOException {
    if (enforced) {
        boolean serializeFlag = writer.getSerializeNulls();
        try {
            writer.setSerializeNulls(true);
            writer.nullValue();
        } finally {
            writer.setSerializeNulls(serializeFlag);
        }
    } else {
        writer.nullValue();
    }
}
 
Example 3
Source File: Rfc3339DateJsonAdapter.java    From moshi with Apache License 2.0 5 votes vote down vote up
@Override public synchronized void toJson(JsonWriter writer, Date value) throws IOException {
  if (value == null) {
    writer.nullValue();
  } else {
    String string = Iso8601Utils.format(value);
    writer.value(string);
  }
}
 
Example 4
Source File: NullSafeJsonAdapter.java    From moshi with Apache License 2.0 5 votes vote down vote up
@Override public void toJson(JsonWriter writer, @Nullable T value) throws IOException {
  if (value == null) {
    writer.nullValue();
  } else {
    delegate.toJson(writer, value);
  }
}
 
Example 5
Source File: MoshiHelper.java    From moshi-jsonapi with MIT License 4 votes vote down vote up
public static void dump(JsonReader reader, JsonWriter writer) throws IOException {
    int nested = 0;
    boolean nullFlag = writer.getSerializeNulls();
    writer.setSerializeNulls(true);
    try {
        while (reader.peek() != JsonReader.Token.END_DOCUMENT) {
            switch (reader.peek()) {
                case BEGIN_ARRAY:
                    nested++;
                    reader.beginArray();
                    writer.beginArray();
                    break;
                case END_ARRAY:
                    reader.endArray();
                    writer.endArray();
                    if (0 == --nested) return;
                    break;
                case BEGIN_OBJECT:
                    nested++;
                    reader.beginObject();
                    writer.beginObject();
                    break;
                case END_OBJECT:
                    reader.endObject();
                    writer.endObject();
                    if (0 == --nested) return;
                    break;
                case NAME:
                    writer.name(reader.nextName());
                    break;
                case NUMBER:
                    Double doubleValue = reader.nextDouble();
                    if (Math.floor(doubleValue) == doubleValue) {
                        writer.value(doubleValue.longValue());
                    } else {
                        writer.value(doubleValue);
                    }
                    break;
                case BOOLEAN:
                    writer.value(reader.nextBoolean());
                    break;
                case STRING:
                    writer.value(reader.nextString());
                    break;
                case NULL:
                    reader.nextNull();
                    writer.nullValue();
                    break;
            }
        }
    } finally {
        writer.setSerializeNulls(nullFlag);
    }
}