Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[json-node] Add extract() method for double type (was missing) #324

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading