![](/img/trans.png)
[英]how to deserialize / serialize byte array using Jackson and wrapper object
[英]How to configure jackson to not to serialize byte array?
我有一个类似JSON的对象,它也具有一些二进制值。 我不想将binary( byte[]
)数据序列化。
我试图为byte[]
添加自定义序列化程序。 但这没有解决。
尝试1:
public class ByteArraySerialiser extends SerializerBase<Byte[]> {
protected ByteArraySerialiser() {
super(Byte[].class, false);
}
@Override
public void serialize(Byte[] arg0, JsonGenerator arg1,
SerializerProvider arg2) throws IOException,
JsonGenerationException {
arg1.writeString("");
}
}
尝试2:
public class ByteArraySerialiser extends SerializerBase<Byte> {
protected ByteArraySerialiser() {
super(Byte.class, false);
}
@Override
public void serialize(Byte arg0, JsonGenerator arg1,
SerializerProvider arg2) throws IOException,
JsonGenerationException {
arg1.writeString("");
}
}
但是,两者都无法覆盖默认的序列化程序。
我不能使用注释,因为它是Map<Object, Object>
。
谢谢。
您是否尝试过在getter或字段本身上使用JsonIgnore批注? 您也可以使用MixIn来做到这一点。 示例(取自oVirt开源):
public abstract class JsonNGuidMixIn extends NGuid {
/**
* Tells Jackson that the constructor with the {@link String} argument is to be used to deserialize the entity,
* using the "uuid" property as the argument.
*
* @param candidate
*/
@JsonCreator
public JsonNGuidMixIn(@JsonProperty("uuid") String candidate) {
super(candidate);
}
/**
* Ignore this method since Jackson will try to recursively dereference it and fail to serialize.
*/
@JsonIgnore
@Override
public abstract Guid getValue();
}
用法在JSonObjectSerializer上(在此处复制粘贴一部分)
@Override
public String serialize(Serializable payload) throws SerializationExeption {
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(NGuid.class, JsonNGuidMixIn.class);
mapper.getSerializationConfig().addMixInAnnotations(Guid.class, JsonNGuidMixIn.class);
mapper.configure(Feature.INDENT_OUTPUT, true);
mapper.enableDefaultTyping();
return writeJsonAsString(payload, mapper);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.