我正在尝试使用 Jackson 的 XmlMapper 来反序列化一些包含未包装列表的简单 xml 文件。
我的代码:
package zm.study.xmlserialize.jackson;
import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper ;
public class JacksonListTest {
public static class A {
public String c;
@JacksonXmlElementWrapper(useWrapping=false)
public List<String> as;
}
@Test
public void deserializeTest() throws Exception
{
XmlMapper mapper = new XmlMapper();
String xml = "<A><c>c</c><as>a1</as><as>a2</as></A>";
//mapper.readValue(xml, A.class);
mapper.convertValue(mapper.readTree(xml), A.class);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当列表不是 class/xml 中的第一个时,库会引发异常。
当我从 xml 和类中删除“c”元素时,异常消失了。如果我使用 readValue 而不是 convertValue,异常也会消失,但我需要 convertMethod 才能工作。
例外是:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token
at [Source: (StringReader); line: 1, column: 18] (through reference chain: zm.study.xmlserialize.jackson.JacksonListTest$A["as"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
Run Code Online (Sandbox Code Playgroud)
...
我不确定是否有可能以这种方式做到这一点。readTree方法返回扩展的对象JsonNode,在这种情况下它将是ObjectNode. ObjectNode不接受两个同名属性,最后反序列化后表示:
{"c":"c","as":"a2"}
Run Code Online (Sandbox Code Playgroud)
之后,您想将此节点转换为A POJO类。Listexpects START_ARRAYtoken not 的默认反序列化器String。您可以通过实现扩展StdConverter<String, List>但列表将被修剪为一个元素的自定义转换器来使其工作。在这种情况下,我认为,你必须使用readValue,因为您需要指示方法Jackson是as元素展开阵列。
编辑
在您发表评论后,我意识到我们可以欺骗XmlMapper我们想要的任何东西。很明显,Jackson用于JsonNodeDeserializer反序列化JsonNode-s。所以,我们需要做的就是找到一个可以注入代码的地方。幸运的是,有一种方法_handleDuplicateField可以处理我们的情况。默认情况下,如果FAIL_ON_READING_DUP_TREE_KEY启用标志,它会抛出异常:
protected void _handleDuplicateField(JsonParser p, DeserializationContext ctxt,
JsonNodeFactory nodeFactory,
String fieldName, ObjectNode objectNode,
JsonNode oldValue, JsonNode newValue)
throws JsonProcessingException
{
// [databind#237]: Report an error if asked to do so:
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)) {
ctxt.reportInputMismatch(JsonNode.class,
"Duplicate field '%s' for ObjectNode: not allowed when FAIL_ON_READING_DUP_TREE_KEY enabled",
fieldName);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,让我们使用这个事实并扩展这个类:
class MergeDuplicateFieldsJsonNodeDeserializer extends JsonNodeDeserializer {
@Override
protected void _handleDuplicateField(JsonParser p, DeserializationContext ctxt,
JsonNodeFactory nodeFactory, String fieldName, ObjectNode objectNode,
JsonNode oldValue, JsonNode newValue) throws JsonProcessingException {
super._handleDuplicateField(p, ctxt, nodeFactory, fieldName, objectNode, oldValue, newValue);
ArrayNode array;
if (oldValue instanceof ArrayNode) {
// Merge 3-rd, 4-th, ..., n-th element to already existed array
array = (ArrayNode) oldValue;
array.add(newValue);
} else {
// Merge first two elements
array = nodeFactory.arrayNode();
array.add(oldValue);
array.add(newValue);
}
objectNode.set(fieldName, array);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我们需要注册这个反序列化器。整个测试可能如下所示:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import java.util.Arrays;
import java.util.List;
public class XmlMapperApp {
public static void main(String[] args) throws Exception {
A a = new A();
a.c = "String";
a.as = Arrays.asList("1", "2", "tom", "Nick");
SimpleModule mergeDuplicatesModule = new SimpleModule("Merge duplicated fields in array");
mergeDuplicatesModule.addDeserializer(JsonNode.class, new MergeDuplicateFieldsJsonNodeDeserializer());
XmlMapper mapper = new XmlMapper();
mapper.registerModule(mergeDuplicatesModule);
String xml = mapper.writeValueAsString(a);
System.out.println(xml);
System.out.println(mapper.readTree(xml));
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印:
<A><c>String</c><as>1</as><as>2</as><as>tom</as><as>Nick</as></A>
{"c":"String","as":["1","2","tom","Nick"]}
Run Code Online (Sandbox Code Playgroud)