使用Jackson的JSON反序列化

使用Jackson的JSON反序列化

本文介绍了使用Jackson的JSON反序列化:没有为类型找到合适的构造函数 - 提供默认构造函数或注释构造函数是不可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jackson ObjectMapper将对象层次结构序列化为json String。之后我想将对象反序列化。我得到了例外如下。

I used Jackson ObjectMapper to serialise object hierarchy to json String. After that I wanted to deserialize the object back. I got exception as below.

重要的是 APINewDealArrangementImpl 类层次结构超出了范围我的更改 - 它是外部库的一部分。在这种情况下我无法实现默认构造函数,也无法使用 @JsonCreator annotion

The important thing is that APINewDealArrangementImpl class hierarchy is out of the scope of my changes - it is part of external library. In this case I'm not able to implement default constructor nor use @JsonCreator annotion.

如何避免找不到合适的构造函数异常?是否可以使用Jackson API中的一些自定义 TypeResolverBuilder 实现或其他功能来解决此问题?感谢您的帮助。

How can I avoid "No suitable constructor found" Exception? Is it possible to conquer this problem using some custom TypeResolverBuilder implementation or other functionalities in Jackson API? Thanks for help.

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.tzero.api.transactions.TransactionState]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: java.io.StringReader@57ac3379; line: 4, column: 5] (through reference chain: com.tzero.api.java.transactions.APINewDealArrangementImpl["state"])
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
    at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)


推荐答案

您可以使用mixin在不同的类上提供注释。这有点奇怪,但非常有用。

You can use a "mixin" to provide the annotations on a different class. Which is a bit odd, but very useful.

@Test
public void mixin_to_specify_creator() throws Exception {
    TargetData target = mapper.addMixIn(TargetData.class, TargetDataMixin.class).reader(TargetData.class)
            .<TargetData> readValue("{\"name\":\"the name\", \"description\":\"the description\"}");
    assertThat(target.name, equalTo("the name"));
    assertThat(target.description, equalTo("the description"));
}

public static class TargetData {
    private final String name;
    private final String description;

    public TargetData(String name, String description) {
        this.name = name;
        this.description = description;
    }
}

public static abstract class TargetDataMixin {
    @JsonCreator
    public TargetDataMixin(@JsonProperty("name") String name, @JsonProperty("description") String description) {
    }
}

所以这里有在Tar​​getData上没有Jackson注释,注释来自TargetDataMixin(并且完全忽略了TargetDataMixin的实现)

So here there are no Jackson annotations on TargetData, the annotations are taken from TargetDataMixin (and the implementation of TargetDataMixin is totally ignored)

这篇关于使用Jackson的JSON反序列化:没有为类型找到合适的构造函数 - 提供默认构造函数或注释构造函数是不可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:34