From a8d0d30ba49e5493403726da1be3124a5c52dc44 Mon Sep 17 00:00:00 2001 From: gbonaventure <123872007+gregory-bonaventure@users.noreply.github.com> Date: Mon, 23 Feb 2026 00:33:32 +0100 Subject: [PATCH] fix: handle null Java values in JsonMapper for absent JSON fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a JSON field is absent from the object, Map.get() returns Java null (not JsonNull). All JsonMapper methods now treat null the same as JsonNull — returning null for nullable types and throwing a clear error for primitive types. Co-Authored-By: Claude Opus 4.6 --- .../io/mktflow/json/internal/JsonMapper.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/just-json-core/src/main/java/io/mktflow/json/internal/JsonMapper.java b/just-json-core/src/main/java/io/mktflow/json/internal/JsonMapper.java index e118a6e..b0a17f8 100644 --- a/just-json-core/src/main/java/io/mktflow/json/internal/JsonMapper.java +++ b/just-json-core/src/main/java/io/mktflow/json/internal/JsonMapper.java @@ -15,7 +15,7 @@ public final class JsonMapper { private JsonMapper() {} public static String toString(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonString s) { @@ -28,39 +28,39 @@ public static int toInt(JsonValue value) { if (value instanceof JsonValue.JsonNumber n) { return Integer.parseInt(n.value()); } - throw new JsonException("Expected JSON number but got " + value.getClass().getSimpleName()); + throw new JsonException("Expected JSON number but got " + (value == null ? "null" : value.getClass().getSimpleName())); } public static long toLong(JsonValue value) { if (value instanceof JsonValue.JsonNumber n) { return Long.parseLong(n.value()); } - throw new JsonException("Expected JSON number but got " + value.getClass().getSimpleName()); + throw new JsonException("Expected JSON number but got " + (value == null ? "null" : value.getClass().getSimpleName())); } public static double toDouble(JsonValue value) { if (value instanceof JsonValue.JsonNumber n) { return Double.parseDouble(n.value()); } - throw new JsonException("Expected JSON number but got " + value.getClass().getSimpleName()); + throw new JsonException("Expected JSON number but got " + (value == null ? "null" : value.getClass().getSimpleName())); } public static float toFloat(JsonValue value) { if (value instanceof JsonValue.JsonNumber n) { return Float.parseFloat(n.value()); } - throw new JsonException("Expected JSON number but got " + value.getClass().getSimpleName()); + throw new JsonException("Expected JSON number but got " + (value == null ? "null" : value.getClass().getSimpleName())); } public static boolean toBoolean(JsonValue value) { if (value instanceof JsonValue.JsonBoolean b) { return b.value(); } - throw new JsonException("Expected JSON boolean but got " + value.getClass().getSimpleName()); + throw new JsonException("Expected JSON boolean but got " + (value == null ? "null" : value.getClass().getSimpleName())); } public static Integer toIntegerBoxed(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonNumber n) { @@ -70,7 +70,7 @@ public static Integer toIntegerBoxed(JsonValue value) { } public static Long toLongBoxed(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonNumber n) { @@ -80,7 +80,7 @@ public static Long toLongBoxed(JsonValue value) { } public static Double toDoubleBoxed(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonNumber n) { @@ -90,7 +90,7 @@ public static Double toDoubleBoxed(JsonValue value) { } public static Float toFloatBoxed(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonNumber n) { @@ -100,7 +100,7 @@ public static Float toFloatBoxed(JsonValue value) { } public static Boolean toBooleanBoxed(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonBoolean b) { @@ -110,7 +110,7 @@ public static Boolean toBooleanBoxed(JsonValue value) { } public static BigDecimal toBigDecimal(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonNumber n) { @@ -120,7 +120,7 @@ public static BigDecimal toBigDecimal(JsonValue value) { } public static BigInteger toBigInteger(JsonValue value) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonNumber n) { @@ -130,7 +130,7 @@ public static BigInteger toBigInteger(JsonValue value) { } public static > E toEnum(JsonValue value, Class enumClass) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonString s) { @@ -140,7 +140,7 @@ public static > E toEnum(JsonValue value, Class enumClass) } public static > E toEnum(JsonValue value, Map lookup) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonString s) { @@ -154,7 +154,7 @@ public static > E toEnum(JsonValue value, Map looku } public static List toList(JsonValue value, Function elementMapper) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonArray arr) { @@ -168,7 +168,7 @@ public static List toList(JsonValue value, Function element } public static Map toMap(JsonValue value, Function valueMapper) { - if (value instanceof JsonValue.JsonNull) { + if (value == null || value instanceof JsonValue.JsonNull) { return null; } if (value instanceof JsonValue.JsonObject obj) {