-
-
Notifications
You must be signed in to change notification settings - Fork 814
JsonParser#getNumberType()
throws JsonParseException
when the current token is non-numeric instead of returning null
#1433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Changing code behaviour tends to break stuff for other users. I don't think there is a good reason to change the behaviour here. It is perfectly justifiable to throw an exception in getNumberType if the value is not a number. Even if we did change the behaviour, you would have to wait for a non patch release and this is likely to be many months away. Are you willing to wait multiple months, maybe 6 for the 2.20.0 release? The Parser API has a |
You call also try getText which I think will return the number as a string regardless of whether it appears as a number or as a string in the input doc. You can then parse the string. If you are trying to get Jackson to parse hexadecimal numbers for you, I am not at all sure if Jackson has this built-in. It is not part of the JSON spec. |
I've already found a different method, but I figured that I'd report this so that the behaviour can be documented. I agree that updating the documentation to match the current behaviour is better for backwards compatibility (Hyrum's law and all that). |
Thanks @CrazySqueak - I missed the bit about the javadoc. It does indeed say that the method should return null. So maybe this is a bug. There may be a justification to change the behaviour to match the documented behaviour but still this is not likely to appear until 2.20.0. |
@cowtowncoder I had a look at ParserBase.getNumberType and it looks like to make that method match its Javadoc and return nulls for non-number types that we would have to do something like write a new method to replace _parseNumericValue for this use case. We could copy/paste _parseNumericValue and make the copy not throw an exception for certain token types like VALUE_STRING. It might be easier just to change the Javadoc to remove the bit about returning nulls for non-number types. |
I'll need to dig in to this bit more later tonight, but my first instinct is to agree with you and @CrazySqueak that we'll probably better change documentation -- esp. for 2.x. For 3.x we could change behavior too, but not 100% sure which behavior I prefer TBH. |
Will change Javadocs and also add testing; created #1434 for follow-up work for 3.0 to actually change behavior. |
JsonParser#getNumberType()
throws JsonParseException
when the current token is non-numeric instead of returning null
Context
I wrote a deserializer for colours which accepts them both as a raw integer (0xRRGGBB) or as a string ("name" or "#RRGGBB").
I assumed that, as mentioned in the javadoc, getNumberType would return a non-null number constant if the current token is a number, and null if it were a string or something else, thus making it suitable for discerning between the two options.
Expected Behaviour
JsonParser#getNumberType
returns null for a non-numeric token (in this case, of typeVALUE_STRING
), as stated by the documentation.Actual Behaviour
It throws an exception:
com.fasterxml.jackson.core.JsonParseException: Current token (VALUE_STRING) not numeric, can not use numeric value accessors.
From what I can see, this is because
ParserBase#getNumberType()
calls_parseNumericValue
to determine what number it's looking at, which always expects the current token to be numeric and throws an exception if it isn't.Full Traceback
Version
The text was updated successfully, but these errors were encountered: