-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
As explained on https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-3 we want to improve and extend the set of Scalar value accessors like asInt()
for Jackson 3.0.
As of 2.x, there are a few accessors for types like, say, int
:
intValue()
: returnint
if (and only if!) we got numeric node (JsonToken.VALUE_NUMBER_INT
orJsonToken.VALUE_NUMBER_FLOAT) -- but won't throw exception, returns default value (
0`) otherwise. Also: can lose accuracy for FP numbers with fractionsintValue(int defaultValue)
--int
if coercible,defaultValue
if notasInt()
-- adds coercions from some non-numeric types too
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
- Jackson 3.0 now has
- Should allow use of Java 8
Optional
, as that is useful forstream()
operations
This would lead to bigger set of methods, once again for int
:
intValue()
as before, return value ifJsonToken.VALUE_NUMBER_INT
, within Javaint
range AND has no fraction (if floating point value) -- but if not, throw exceptionintValue(int defaultValue)
asintValue()
except returnsdefaultValue
instead of exceptionintValueOpt()
similar tointValue()
but returnsOptionalInt
, either present (as perintValue()
) or absent (instead of exception)asInt()
(orasIntValue()
?) similar tointValue()
but allows coercion fromdouble
andString
, as well asnull
(and even Boolean?). But if not, throw exceptionasIntOpt()
likeasInt()
but returnsOptionalInt
, either present (as perasInt()
) or absent (instead of exception)
We will add/change similar methods for:
- "long":
longValue()
,longValue(long defaultValue)
,OptionalLong longValueOpt()
,asLong()
- "double":
doubleValue()
,doubleValue(double defaultValue)
,OptionalDouble doubleValueOpt()
,asDouble()
- "decimal" (for
BigDecimal
):decimalValue()
,decimalValue(BigDecimal defaultValue)
,Optional<BigDecimal> decimalValueOpt()
,asDecimal()
And we can consider additional accessors as well, but let's start with above.
NOTE:
- There are following partial sets for number access: need to be changed to throw on non-numbers and have range checks
bigIntegerValue()
-- samefloatValue()
shortValue()
-- samenumberValue()
-- same; needs to throw exception for non-numbers
NOTE:
This issue does NOT yet add/change JsonNode.asXxx()
but only (numeric) JsonNode.xxxValue()
methods (and variants) -- there's #5003 for asInt()
and such.