Skip to content

Commit

Permalink
[json-node] Add extract() method for double type (was missing) (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave authored Jan 8, 2025
1 parent 1c41b4d commit 4d92dc7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions json-node/src/main/java/io/avaje/json/node/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions json-node/src/test/java/io/avaje/json/node/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4d92dc7

Please sign in to comment.