javascript Java Jackson:反序列化复杂的多态对象模型:JsonMappingException:意外的令牌(START_OBJECT),预期的VALUE_STRING

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15833979/
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-10-27 02:16:04  来源:igfitidea点击:

Java Hymanson: deserialize complex polymorphic object model: JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING

javajavascriptjsonexceptionHymanson

提问by Giovanni Bitliner

I have this tree of objects

我有这棵对象树

A

一个

B extends A

B 扩展 A

C extends B

C 扩展 B

D extends B

D 扩展 B

E extends C

E 扩展 C

F extends A and has one reference to A

F 扩展了 A 并且有一个对 A 的引用

A has the following annotation

A 有以下注释

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS,include=JsonTypeInfo.As.PROPERTY,property="@class")

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS,include=JsonTypeInfo.As.PROPERTY,property="@class")

If i try to deserialize a JSON array of objects that extends A, it throws the following error

如果我尝试反序列化扩展 A 的 JSON 对象数组,则会引发以下错误

org.codehaus.Hymanson.map.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: need JSON String that contains type id (for subtype of java.util.Collection)

org.codehaus.Hymanson.map.JsonMappingException:意外的令牌(START_OBJECT),预期的 VALUE_STRING:需要包含类型 id 的 JSON 字符串(用于 java.util.Collection 的子类型)

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:

json 字符串由集合的 toString() 方法生成,该集合参数为类型 A,其中 A 使用以下代码在 JSON 中序列化:

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;

The code to deserialize the json array (that is the set described above) is:

反序列化 json 数组(即上面描述的集合)的代码是:

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;

The json sample that it parses is like:

它解析的 json 示例如下:

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

Here the json string includes only some objects of my sample of java Set

这里的 json 字符串只包括我的 java Set 样本的一些对象

回答by Pascal Gélinas

I believe the problem is with the default typing. The start of your JSON is not generated as what Hymanson expect with default typing. The begining of the JSON should be:

我相信问题出在默认类型上。JSON 的开头不是按照 Hymanson 期望的默认类型生成的。JSON 的开头应该是:

["java.util.HashSet", [{

and the end should have an extra closing bracket }]].

并且最后应该有一个额外的结束括号}]]

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:

那是因为您使用toString()集合的方法生成了 JSON 。您应该改为使用ObjectMapper, 配置为默认类型,如下所示:

res = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(yourSet);