-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Milestone
Description
As per ideas on https://github.com/FasterXML/jackson-future-ideas/wiki/Jackson3-Changes---JsonNode it seems we could add couple of new, useful "validating" accessor methods, similar to how many other Java JSON libraries mandate presence of value when using accessors.
Here's a suggested list:
required(locator): similar toget(locator)andpath(locator)but will throw exception (instead of returningnullorMissingNode) with message indicating locatorlocatoris either property name (for Objects) or int index (for Arrays), same as with other accessors- Returns
JsonNodeat location, guaranteed not to benullorMissingNode - Exception to throw needs to be unchecked exception, even for 2.10 (in 3.0 we will likely change all Jackson exceptions to be unchecked) -- partly since we do not have access to context with any of the accessors
requiredAt(jsonPointer)(eitherStringor pre-compiledJsonPointer)- same as above, but allows convenient single-call verificatiion
require(): verify that node is NOTmissingNode; if it is, throw same exception as previous accessors would- essentially,
root.required("foo")would be equivalent toroot.path("foo").require()(except for slightly different error message) - returns
this
- essentially,
requireNonNull(): similarly torequire(), but also throw exception if "this" isNullNode(returnstrueforisNull())
So: these could improve JsonNode ergonomics a lot.
There are obviously many more we could consider (for example, whole set of requireInt() or such), but I think incremental improvements are a good way to go here.
Metadata
Metadata
Assignees
Labels
No labels