views:

169

answers:

2

Hi

I am using Spring (with Jackson) and jQuery to pass a form as an object. My pojo includes nullable floats. However, when the fields are empty, Jackson throws an Exeption:

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Float from String value '': not a valid double value

What do I have to do in order to allow nulls (or empty values in the form field)?

Thanks Er

A: 

Your best bet is to check for empty string in the javascript code and pass null in that case.

Bozho
Thanks, that will be my solution for prototyping. Yet, I do not believe that the client side is the right place for that conversion.
+1  A: 

By default Jackson does indeed only consider explicit JSON null as null value. But if coercion from empty String to null was desired, it's easy to add feature requests. It sounds like a nice improvement actually -- this is how open source projects are often improved, based on user's asking for things they think should be there, ways things should work.

On short term you could also register custom deserializer (http://wiki.fasterxml.com/JacksonHowToCustomDeserializers tells something about it, although is not a guide) that accepts empty String and produces null.

StaxMan
Thanks, that is the solution I was looking for. Anyway, I understand how to create a custom serializer, but I don't know how to make it work with Spring. Any suggestion?
I don't know details, but for value type you might be able to use @JsonDeserialize(using=...) for value type (if this is possible). If not, you need to be able to access ObjectMapper.There's tiny documentlet at http://wiki.fasterxml.com/JacksonHowToCustomDeserializers explains registration. But that too assumes full control which is probably not true with Spring.FWIW, feature request for handling empty Strings is (http://jira.codehaus.org/browse/JACKSON-349), and it will be part of 1.6 at least.
StaxMan