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

PP-10498 add new method #607

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ public Map<String, String> getAsMap(JsonNode jsonNode) {
}
}
return null;
}
}

public Map<String, Object> getAsObjectMap(JsonNode jsonNode) {
if (jsonNode != null) {
if (!jsonNode.isNull() && jsonNode.isObject()) {
try {
return objectMapper.readValue(jsonNode.traverse(), new TypeReference<Map<String, Object>>() {});
} catch (IOException e) {
throw new RuntimeException("Malformed JSON object in value", e);
}
}
}
return null;
}

public List<String> getAsListOfString(JsonNode jsonNode) {
if (jsonNode != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public Map<String, String> valueAsObject() {
return jsonObjectMapper.getAsMap(value);
}

public Map<String, Object> valueAdObjectMap() {
return jsonObjectMapper.getAsObjectMap(value);
}


private JsonPatchRequest(JsonPatchOp op, String path, JsonNode value) {
this.op = op;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public void shouldListOfString() throws JsonProcessingException {
List<String> fromJson = jsonObjectMapper.getAsListOfString(jsonNode);
assertThat(fromJson, contains("foo", "bar"));
}

@Test
public void shouldMapToObjectMap() throws JsonProcessingException {
JsonNode jsonNode = objectMapper.readTree("{\"foo\":{\"other\":\"faa\"}}");
Map<String, Object> fromJson = jsonObjectMapper.getAsObjectMap(jsonNode);
assertThat(fromJson, hasEntry("foo", Map.of("other", "faa")));
}
}