(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 or String:
booleanValue(): return boolean if (and only if!) we got actual Boolean node -- but won't throw exception, returns default value (false) otherwise
asBoolean(): same as asBoolean(0)
asBoolean(boolean defaultValue): return boolean 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 (or IOException). So this limitation is due to backwards compatibility (for 2.x)
So there are a few things to improve for 3.0:
- Should allow throwing exceptions, for case where no default is specified. We can now do this more easily as we throw unchecked exception -- meaning accessors are still safe with Java 8 streaming
- Jackson 3.0 now has
JsonNodeException to use
- Should allow use of Java 8
Optional, as that is useful for stream() operations
This would lead to bigger set of methods, once again for boolean:
booleanValue() as before, return value if JSON Boolean -- but if not, throw exception
booleanValue(boolean defaultValue) as booleanValue() except returns defaultValue instead of exception
booleanValueOpt() similar to booleanValue() but returns Optional<Boolean>, either present (as per intValue()) or absent (instead of exception)
asBoolean() similar to booleanValue() but allows coercion from Integral numbers (0 == false, anything else true) and String, as well as null But if not, throw exception
asBooleanOpt() like asBoolean() but returns Optional<Boolean>, either present (as per asBoolean()) or absent (instead of exception)
We should also do the same for String (by default stringValue() 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:
NOTE 2:
This issue does NOT tackle asXxx() variants -- there's #5003 for that.
(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,
booleanorString:booleanValue(): returnbooleanif (and only if!) we got actual Boolean node -- but won't throw exception, returns default value (false) otherwiseasBoolean(): same asasBoolean(0)asBoolean(boolean defaultValue): returnbooleanvalue from JSON Boolean OR if coercible (from String); otherwise returndefaultValue`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:
JsonNodeExceptionto 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 returnsdefaultValueinstead 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 asnullBut 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
booleanthere'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.