Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to deserializ​e a Set of polymorphi​c objects (Jackson 2.1.2)
    primarykey
    data
    text
    <p>I’m trying to deserialize the following:</p> <pre><code>{ "type":"GIQuestionnaire", "id":6051, "questions":[ { "type":"IncludeExcludeQuestion", "id":24057, "answers":[ { "type":"IncludeExcludeAnswer", "id":101497 }, { "type":"IncludeExcludeAnswer", "id":101496 } ] } ] } </code></pre> <p>Which results in this error:</p> <pre><code>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) </code></pre> <p>The Junit test:</p> <pre><code>@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()); } } </code></pre> <p>A Questionnaire contains a Set (implemented as TreeSet) of Question objects. A Question contains a Set (TreeSet) of Answer objects.</p> <p>Here are the relevant snippets of code that show how I used Jackson annotations:</p> <pre><code>// 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&lt;Question&gt; questions = new TreeSet&lt;&gt;(); // … 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&lt;Question&gt;, Serializable { @JsonBackReference("questionnaire-questions") private Questionnaire questionnaire; @JsonManagedReference("question-answers") @JsonDeserialize(as = TreeSet.class, contentAs = Answer.class) private Set&lt;Answer&gt; answers = new TreeSet&lt;&gt;(); // … 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&lt;Answer&gt;, Serializable { @JsonBackReference("question-answers") private Question question; // … remainder omitted for brevity } public class QuestionDeserializer extends JsonDeserializer&lt;Question&gt; { @Override public Question deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { //deserialize a Question to a concrete instance } } public class AnswerDeserializer extends JsonDeserializer&lt;Answer&gt; { @Override public Answer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { //deserialize an Answer to a concrete instance } } </code></pre> <p>Where am I going wrong with my annotations and/or Deserializers? Thanks very much!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload