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) {