I have the following class, that is mapped by Jackson (simplified version):
public class POI {
@JsonProperty("name")
private String name;
}
In some cases the server returns "name": null
and I would like to then set name to empty Java String.
Is there any Jackson annotation or should I just check for the null inside my getter and return empty string if the property is null
?
A string refers to a character's sequence. Sometimes strings can be empty or NULL. The difference is that NULL is used to refer to nothing. However, an empty string is used to point to a unique string with zero length.
All replies. You can use XmlElement attribute and specify that value should be nullable. Then the empty element will be serialized with correct nil value.
You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.
With its default settings, Jackson serializes null-valued public fields. In other words, resulting JSON will include null fields. Here, the name field which is null is in the resulting JSON string.
Jackson 2.9 actually offers a new mechanism not yet mentioned: use of @JsonSetter
for properties, and its equivalent "Config Overrides" for types like String.class
.
Longer explanation included in
https://medium.com/@cowtowncoder/jackson-2-9-features-b2a19029e9ff
but gist is that you can either mark field (or setter) like so:
@JsonSetter(nulls=Nulls.AS_EMPTY) public String stringValue;
or configure mapper to do the same for all String
value properties:
mapper.configOverride(String.class)
.setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
both of which would convert incoming null
into empty value, which for Strings is "".
This also works for Collection
s and Map
s as expected.
Again, this answer is for the SO users who happen to stumble upon this thread.
While the accepted answer stands accepted and valid in all its sense - it did not help me in the case where the decision to set null
string values to empty
string came only after we made our services available to iOS
clients.
So, around 30-40 pojo's(increasing) and initializing them while instantiating the object in question or at the point of declaration was too much.
Here's how we did it.
public class CustomSerializerProvider extends DefaultSerializerProvider {
public CustomSerializerProvider() {
super();
}
public CustomSerializerProvider(CustomSerializerProvider provider, SerializationConfig config,
SerializerFactory jsf) {
super(provider, config, jsf);
}
@Override
public CustomSerializerProvider createInstance(SerializationConfig config, SerializerFactory jsf) {
return new CustomSerializerProvider(this, config, jsf);
}
@Override
public JsonSerializer<Object> findNullValueSerializer(BeanProperty property) throws JsonMappingException {
if (property.getType().getRawClass().equals(String.class))
return Serializers.EMPTY_STRING_SERIALIZER_INSTANCE;
else
return super.findNullValueSerializer(property);
}
}
And, the serializer
public class Serializers extends JsonSerializer<Object> {
public static final JsonSerializer<Object> EMPTY_STRING_SERIALIZER_INSTANCE = new EmptyStringSerializer();
public Serializers() {}
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
private static class EmptyStringSerializer extends JsonSerializer<Object> {
public EmptyStringSerializer() {}
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
}
}
And, then set the serializer in the ObjectMapper. (Jackson 2.7.4)
ObjectMapper nullMapper = new ObjectMapper();
nullMapper.setSerializerProvider(new CustomSerializerProvider());
Hoping, this will save someone some time.
A simple solution using no Jackson specialities: Write a Getter for name which returns an empty String instead of null as Jackson uses those to serialize.
public String getName() {
return name != null ? name : "";
}
Another way would be to write a custom deserializer. Look here: http://wiki.fasterxml.com/JacksonHowToCustomSerializers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With