json Jackson 反序列化......意外令牌(END_OBJECT),

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6435062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:53:54  来源:igfitidea点击:

Hymanson deserialization ... Unexpected token (END_OBJECT),

jsoncollectionspolymorphismdeserializationHymanson

提问by Ankur

I am trying to deserialize a JSON array into a Java Collection using Hymanson. 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.

我正在尝试使用 Hymanson 将 JSON 数组反序列化为 Java 集合。这是由我昨晚问的这个问题的答案所激发的,我可以实例化一个超类并根据提供的参数实例化一个特定的子类

The error I am gettings is (line breaks added for readability):

我得到的错误是(为了可读性添加了换行符):

org.codehaus.Hymanson.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).

我的情况相当复杂。我的数组包含对象,这些对象本身包含一个数组值。该数组又包含也是对象但不一定相同的值(因此是多态性)。

Here is a sample JSON string:

这是一个示例 JSON 字符串:

[
    {
      "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
    }
]

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

我想使用这个(下面)代码或类似的东西来获取一个对象,它是Collection<QueryProperty>我调用的一个实例queryProperties

ObjectMapper mapper = new ObjectMapper();  
Collection<QueryProperty> queryProperties = 
   queryProperties = mapper.readValue(query, 
      new TypeReference<Collection<QueryProperty>>(){});

My classes for deserialization (they have public getters/setters which I am not printing) are listed below:

下面列出了我的反序列化类(它们有我没有打印的公共 getter/setter):

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

ResourceQueryValue

public class ResourceQueryValue extends QueryValue{
    int id;
    String uri;
    String name;
}

NumericQueryValuethe same JSON doesn't include an object of this type.

NumericQueryValue相同的 JSON 不包含这种类型的对象。

public class NumericQueryValue extends QueryValue{
    double lower;
    double upper;
}

Initial part of Stack trace:

堆栈跟踪的初始部分:

org.codehaus.Hymanson.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.Hymanson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.Hymanson.map.deser.StdDeserializationContext.wrongTokenException(StdDeserializationContext.java:240)
    at org.codehaus.Hymanson.map.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:86)
    at org.codehaus.Hymanson.map.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:89)

回答by Ankur

As often happens, writing out a question helps you see the solution. So I need to do two things.

正如经常发生的那样,写出一个问题可以帮助您找到解决方案。所以我需要做两件事。

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.

首先,我需要将类型信息添加到 JSON 中 - 这不是我真正想做的,但我想您需要在某处提供该信息。

And then I need to edit the annotation on QueryValue to be:

然后我需要将 QueryValue 上的注释编辑为:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")  
@JsonSubTypes({  
    @Type(value = ResourceQueryValue.class, name = "ResourceQueryValue"),  
    @Type(value = NumericQueryValue.class, name= "NumericQueryValue")
    })