From e4bc37ecdd0cb6d3c24e3b31eab9170cf3d1e0f6 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Wed, 8 Jan 2025 21:03:59 +1300 Subject: [PATCH] [json-node] Add extract() method for double type (was missing) --- .../java/io/avaje/json/node/JsonNode.java | 9 +++++ .../java/io/avaje/json/node/JsonObject.java | 8 +++++ .../io/avaje/json/node/JsonObjectTest.java | 35 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/json-node/src/main/java/io/avaje/json/node/JsonNode.java b/json-node/src/main/java/io/avaje/json/node/JsonNode.java index 67e05303..811c961d 100644 --- a/json-node/src/main/java/io/avaje/json/node/JsonNode.java +++ b/json-node/src/main/java/io/avaje/json/node/JsonNode.java @@ -142,6 +142,15 @@ default long extract(String path, long missingValue) { throw new UnsupportedOperationException(); } + /** + * Extract the long from the given path if present or the given default value. + * + * @param missingValue The value to use when the path is missing. + */ + default double extract(String path, double missingValue) { + throw new UnsupportedOperationException(); + } + /** * Extract the int from the given path if present or the given default value. * diff --git a/json-node/src/main/java/io/avaje/json/node/JsonObject.java b/json-node/src/main/java/io/avaje/json/node/JsonObject.java index 11ff645b..31cbec88 100644 --- a/json-node/src/main/java/io/avaje/json/node/JsonObject.java +++ b/json-node/src/main/java/io/avaje/json/node/JsonObject.java @@ -245,6 +245,14 @@ public long extract(String path, long missingValue) { : ((JsonNumber) node).longValue(); } + @Override + public double extract(String path, double missingValue) { + final var node = find(path); + return !(node instanceof JsonNumber) + ? missingValue + : ((JsonNumber) node).doubleValue(); + } + @Override public Number extract(String path, Number missingValue) { final var node = find(path); diff --git a/json-node/src/test/java/io/avaje/json/node/JsonObjectTest.java b/json-node/src/test/java/io/avaje/json/node/JsonObjectTest.java index f27784af..24b2081e 100644 --- a/json-node/src/test/java/io/avaje/json/node/JsonObjectTest.java +++ b/json-node/src/test/java/io/avaje/json/node/JsonObjectTest.java @@ -202,4 +202,39 @@ void nullValuesInMap() { assertThat(jsonObject.elements().keySet()).containsExactly("a", "b", "c", "d"); } + + @Test + void extractNumbers() { + String s = "{\"aInt\":7,\"aDouble\":23.5,\"text\":\"foo\",\"bool\":true,\"aNull\":null}"; + JsonNodeMapper mapper = JsonNodeMapper.builder().build(); + JsonObject jsonObject = mapper.fromJsonObject(s); + + // Number becomes a Long if it can or otherwise a Double + assertThat(jsonObject.extract("aInt", BigDecimal.TEN)).isEqualTo(7L); + assertThat(jsonObject.extract("aDouble", BigDecimal.TEN)).isEqualTo(23.5D); + + assertThat(jsonObject.extract("aInt", 0)).isEqualTo(7); + assertThat(jsonObject.extract("aInt", 0L)).isEqualTo(7L); + assertThat(jsonObject.extract("aInt", 0D)).isEqualTo(7D); + + assertThat(jsonObject.extract("aDouble", 0)).isEqualTo(23); + assertThat(jsonObject.extract("aDouble", 0L)).isEqualTo(23L); + assertThat(jsonObject.extract("aDouble", 0D)).isEqualTo(23.5D); + + assertThat(jsonObject.extract("doesNotExist", 3)).isEqualTo(3); + assertThat(jsonObject.extract("doesNotExist", 3L)).isEqualTo(3L); + assertThat(jsonObject.extract("doesNotExist", 3.5D)).isEqualTo(3.5D); + + assertThat(jsonObject.extract("text", 3)).isEqualTo(3); + assertThat(jsonObject.extract("text", 3L)).isEqualTo(3L); + assertThat(jsonObject.extract("text", 3.5D)).isEqualTo(3.5D); + + assertThat(jsonObject.extract("bool", 3)).isEqualTo(3); + assertThat(jsonObject.extract("bool", 3L)).isEqualTo(3L); + assertThat(jsonObject.extract("bool", 3.5D)).isEqualTo(3.5D); + + assertThat(jsonObject.extract("aNull", 3)).isEqualTo(3); + assertThat(jsonObject.extract("aNull", 3L)).isEqualTo(3L); + assertThat(jsonObject.extract("aNull", 3.5D)).isEqualTo(3.5D); + } }