-
Notifications
You must be signed in to change notification settings - Fork 154
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
Always parse integers as longs #105
Comments
Thanks for the feedback. Are you talking about parsing JSON into integers, or parsing Java integers using Jackson's long functionality? Perhaps you can show a snippet describing the issue you're seeing? |
What I mean is (class (cheshire.core/parse-string "1"))
;> java.lang.Integer It's a surprising behaviour (at least for me). Everywhere else in Clojure Not a bug for sure, but an interesting default choice. What is the rationale for choosing Integer instead of Long? |
I think long would be better if there aren't any performance issues. I assume large enough integers are already returned as longs? |
Yes, they are:
I believe this is something coming from Jackson, see: JsonToken/VALUE_NUMBER_INT (.getNumberValue jp) ( |
I run into this from time to time as well. It looks like a related jackson package has a way to force longs: http://stackoverflow.com/questions/7316689/forcing-jackson-to-deserialize-to-specific-primitive-type FasterXML/jackson-databind#504 I don't think Cheshire is using jackson-databind though. I'm not really up to speed on what the difference between jackson-core and jackson-databind is even. It doesn't look like the core jackson JSON parser has any such option unfortunately. This is probably "bad practice", but you can hijack
I'm not sure if this is actually better than littering code with explicit (long) casts. |
For context, this is where I normally get bit:
I'm not calling Because in Clojure:
And:
|
Cheshire doesn't use jackson-databind, which is a tool for serializing plain Java objects into JSON and back, it just uses the core. I don't currently know of a way to do this with Jackson, and I'm not sure we want to coerce every number type to long necessarily.. |
It might be helpful to have a way to override how each JSON type is parsed. The Possibly one could pass in a map with the |
The |
In the code of |
You're right, I was incorrectly thinking the factory options instead of the Cheshire ones. In that case, as long as it doesn't affect performance detrimentally I think it'd be reasonable to add an option to do that. |
First of all, thanks for the library. I've used it extensively over many years and it always worked smoothly.
Except for today, where I was surprised to find that the default behaviour of cheshire is to convert numbers to java.lang.Integer and that blew another java interop method that was expecting a java.lang.Long.
Is there a way to set a flag so cheshire always parses numbers as Long (which I think is the less surprising behaviour given that Clojure defaults to Long)?
The text was updated successfully, but these errors were encountered: