无法反序列化一组多态对象(Jackson 2.1.2)

时间:2013-02-13 23:17:06

标签: json serialization jackson

我正在尝试反序列化以下内容:

{
 "type":"GIQuestionnaire",
 "id":6051,   
 "questions":[
      {
        "type":"IncludeExcludeQuestion",
        "id":24057,
        "answers":[
            {
              "type":"IncludeExcludeAnswer",
               "id":101497
            },
            {
              "type":"IncludeExcludeAnswer",
               "id":101496
            }
         ]
      }
   ]
}

导致此错误:

java.lang.AssertionError: Can not handle managed/back reference 'questionnaire-questions': value deserializer is of type ContainerDeserializerBase, but content type is not handled by a BeanDeserializer  (instead it's of type com.foo.questionnaire.json.QuestionDeserializer)

Junit测试:

@Test
public void testDeserializeQuestionnaire() {
    ObjectMapper mapper = new ObjectMapper();
    GIQuestionnaire q = manager.createGIQuestionnaire(…);
    try
    {
        String json = mapper.writeValueAsString(q);
        assertTrue(q.equals(mapper.readValue(json, GIQuestionnaire.class)));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}

调查问卷包含Question对象的Set(实现为TreeSet)。问题包含答案对象的Set(TreeSet)。

以下是相关的代码片段,展示了我如何使用Jackson注释:

// Questionnaire abstract base class
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
    @JsonSubTypes.Type(value=GIQuestionnaire.class, name="GIQuestionnaire"),
    @JsonSubTypes.Type(value=PolicyQuestionnaire.class, name="PolicyQuestionnaire")    
})
public abstract class Questionnaire implements Serializable {
    @JsonManagedReference("questionnaire-questions")
    @JsonDeserialize(as = TreeSet.class, contentAs = Question.class)

    private Set<Question> questions = new TreeSet<>();

    // … remainder omitted for brevity 
}

// Question abstract base class
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = EntityQuestion.class, name = "EntityQuestion"),
    @JsonSubTypes.Type(value = IncludeExcludeQuestion.class, name = "IncludeExcludeQuestion")
})
@JsonDeserialize(using = QuestionDeserializer.class)
public abstract class Question implements Comparable<Question>, Serializable {
   @JsonBackReference("questionnaire-questions")
   private Questionnaire questionnaire;

   @JsonManagedReference("question-answers")
   @JsonDeserialize(as = TreeSet.class, contentAs = Answer.class)
   private Set<Answer> answers = new TreeSet<>();

    // … remainder omitted for brevity
}

// Answer abstract base class
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = EntityAnswer.class, name = "EntityAnswer"),         
    @JsonSubTypes.Type(value = IncludeExcludeAnswer.class, name = "IncludeExcludeAnswer")
})
@JsonDeserialize(using = AnswerDeserializer.class)
public abstract class Answer implements Comparable<Answer>, Serializable {

   @JsonBackReference("question-answers")
   private Question question;
   // … remainder omitted for brevity 
}

public class QuestionDeserializer extends JsonDeserializer<Question> {
    @Override
    public Question deserialize(JsonParser jsonParser, DeserializationContext     deserializationContext) throws IOException, JsonProcessingException {
        //deserialize a Question to a concrete instance
    }
}

public class AnswerDeserializer extends JsonDeserializer<Answer> {
    @Override
    public Answer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        //deserialize an Answer to a concrete instance
    }
}

我的注释和/或反序列化器出了什么问题?

2 个答案:

答案 0 :(得分:3)

根据Jackson WIKI,不支持多态反序列化和@JsonManagedReference / @JsonBackReference的组合。

  

目前(从版本1.6.0开始)抽象类型的组合(使用@JsonTypeInfo进行多态处理)不适用于此功能

另请参阅杰克逊问题跟踪器中的最新一期JIRA-890

答案 1 :(得分:0)

如果您尝试使用jackson 2库中提供的非常方便的界面实现@JsonIdentityInfo,该怎么办?

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Answer { ....

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Question { ....

在maven中

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.0.2</version>
</dependency>