3
注:杰克逊2.1.x.如何将JSON null反序列化为NullNode而不是Java null?
问题很简单,但到目前为止我找不到解决方案。我浏览了现有的文档等,无法找到答案。
的基类是这样的:
@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "op")
@JsonSubTypes({
@Type(name = "add", value = AddOperation.class),
@Type(name = "copy", value = CopyOperation.class),
@Type(name = "move", value = MoveOperation.class),
@Type(name = "remove", value = RemoveOperation.class),
@Type(name = "replace", value = ReplaceOperation.class),
@Type(name = "test", value = TestOperation.class)
})
public abstract class JsonPatchOperation
{
/*
* Note: no need for a custom deserializer, Jackson will try and find a
* constructor with a single string argument and use it
*/
protected final JsonPointer path;
protected JsonPatchOperation(final JsonPointer path)
{
this.path = path;
}
public abstract JsonNode apply(final JsonNode node)
throws JsonPatchException;
@Override
public String toString()
{
return "path = \"" + path + '"';
}
}
而且有问题的阶级是这样的:
public abstract class PathValueOperation
extends JsonPatchOperation
{
protected final JsonNode value;
protected PathValueOperation(final JsonPointer path, final JsonNode value)
{
super(path);
this.value = value.deepCopy();
}
@Override
public String toString()
{
return super.toString() + ", value = " + value;
}
}
当我尝试反序列化:
{ "op": "add", "path": "/x", "value": null }
我想空值将被反序列化为NullNode
,而不是Java null。到目前为止我还找不到一种方法。
你是如何实现这一目标的?
(注:具体类的所有构造函数是@JsonCreator
s的适当@JsonProperty
注解 - 他们没有问题的工作,我唯一的问题是JSON空处理)
对于什么是值得的,这可能是一个错误,在HTTPS现在报告: //github.com/FasterXML/jackson-databind/issues/186 – StaxMan 2013-03-08 20:42:32