Package ca.marcusdunn.jsonlens.jackson
@NullMarked
package ca.marcusdunn.jsonlens.jackson
A JsonModel for Jackson 3 JsonNode values:
JacksonJsonModel.
// 1. Read the JSON value with your own JSON library. Keep numbers exact.
JsonMapper mapper = JsonMapper.builder()
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
.build();
JsonNode root = mapper.readTree(BOOKSTORE);
// 2. Parse the query. A query that is not valid gives an error value.
Result<JsonPathQuery, ParseError> parsed =
JsonPathParser.standard().parse("$.store.book[?@.price < 10].title");
// 3. Apply the query to the value through a JsonModel.
switch (parsed) {
case Result.Ok(JsonPathQuery query) -> {
Result<List<Node<JsonNode>>, EvaluationError> result =
JsonPathEvaluator.standard().evaluate(query, root, JacksonJsonModel.INSTANCE);
switch (result) {
case Result.Ok(List<Node<JsonNode>> nodes) -> {
for (Node<JsonNode> node : nodes) {
lines.add(node.path() + " = " + node.value());
}
}
case Result.Err(EvaluationError error) -> lines.add("Overflow: " + error.message());
}
}
case Result.Err(ParseError error) -> lines.add("Bad query: " + error.message());
}
// lines:
// $['store']['book'][0]['title'] = "Sayings of the Century"
// $['store']['book'][2]['title'] = "Moby Dick"
-
ClassDescriptionA
JsonModelfor Jackson 3JsonNodevalues.A JSON document that a Jackson streamingJsonParserreads on demand, with no tree.A value of aJacksonStream.An error of aJacksonStream: the parser could not give a JSON text.The input has no value.The parser gave a token that is not a JSON value.The parser failed.The input has more tokens after the root value.