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

Add DeserializationFeature for converting empty String ("") into null on deserialization #768

Closed
karayv opened this issue Apr 23, 2015 · 6 comments

Comments

@karayv
Copy link

karayv commented Apr 23, 2015

Hi Jackson community!

I often here from our front-end guys, that it's required additional coding to pass null value if user does not specify value for a specific field on HTML form (especially using AngularJS). Empty string passed instead.

So it would be nice to have a feature that if enabled would convert all empty string to null references on deserialisation. Disabled by default.

@karayv
Copy link
Author

karayv commented Apr 23, 2015

Although it can be achieved with custom deserializer...

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;

public class EmptyStringDeserializer extends JsonDeserializer<String> {

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {

        if (jp.getCurrentToken() == JsonToken.VALUE_STRING && "".equals(jp.getText())) {
            return null;
        }

        return StringDeserializer.instance.deserialize(jp, ctxt);
    }

}

Registering module:

        ObjectMapper mapper = new ObjectMapper();

        SimpleModule emptyStringToNullModule = new SimpleModule("EmptyStringToNullModule", Version.unknownVersion())
.addDeserializer(String.class, new EmptyStringDeserializer());
        mapper.registerModule(emptyStringToNullModule);

@cowtowncoder
Copy link
Member

Sounds like potentially useful feature, and could be added in 2.6.0.

If implemented, need to make sure Afterburner module will also work, since it will try to "inline" handling of String deserialization.

@karayv
Copy link
Author

karayv commented Apr 23, 2015

Thanks cowtowncoder,

Spoke again today with our FE guys - it appears that it will be a trouble for them to pass empty string and than get back null. So this might be not so useful (specifically working with AngularJS) unless we would have something similar on serialisation as well, like null to empty string conversion. But this might produce traffic overhead in some situations. Any opinions?

Thank you!

@cowtowncoder
Copy link
Member

You can add support for converting null to empty String by creating a custom serializer for String, defining getNullValue() to return empty String and registering that via module.
So that should be doable without additional features.

@cowtowncoder cowtowncoder changed the title Add WRITE_EMPTY_STRINGS_AS_NULL deserialisation feature. Add DeserializationFeature for converting empty String ("") into null on deserialization Mar 30, 2017
@cowtowncoder
Copy link
Member

Would like to actually implement this, but now not sure if this should go in DeserializationFeature, since we have low limit of max number, and it seems we have couple of larger groups of features.
So need to think of where and how this should be applied.

@cowtowncoder
Copy link
Member

Actually not quite sure about this one: there are ways to do the opposite (null to "empty"); and for some formats, inferring nulls can be valuable, but generally relies on specific mechanism (with XML, xsi:nil, for example). So not quite sure what is left.

Will close for now: a new issue may be filed with new use case, ideas; can link to this one as background.

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