问题描述
我有这个对象树
A
B延伸A
C延伸B
D延伸B
E延伸C
F延伸A并且有一个参考A
F extends A and has one reference to A
A具有以下注释
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,include = JsonTypeInfo.As.PROPERTY,property =@ class)
如果我尝试反序列化扩展A的JSON对象数组,则会抛出以下错误
If i try to deserialize a JSON array of objects that extends A, it throws the following error
json字符串由set的toString()方法生成,该set参数化为A类,其中A在JSON中使用以下代码序列化:
The json string is generated by toString() method of a set and the set is parametric to type A where A is serialized in JSON with the following code:
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS);
String res="";
try {
res = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
反序列化json数组的代码(即上述集合)是:
The code to deserialize the json array (that is the set described above) is:
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS);
Collection<T> results=null;
try {
results = mapper.readValue(json, TypeFactory.defaultInstance().constructParametricType(Collection.class, clazz ) );
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return results;
它解析的json示例如下:
The json sample that it parses is like:
"[{
"@class" : "pack1.pack2.MyClass",
"id" : null,
"f1" : "",
"f2" : 0.9933817827,
"f3" : 6.883261E-4,
"f4" : 0.001375699,
"f5" : {
"@class" : "pack1.pack2.MyClass2",
"id" : null,
"f1" : "",
"f2" : 0.0,
"f3" : 0.0,
"f4" : 0.0,
"f5" : [ "java.util.HashSet", [ 0 ] ],
"f6" : [ "java.util.HashSet", [ 2 ] ],
"f7" : [ "java.util.ArrayList", [ "scelta", "brani", "buona" ] ],
"f8" : [ null, "NOM", null ],
"f9" : false
},
"f10" : [ "java.util.HashMap", {
"2" : "ADJ"
} ],
"f11" : [ "java.util.HashSet", [ 0 ] ],
"f12" : [ "java.util.HashSet", [ 2 ] ],
"f13" : [ "java.util.ArrayList", [ "scelta", "brani", "buona" ] ],
"featureIndicator" : false
}]"
这里json字符串只包含我的java集样本的一些对象
Here the json string includes only some objects of my sample of java Set
推荐答案
我认为问题在于默认输入。 JSON的开头并不像杰克逊所期望的默认输入那样生成。 JSON的开头应该是:
I believe the problem is with the default typing. The start of your JSON is not generated as what Jackson expect with default typing. The begining of the JSON should be:
["java.util.HashSet", [{
并且结尾应该有一个额外的结束括号}]]
。
and the end should have an extra closing bracket }]]
.
这是因为你使用你的集合的 toString()
方法生成JSON。您应该使用 ObjectMapper
,使用默认类型进行配置,如下所示:
That's because you generate the JSON using the toString()
method of your set. You should instead use the ObjectMapper
, that's configured with default typing, like so:
res = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(yourSet);
这篇关于Java Jackson:反序列化复杂的多态对象模型:JsonMappingException:意外的令牌(START_OBJECT),预期的VALUE_STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!