Jackson: Deserializing Zero Strings as Empty Strings

I have the following class that is displayed by Jackson (simplified version):

public class POI {
    @JsonProperty("name")
    private String name;
}

In some cases, the server returns "name": null, and I would then set the name for an empty Java String.

Is there any kind of Jackson annotation or should I just check for null inside my recipient and return an empty string if the property null?

+4
source share
3 answers

You can either set it in the default constructor, or in the declaration:

public class POI {
    @JsonProperty("name")
    private String name; 

    public POI() {
        name = "";
    }
}

OR

public class POI {
    @JsonProperty("name")
    private String name = "";
} 
+3
source

, : Getter , , , .

public String getName() {
  return name != null ? name : "";
}

- . : http://wiki.fasterxml.com/JacksonHowToCustomSerializers

+6

, SO, .

, - , null empty , iOS .

Thus, about 30-40 pojo (increase) and their initialization when creating an instance of 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, 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 ObjectMapper. ( Jackson 2.7.4 )

ObjectMapper nullMapper = new ObjectMapper();
nullMapper.setSerializerProvider(new CustomSerializerProvider());

In the hope, this will save a little time.

+1
source

All Articles