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

The following examples show how to use com.squareup.moshi.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: 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 2
Source File: SerializeNullsJsonAdapterTest.java    From moshi-lazy-adapters with Apache License 2.0 5 votes vote down vote up
@Test public void maintainsPreviousSerializationValue() throws Exception {
  JsonAdapter<Data1> adapter = moshi.adapter(Data1.class);
  Data1 data1 = new Data1();

  JsonWriter writer1 = JsonWriter.of(new Buffer());
  writer1.setSerializeNulls(true);
  adapter.toJson(writer1, data1);
  assertThat(writer1.getSerializeNulls()).isTrue();

  JsonWriter writer2 = JsonWriter.of(new Buffer());
  writer2.setSerializeNulls(false);
  adapter.toJson(writer2, data1);
  assertThat(writer2.getSerializeNulls()).isFalse();
}
 
Example 3
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);
    }
}