Skip to content
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

Disable the Number to String automatic conversion #192

Closed
fabianpiau opened this issue May 19, 2015 · 3 comments
Closed

Disable the Number to String automatic conversion #192

fabianpiau opened this issue May 19, 2015 · 3 comments

Comments

@fabianpiau
Copy link

By default, Jackson is converting numbers to strings. In most cases, that's fine. But not in my situation unfortunately. Is there a way to prevent that?

For example:

JSON

{
"numberAsString": 123
}

JAVA

private String numberAsString;

It works just fine. But the thing is that I want this automatic boxing not enabled and got an exception instead. So the user is forced to use a String. I.e.

{
"numberAsString": "123"
}

I don't think such a feature exist, I look on http://wiki.fasterxml.com/JacksonFeaturesGenerator and a bit everywhere like http://stackoverflow.com/questions/7806316/jackson-json-converts-integers-into-strings

I tried to create a custom deserializer, but it seems that the value is already converted as a string when I got to the deserializer, and I don't have the possibility to determine if it was a string or an integer originally in the request.

Thanks

Fabian

@fabianpiau fabianpiau changed the title Disable the Number to String Disable the Number to String automatic conversion May 19, 2015
@cowtowncoder
Copy link
Member

Custom deserializer registered for java.lang.String should definitely work and let you prevent conversion. Deserializers will directly see content via JsonParser so they can detect underlying token type.

As to feature, that is a possibility. This feature request should actually go in:

https://github.com/FasterXML/jackson-databind/issues/

because it is related to binding of data from streaming JSON API (this project) into POJOs.
If you could re-create the request there that would be great; otherwise it may not get enough visibility.

@fabianpiau
Copy link
Author

Thanks for your answer, I have created the issue 796 in jackson-databind submodule.

I don't understand how to do it with the custom deserializer. Indeed, I see the content via JsonParser, but it is already seen as a String, well if I parse it as a int, I will see it at the int, but that will not help me. My point is to know what was the exact type in the JSON request. Is there any existing method that I am missing?

Perhaps you can reply on the new issue and close that one.

@cowtowncoder
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants