You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of 2.x, there are a few accessors for type like, say, int:
intValue(): return int if (and only if!) we got numeric integer node (JsonToken.VALUE_NUMBER_INT) -- but won't throw exception, returns default value (0) otherwise
asInt(): same as asInt(0)
asInt(int defaultValue): return int value from JsonToken.VALUE_NUMBER_INT (if within value range) OR if coercible (from double or 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 int:
intValue() as before, return value if JsonToken.VALUE_NUMBER_INT, within Java int range AND has no fraction (if floating point value) -- but if not, throw exception
intValue(int defaultValue) as intValue() except returns defaultValue instead of exception
intValueOpt() similar to intValue() but returns OptionalInt, either present (as per intValue()) or absent (instead of exception)
asInt() (or asIntValue()?) similar to intValue() but allows coercion from double and String, as well as null (and even Boolean?). But if not, throw exception
asIntOpt() like asInt() but returns OptionalInt, either present (as per asInt()) or absent (instead of exception)
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 type like, say,
int
:intValue()
: returnint
if (and only if!) we got numeric integer node (JsonToken.VALUE_NUMBER_INT
) -- but won't throw exception, returns default value (0
) otherwiseasInt()
: same asasInt(0)
asInt(int defaultValue
): returnint
value fromJsonToken.VALUE_NUMBER_INT
(if within value range) OR if coercible (fromdouble
orString
); 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:
JsonNodeException
to useOptional
, as that is useful forstream()
operationsThis 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:
longValue()
,longValue(long defaultValue)
,OptionalLong longValueOpt()
,asLong()
doubleValue()
,doubleValue(double defaultValue)
,OptionalDouble doubleValueOpt()
,asDouble()
booleanValue()
,booleanValue(boolean defaultValue)
,Optional<Boolean> booleanValueOpt()
,asBoolean()
StringNode
(if matching name),NullNode
,MissingNode
stringValue()
,stringValue(String defaultValue)
,Optional<String> stringValueOpt()
,asString()
ContainerNode
(Array, Object) or from trueBinaryNode
And we can consider additional accessors as well, but let's start with above.
The text was updated successfully, but these errors were encountered: