Open JinseongHwang opened 5 days ago
There's no such method yet? Then makes sense wrt usage. But I'm a bit concerned about the naming though.
'int' can never be nullable in Java, only Kotlin. 'Integer' can be nullable in Java. We might want to check Scala as well.
java.lang.Integer is the best return type - works for Java, Kotlin and Scala.
I think the changes could end up being very large because we would probably need a canConvertToNullableInt method too (as well as for other number types and boolean). canConvertToNullableInt would return true if the underlying number data was a valid int but also true if the data is null.
Now that you mention it PJ, if we were to implement, is it possible that we might end up being quite large, for Float or Double and such🤔?
The rationale behind it could be "why just only integer? Why not double or float?"
@JooHyukKim I think Long, Double, Boolean, etc. should be implemented the same way. 👍 I wrote in the 'Describe the solution you'd like' part of the body above.
I wrote in the 'Describe the solution you'd like' part of the body above.
Yup, I read that. Thank you 🙏🏼. The feature itself seems reasonable.
I think Long, Double, Boolean, etc. should be implemented the same way. 👍
I guess I was trying to reach this point of view. I agree that we most probably would want to have all wrapper types implementations be provided, in same version. Which immedately leads to the question of which version? Because like @pjfanning said above this could become quite large, eating up a whole new minor version.
Having said that, honestly, we could consider this set of feature here as a way to attract users to adopt Jackson 3.0 more if we choose 3.0. Let's hear what @cowtowncoder has in mind wrt version & milestones, and also timing.
Ok, first things first; there are/were some plans to further extend methods:
https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-3
now on "nullable"... that's one possibility, although use of Optional
would be another one (we can use that with Java 8 baseline).
Naming-wise for Integer
we could actually add asInteger()
, (instead of asInt()
), but that couldn't be done with Long
or Double
.
But also keep in mind there's intValue()
vs asInt()
. Former only coerces numeric values; latter will also support converting JSON Strings that can be parsed as int
s.
So we need to be clear on exact expected semantics and naming.
I guess what I am saying is that:
null
or Optional
) to indicated node is not of expected typeasNullableInt[Eger]
as choice (naming, or semantics)It might help if @JinseongHwang could extend a little bit on kind of actual usage.
Thank you for your feedback 👍 I'll think about it and share it with you asap
Is your feature request related to a problem? Please describe.
Hi there,
It doesn't seem to provide the ability to convert a
JsonNode
to a nullable int(Integer
). In JsonNode.java,asInt()
returns anint
type and if there is no value, it returns 0 as the default value.Describe the solution you'd like
I think it would be nice to have a method like
asNullableInt()
that returns anInteger
that is a nullable wrapper class. The same goes forLong
,Double
, andBoolean
.If you agree with me, I'd like to contribute to this project.
Usage example
I'm using it like this. (However, this is just an example, and it would be possible to pass the nullable option as a parameter.)
Additional context
No response