Java Jackson:将空字符串反序列化为空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20654810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson: deserializing null Strings as empty Strings
提问by syntagma
I have the following class, that is mapped by Hymanson (simplified version):
我有以下类,由 Hymanson 映射(简化版):
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.
在某些情况下,服务器返回,"name": null
然后我想将名称设置为空 Java 字符串。
Is there any Hymanson annotation or should I just check for the null inside my getter and return empty string if the property is null
?
是否有任何 Hymanson 注释,或者我应该检查 getter 中的 null 并返回空字符串(如果属性是 )null
?
采纳答案by reindeer
You can either set it in the default constructor, or on declaration:
您可以在默认构造函数中或在声明中设置它:
public class POI {
@JsonProperty("name")
private String name;
public POI() {
name = "";
}
}
OR
或者
public class POI {
@JsonProperty("name")
private String name = "";
}
回答by spa
A simple solution using no Hymanson specialities: Write a Getter for name which returns an empty String instead of null as Hymanson uses those to serialize.
一个不使用 Hymanson 专长的简单解决方案:为 name 编写一个 Getter,它返回一个空字符串而不是 null,因为 Hymanson 使用它们进行序列化。
public String getName() {
return name != null ? name : "";
}
Another way would be to write a custom deserializer. Look here: http://wiki.fasterxml.com/HymansonHowToCustomSerializers
另一种方法是编写自定义解串器。看这里:http: //wiki.fasterxml.com/HymansonHowToCustomSerializers
回答by Tirath
Again, this answer is for the SO userswho happen to stumble upon this thread.
同样,此答案适用于偶然发现此线程的SO 用户。
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.
虽然接受的答案在所有意义上都被接受且有效 -在我们将服务提供给客户之后才决定将null
字符串值设置为字符串的情况下,它对我没有帮助。empty
iOS
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.
因此,大约 30-40 个 pojo(增加)并在实例化有问题的对象时或在声明点初始化它们太多了。
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. (Hymanson 2.7.4)
然后,在 ObjectMapper 中设置序列化器。(Hyman逊 2.7.4)
ObjectMapper nullMapper = new ObjectMapper();
nullMapper.setSerializerProvider(new CustomSerializerProvider());
Hoping, this will save someone some time.
希望,这会节省一些时间。