杰克逊反序列化...意外的令牌(END_OBJECT), [英] Jackson deserialization ... Unexpected token (END_OBJECT),
问题描述
我正在尝试使用Jackson将JSON数组反序列化为Java Collection。这是我昨晚问到的这个问题的答案的动机我可以实例化一个超类,并根据提供的参数实例化一个特定的子类。
I am trying to deserialize a JSON array into a Java Collection using Jackson. This motivated by the answers to this question I asked last night Can I instantiate a superclass and have a particular subclass be instantiated based on the parameters supplied.
我得到的错误是(添加换行符以提高可读性):
The error I am gettings is (line breaks added for readability):
org.codehaus.jackson.map.JsonMappingException:
Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type'
that is to contain type id (for class sempedia.model.query.QueryValue)
at [Source: java.io.StringReader@325aef; line: 1, column: 175]
(through reference chain: sempedia.model.query.QueryProperty["values"])
我的情况非常复杂。我的数组包含的对象本身包含一个数组值。这个数组又包含也是对象但不一定相同的值(因此是多态性)。
My situation is quite complicated. My array contains objects which themselves contain a value which is an array. This array in turn contains values which are also objects but are not necessarily the same (thus polymorphism).
这是一个示例JSON字符串:
Here is a sample JSON string:
[
{
"id":"74562",
"uri":"http://dbpedia.org/ontology/family",
"name":"family",
"values":[
{
"id":"74563",
"uri":"http://dbpedia.org/resource/Orycteropodidae",
"name":"Orycteropodidae"
}
],
"selected":false
},
{
"id":"78564",
"uri":"http://dbpedia.org/ontology/someNumber",
"name":"someNumber",
"values":[
{
"lower":"45",
"upper":"975",
}
],
"selected":true
}
]
我想使用这个(下面)代码或类似的东西来获取一个<$ c $实例的对象c>集合< QueryProperty> 我称之为 queryProperties
I would like to use this (below) code or something similar to get an object which is an instance of Collection<QueryProperty>
which I have called queryProperties
ObjectMapper mapper = new ObjectMapper();
Collection<QueryProperty> queryProperties =
queryProperties = mapper.readValue(query,
new TypeReference<Collection<QueryProperty>>(){});
我的反序列化类(它们有我不打印的公共getter / setter)如下所示:
My classes for deserialization (they have public getters/setters which I am not printing) are listed below:
public class QueryProperty {
int id;
String uri;
String name;
Set<QueryValue> values;
String selected;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@Type(value = ResourceQueryValue.class),
@Type(value = NumericQueryValue.class)
})
public abstract class QueryValue {
String type;
}
ResourceQueryValue
public class ResourceQueryValue extends QueryValue{
int id;
String uri;
String name;
}
NumericQueryValue
相同JSON不包含此类型的对象。
NumericQueryValue
the same JSON doesn't include an object of this type.
public class NumericQueryValue extends QueryValue{
double lower;
double upper;
}
堆栈跟踪的初始部分:
org.codehaus.jackson.map.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id (for class sempedia.model.query.QueryValue)
at [Source: java.io.StringReader@325aef; line: 1, column: 175] (through reference chain: sempedia.model.query.QueryProperty["values"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.wrongTokenException(StdDeserializationContext.java:240)
at org.codehaus.jackson.map.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:86)
at org.codehaus.jackson.map.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:89)
推荐答案
正如经常发生的那样,写出问题有助于您了解解决方案。所以我需要做两件事。
As often happens, writing out a question helps you see the solution. So I need to do two things.
首先我需要将类型信息添加到JSON中 - 这不是我真正想做的,但我想你需要在某处提供该信息。
Firstly I need to add the type information into the JSON - which is not what I really wanted to do, but I guess you need to provide that information somewhere.
然后我需要在QueryValue上编辑注释:
And then I need to edit the annotation on QueryValue to be:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@Type(value = ResourceQueryValue.class, name = "ResourceQueryValue"),
@Type(value = NumericQueryValue.class, name= "NumericQueryValue")
})
这篇关于杰克逊反序列化...意外的令牌(END_OBJECT),的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!