I had to process some json, which could come in slightly different formats (and where I only needed a subset of the json data) and I used JsonPointer (from Jackson) to query the json. I wrote a non-functional solution to the problem, which worked for me, but I wanted to try a functional approach for learning purposes. In the below test program you can see my two solutions. They both work, but the functional solution became quite verbose and I have an annoying warning from Intellij regarding use of get() without isPresent check. I would like to see proposals how to improve the functional implementation and I am happy to see solutions using third-party libraries. The basic problem here, I suppose, is how to model an if-else-if-else, where each branch should return some value, in a functional way.
@Test
public void testIt() {
ObjectMapper om = new ObjectMapper();
ImmutableList.of(
"{ \"foo\": { \"key\": \"1\" } }",
"{ \"bar\": { \"key\": \"1\" } }",
"{ \"key\": \"1\" }")
.forEach(str -> {
try {
System.out.println("Non-functional: " + getNode(om.readTree(str)));
System.out.println("Functional: " + getNodeFunc(om.readTree(str)));
} catch (Exception e) {
throw new RuntimeException("", e);
}
});
}
private JsonNode getNode(JsonNode parentNode) {
JsonPointer jp1 = JsonPointer.compile("/foo");
JsonPointer jp2 = JsonPointer.compile("/bar");
if (!parentNode.at(jp1).isMissingNode()) {
return parentNode.at(jp1);
} else if (!parentNode.at(jp2).isMissingNode()) {
return parentNode.at(jp2);
}
return parentNode;
}
private JsonNode getNodeFunc(JsonNode parentNode) {
BiFunction<JsonNode, String, Optional<JsonNode>> findNode = (node, path) -> {
JsonPointer jp = JsonPointer.compile(path);
return node.at(jp).isMissingNode() ? Optional.empty() : Optional.of(node.at(jp));
};
return findNode.apply(parentNode, "/foo")
.map(Optional::of)
.orElseGet(() -> findNode.apply(parentNode, "/bar"))
.map(Optional::of)
.orElse(Optional.of(parentNode))
.get(); // Intellij complains here: Optional.get() without isPresent check
}
map(Optional::of)?findNode.apply()returns anOptional<JsonNode>so you don't need to usemap().