-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
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); |
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. |
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! |
You can add support for converting null to empty String by creating a custom serializer for |
DeserializationFeature
for converting empty String ("") into null
on deserialization
Would like to actually implement this, but now not sure if this should go in |
Actually not quite sure about this one: there are ways to do the opposite (null to "empty"); and for some formats, inferring Will close for now: a new issue may be filed with new use case, ideas; can link to this one as background. |
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.
The text was updated successfully, but these errors were encountered: