Extend, improve set of non-number scalar value accessors for JsonNode
(JsonNode.booleanValue()
etc) [JSTEP-3]
#4991
Milestone
JsonNode
(JsonNode.booleanValue()
etc) [JSTEP-3]
#4991
(note: related to #4958 but distinct)
As explained on https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-3 we want to improve and extend the set of non-numeric Scalar value accessors like
asBoolean()
for Jackson 3.0.As of 2.x, there are a few accessors for types like, say,
boolean
orString
:booleanValue()
: returnboolean
if (and only if!) we got actual Boolean node -- but won't throw exception, returns default value (false
) otherwiseasBoolean()
: same asasBoolean(0)
asBoolean(boolean defaultValue
): returnboolean
value from JSON Boolean OR if coercible (from String); otherwise return
defaultValue`But none actually throws exception: partly due to historical reasons (was not done initially), and also since methods do not expose
JacksonException
(orIOException
). So this limitation is due to backwards compatibility (for 2.x)So there are a few things to improve for 3.0:
JsonNodeException
to useOptional
, as that is useful forstream()
operationsThis would lead to bigger set of methods, once again for
boolean
:booleanValue()
as before, return value if JSON Boolean -- but if not, throw exceptionbooleanValue(boolean defaultValue)
asbooleanValue()
except returnsdefaultValue
instead of exceptionbooleanValueOpt()
similar tobooleanValue()
but returnsOptional<Boolean>
, either present (as perintValue()
) or absent (instead of exception)asBoolean()
similar tobooleanValue()
but allows coercion from Integral numbers (0 ==false
, anything elsetrue
) andString
, as well asnull
But if not, throw exceptionasBooleanOpt()
likeasBoolean()
but returnsOptional<Boolean>
, either present (as perasBoolean()
) or absent (instead of exception)We should also do the same for
String
(by defaultstringValue()
only for JSON String;asString()
for all scalar-types but not Objects/Arrays).NOTE:
In addition to set for
boolean
there's:stringValue()
,asString()
And there are some partial accessors to handle too:
binaryValue()
NOTE 2:
This issue does NOT tackle
asXxx()
variants -- there's #5003 for that.The text was updated successfully, but these errors were encountered: