Class JacksonJsonModel
- All Implemented Interfaces:
JsonEditor<tools.jackson.databind.JsonNode>, JsonFactory<tools.jackson.databind.JsonNode>, JsonModel<tools.jackson.databind.JsonNode>
A JsonModel for Jackson 3 JsonNode values.
The model reads the nodes of the tree directly. It does not copy them, so each result
Normalized Path leads to a node of your own tree. The model has
no state: use INSTANCE.
// 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"
Exact numbers
By default, Jackson reads a JSON number with a fraction or an exponent as a
double. Such a number is not exact, and a number outside the range of
double becomes infinite. To keep all numbers exact, enable
DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS on the mapper:
JsonMapper mapper = JsonMapper.builder()
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
.build();
Changes
The model also implements JsonEditor: it changes ObjectNode and ArrayNode values in
place.
Member order
Jackson keeps the members of an object in document order, so the evaluator gives object members in document order.
Nodes that are not JSON
A Jackson tree can hold values that JSON cannot represent. The model classifies them as follows:
- A NaN or infinite
doubleorfloatis aJsonKind.STRINGwith the text"NaN","Infinity", or"-Infinity". - A binary node is a
JsonKind.STRINGwith the Base64 text that Jackson writes for it. - A POJO node is a
JsonKind.STRINGwith the text ofString.valueOfof the POJO. A POJO node that holdsnullis aJsonKind.NULL. - A missing node is a
JsonKind.NULL.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiontools.jackson.databind.JsonNodeBuilds an array.intarrayLength(tools.jackson.databind.JsonNode array) Returns the number of elements in an array.tools.jackson.databind.JsonNodebool(boolean value) Buildstrueorfalse.Maybe<tools.jackson.databind.JsonNode> element(tools.jackson.databind.JsonNode array, int index) Returns the element at a zero-based index.booleanhasDuplicate(tools.jackson.databind.JsonNode object, JsonString name) AnObjectNodeis a map, so it cannot hold duplicate names.voidinsertElement(tools.jackson.databind.JsonNode array, int index, tools.jackson.databind.JsonNode value) Inserts an element.kind(tools.jackson.databind.JsonNode node) Returns the kind of a node.Maybe<tools.jackson.databind.JsonNode> member(tools.jackson.databind.JsonNode object, JsonString name) Returns the value of the member with a given name.intmemberCount(tools.jackson.databind.JsonNode object) Returns the number of members in an object.MemberCursor<tools.jackson.databind.JsonNode> memberCursor(tools.jackson.databind.JsonNode object) Returns a cursor over the members of an object.tools.jackson.databind.JsonNodeBuildsnull.Maybe<tools.jackson.databind.JsonNode> number(JsonNumber value) Builds a number.numberValue(tools.jackson.databind.JsonNode number) Returns the value of a number.tools.jackson.databind.JsonNodeBuilds an object.Maybe<tools.jackson.databind.JsonNode> putMember(tools.jackson.databind.JsonNode object, JsonString name, tools.jackson.databind.JsonNode value) Sets the value of a member.tools.jackson.databind.JsonNoderemoveElement(tools.jackson.databind.JsonNode array, int index) Removes an element.Maybe<tools.jackson.databind.JsonNode> removeMember(tools.jackson.databind.JsonNode object, JsonString name) Removes a member.tools.jackson.databind.JsonNodesetElement(tools.jackson.databind.JsonNode array, int index, tools.jackson.databind.JsonNode value) Replaces an element.tools.jackson.databind.JsonNodestring(JsonString value) Builds a string.stringValue(tools.jackson.databind.JsonNode string) Returns the value of a string.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface JsonFactory
copyOfMethods inherited from interface JsonModel
compareNumbers, equal, members
-
Field Details
-
INSTANCE
The model. It has no state.
-
-
Method Details
-
kind
-
arrayLength
public int arrayLength(tools.jackson.databind.JsonNode array) Description copied from interface:JsonModelReturns the number of elements in an array.- Specified by:
arrayLengthin interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
array- a node of kindJsonKind.ARRAY- Returns:
- the number of elements, zero or more
-
element
public Maybe<tools.jackson.databind.JsonNode> element(tools.jackson.databind.JsonNode array, int index) Description copied from interface:JsonModelReturns the element at a zero-based index.- Specified by:
elementin interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
array- a node of kindJsonKind.ARRAYindex- a zero-based index. It can be outside the array.- Returns:
- the element, or
Maybe.Noneif the index is outside the array
-
memberCount
public int memberCount(tools.jackson.databind.JsonNode object) Description copied from interface:JsonModelReturns the number of members in an object.
The default method counts the members of the cursor. A model overrides it if it knows the number without a walk. The result must be the number of members of
JsonModel.memberCursor(Object).- Specified by:
memberCountin interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
object- a node of kindJsonKind.OBJECT- Returns:
- the number of members, zero or more
-
member
public Maybe<tools.jackson.databind.JsonNode> member(tools.jackson.databind.JsonNode object, JsonString name) Description copied from interface:JsonModelReturns the value of the member with a given name.
Names are equal only if they have the same Unicode scalar values. The model must not apply Unicode normalization or case folding (RFC 9535, Section 2.3.1.2).
The evaluator gives a name from the query, or a name from the cursor of this model or another model.
The default method walks the cursor, and gives the value of the first member with the name. A model overrides it to find the member faster, for example in a hash map. The result must be equal to the result of the default method.
- Specified by:
memberin interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
object- a node of kindJsonKind.OBJECTname- a member name- Returns:
- the member value, or
Maybe.Noneif the object has no member with the name
-
hasDuplicate
AnObjectNodeis a map, so it cannot hold duplicate names.- Specified by:
hasDuplicatein interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
object- a node of kindJsonKind.OBJECTname- a member name- Returns:
trueif the object has two or more members with the name
-
memberCursor
public MemberCursor<tools.jackson.databind.JsonNode> memberCursor(tools.jackson.databind.JsonNode object) Description copied from interface:JsonModelReturns a cursor over the members of an object.
The cursor is the one way to read the members: the default methods for objects use it. The order of the cursor is the order in which the evaluator selects the children of the object. RFC 9535 does not specify this order. The order must be the same each time the method is called for the same object. The cursor gives each member, also a member with the name of an earlier member.
The names can read the representation directly, as
JsonModel.stringValue(Object)does. The evaluator keeps a name in the Normalized Path of each result node, so a name must stay valid while the values of the model are valid.- Specified by:
memberCursorin interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
object- a node of kindJsonKind.OBJECT- Returns:
- a new cursor before the first member
-
stringValue
Description copied from interface:JsonModelReturns the value of a string.
The result can read the representation directly. For a
String, useJsonString.of(String).- Specified by:
stringValuein interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
string- a node of kindJsonKind.STRING- Returns:
- the value of the string
-
numberValue
Description copied from interface:JsonModelReturns the value of a number.
The model chooses the representation. For the common types, use the factories of
JsonNumber, for exampleJsonNumber.of(long).- Specified by:
numberValuein interfaceJsonModel<tools.jackson.databind.JsonNode>- Parameters:
number- a node of kindJsonKind.NUMBER- Returns:
- the value of the number
-
string
Description copied from interface:JsonFactoryBuilds a string.- Specified by:
stringin interfaceJsonFactory<tools.jackson.databind.JsonNode>- Parameters:
value- the value- Returns:
- a node of kind
JsonKind.STRING
-
number
Description copied from interface:JsonFactoryBuilds a number.
A representation can have a limit. For example, a
BigDecimalcannot hold a number with an exponent outside the range ofint. Then the result isMaybe.None: the factory never builds a number with a different value.- Specified by:
numberin interfaceJsonFactory<tools.jackson.databind.JsonNode>- Parameters:
value- the value- Returns:
- a node of kind
JsonKind.NUMBERwith the exact value, orMaybe.Noneif the representation cannot hold the value
-
bool
public tools.jackson.databind.JsonNode bool(boolean value) Description copied from interface:JsonFactoryBuildstrueorfalse.- Specified by:
boolin interfaceJsonFactory<tools.jackson.databind.JsonNode>- Parameters:
value- the value- Returns:
- a node of kind
JsonKind.TRUEorJsonKind.FALSE
-
nullValue
public tools.jackson.databind.JsonNode nullValue()Description copied from interface:JsonFactoryBuildsnull.- Specified by:
nullValuein interfaceJsonFactory<tools.jackson.databind.JsonNode>- Returns:
- a node of kind
JsonKind.NULL
-
array
Description copied from interface:JsonFactoryBuilds an array.- Specified by:
arrayin interfaceJsonFactory<tools.jackson.databind.JsonNode>- Parameters:
elements- the elements, in order- Returns:
- a node of kind
JsonKind.ARRAY
-
object
public tools.jackson.databind.JsonNode object(List<Property<tools.jackson.databind.JsonNode>> members) Description copied from interface:JsonFactoryBuilds an object.- Specified by:
objectin interfaceJsonFactory<tools.jackson.databind.JsonNode>- Parameters:
members- the members, in order. The names are unique.- Returns:
- a node of kind
JsonKind.OBJECT
-
putMember
public Maybe<tools.jackson.databind.JsonNode> putMember(tools.jackson.databind.JsonNode object, JsonString name, tools.jackson.databind.JsonNode value) Description copied from interface:JsonEditorSets the value of a member. If the object has a member with the name, the method replaces its value. Otherwise, it adds a member.- Specified by:
putMemberin interfaceJsonEditor<tools.jackson.databind.JsonNode>- Parameters:
object- a node of kindJsonKind.OBJECTname- the member namevalue- the new value- Returns:
- the previous value, or
Maybe.Noneif the object had no member with the name
-
removeMember
public Maybe<tools.jackson.databind.JsonNode> removeMember(tools.jackson.databind.JsonNode object, JsonString name) Description copied from interface:JsonEditorRemoves a member.- Specified by:
removeMemberin interfaceJsonEditor<tools.jackson.databind.JsonNode>- Parameters:
object- a node of kindJsonKind.OBJECTname- the member name- Returns:
- the removed value, or
Maybe.Noneif the object had no member with the name
-
insertElement
public void insertElement(tools.jackson.databind.JsonNode array, int index, tools.jackson.databind.JsonNode value) Description copied from interface:JsonEditorInserts an element. The elements at and after the index move one position to the right.- Specified by:
insertElementin interfaceJsonEditor<tools.jackson.databind.JsonNode>- Parameters:
array- a node of kindJsonKind.ARRAYindex- the index of the new element: from 0 to the length of the array. The length appends the element.value- the new element
-
setElement
public tools.jackson.databind.JsonNode setElement(tools.jackson.databind.JsonNode array, int index, tools.jackson.databind.JsonNode value) Description copied from interface:JsonEditorReplaces an element.- Specified by:
setElementin interfaceJsonEditor<tools.jackson.databind.JsonNode>- Parameters:
array- a node of kindJsonKind.ARRAYindex- the index of the element: from 0 to the length of the array minus 1value- the new element- Returns:
- the previous element
-
removeElement
public tools.jackson.databind.JsonNode removeElement(tools.jackson.databind.JsonNode array, int index) Description copied from interface:JsonEditorRemoves an element. The elements after the index move one position to the left.- Specified by:
removeElementin interfaceJsonEditor<tools.jackson.databind.JsonNode>- Parameters:
array- a node of kindJsonKind.ARRAYindex- the index of the element: from 0 to the length of the array minus 1- Returns:
- the removed element
-