diff --git a/approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.java b/approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.java new file mode 100644 index 00000000..76210336 --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.java @@ -0,0 +1,26 @@ +package org.approvaltests; + +import com.spun.util.Wrapper; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +public class JsonApprovalsTest { + @Test + void nullDateTest() { + LocalDateWrapper localDateWrapper = new LocalDateWrapper(); + JsonApprovals.verifyAsJson(localDateWrapper, g -> g.serializeNulls()); + } + + private class LocalDateWrapper { + public LocalDateTime getLocalDate() { + return localDate; + } + + public void setLocalDate(LocalDateTime localDate) { + this.localDate = localDate; + } + + private LocalDateTime localDate; + } +} diff --git a/approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.nullDateTest.approved.json b/approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.nullDateTest.approved.json new file mode 100644 index 00000000..6c31a16c --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.nullDateTest.approved.json @@ -0,0 +1,3 @@ +{ + "localDate": null +} \ No newline at end of file diff --git a/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java b/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java index 90cfee33..97947c9e 100644 --- a/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java +++ b/approvaltests-util/src/main/java/com/spun/util/JsonUtils.java @@ -112,7 +112,11 @@ public static class LocalDateTimeAdapter extends TypeAdapter @Override public void write(JsonWriter jsonWriter, LocalDateTime instant) throws IOException { - jsonWriter.value(instant.toString()); + if (instant == null) { + jsonWriter.nullValue(); + } else { + jsonWriter.value("" + instant); + } } @Override public LocalDateTime read(JsonReader jsonReader) throws IOException