From a7c9e9aca586370bf7b2c49e3c2e14dec61e8a02 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 16 Dec 2024 17:44:57 +1300 Subject: [PATCH] [json-core] Fix reading NULL value in a Map or List (#318) --- .../org/example/other/ObjectAdapterTest.java | 18 +++++++++++++ .../avaje/json/core/BasicObjectAdapter.java | 1 + .../avaje/json/simple/SimpleMapperTest.java | 26 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/blackbox-test/src/test/java/org/example/other/ObjectAdapterTest.java b/blackbox-test/src/test/java/org/example/other/ObjectAdapterTest.java index edbd674c..e373bb3c 100644 --- a/blackbox-test/src/test/java/org/example/other/ObjectAdapterTest.java +++ b/blackbox-test/src/test/java/org/example/other/ObjectAdapterTest.java @@ -30,6 +30,24 @@ void nullJson() { assertThat(asJson).isEqualTo(""); } + @Test + void nullInMap() { + Object val = objectType.fromJson("{\"a\":1,\"b\":null,\"c\":3}"); + assertThat(val).isInstanceOf(Map.class); + + String asJson = objectType.toJson(null); + assertThat(asJson).isEqualTo(""); + } + + @Test + void nullInList() { + Object val = objectType.fromJson("[1,null,3]"); + assertThat(val).isInstanceOf(List.class); + + String asJson = objectType.toJson(val); + assertThat(asJson).isEqualTo("[1,3]"); + } + @Test void booleanTrue() { String asJson = objectType.toJson(true); diff --git a/json-core/src/main/java/io/avaje/json/core/BasicObjectAdapter.java b/json-core/src/main/java/io/avaje/json/core/BasicObjectAdapter.java index dfb5f8cb..9e494da1 100644 --- a/json-core/src/main/java/io/avaje/json/core/BasicObjectAdapter.java +++ b/json-core/src/main/java/io/avaje/json/core/BasicObjectAdapter.java @@ -60,6 +60,7 @@ public Object fromJson(JsonReader reader) { case BOOLEAN: return booleanAdapter.fromJson(reader); case NULL: + reader.isNullValue(); return null; default: throw new IllegalStateException("Expected a value but was " + reader.currentToken() + " at path " + reader.location()); diff --git a/json-core/src/test/java/io/avaje/json/simple/SimpleMapperTest.java b/json-core/src/test/java/io/avaje/json/simple/SimpleMapperTest.java index 9666b2a3..48c262d1 100644 --- a/json-core/src/test/java/io/avaje/json/simple/SimpleMapperTest.java +++ b/json-core/src/test/java/io/avaje/json/simple/SimpleMapperTest.java @@ -32,6 +32,32 @@ void mapToJsonFromJson() { assertThat(mapFromJson2).isEqualTo(mapFromJson); } + @Test + void nullDirectly() { + var mapFromJson = simpleMapper.fromJson("null"); + assertThat(mapFromJson).isNull(); + } + + @Test + void mapWithNull() { + Map mapFromJson = simpleMapper.fromJsonObject("{\"one\":1,\"two\":null,\"three\":3}"); + + assertThat(mapFromJson).containsKeys("one", "two", "three"); + assertThat(mapFromJson.toString()).isEqualTo("{one=1, two=null, three=3}"); + + assertThat(simpleMapper.toJson(mapFromJson)).isEqualTo("{\"one\":1,\"three\":3}"); + } + + @Test + void listWithNull() { + List listFromJson = simpleMapper.fromJsonArray("[1,null,3]"); + + assertThat(listFromJson).hasSize(3); + assertThat(listFromJson.get(1)).isNull(); + + assertThat(simpleMapper.toJson(listFromJson)).isEqualTo("[1,3]"); + } + @Test void arrayToJsonFromJson() {