jackson - Deserialize object with a list of polymorphic types
I think it's best to explain with an example.
I have a JSON object I want to deserialize that contains a list of type interface and which type is in the list, but I'm not sure how to get the deserializer to determine which concrete type is in the list:
Type to deserialize
public class MyClass
private MyEnum type; // A or B
private List<Parent> objects;
Interface
public interface Parent
Children
public ChildA implements Parent
public ChildB implements Parent
I know I can use the type with JsonSubTypes with the type if it wasn't a List, such as:
@JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, property = "type", visible = true)
@JsonSubTypes(
@JsonSubTypes.Type(value = ChildA.class, name = "A"),
@JsonSubTypes.Type(value = ChildB.class, name = "B")
)
And the same thing if type is inside the Parent type. But is there a way to assist the deserializer in determining the type in the list when the type is outside the Parent class? (the list will only ever contain one child type)
java json jackson
add a comment |
I think it's best to explain with an example.
I have a JSON object I want to deserialize that contains a list of type interface and which type is in the list, but I'm not sure how to get the deserializer to determine which concrete type is in the list:
Type to deserialize
public class MyClass
private MyEnum type; // A or B
private List<Parent> objects;
Interface
public interface Parent
Children
public ChildA implements Parent
public ChildB implements Parent
I know I can use the type with JsonSubTypes with the type if it wasn't a List, such as:
@JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, property = "type", visible = true)
@JsonSubTypes(
@JsonSubTypes.Type(value = ChildA.class, name = "A"),
@JsonSubTypes.Type(value = ChildB.class, name = "B")
)
And the same thing if type is inside the Parent type. But is there a way to assist the deserializer in determining the type in the list when the type is outside the Parent class? (the list will only ever contain one child type)
java json jackson
add a comment |
I think it's best to explain with an example.
I have a JSON object I want to deserialize that contains a list of type interface and which type is in the list, but I'm not sure how to get the deserializer to determine which concrete type is in the list:
Type to deserialize
public class MyClass
private MyEnum type; // A or B
private List<Parent> objects;
Interface
public interface Parent
Children
public ChildA implements Parent
public ChildB implements Parent
I know I can use the type with JsonSubTypes with the type if it wasn't a List, such as:
@JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, property = "type", visible = true)
@JsonSubTypes(
@JsonSubTypes.Type(value = ChildA.class, name = "A"),
@JsonSubTypes.Type(value = ChildB.class, name = "B")
)
And the same thing if type is inside the Parent type. But is there a way to assist the deserializer in determining the type in the list when the type is outside the Parent class? (the list will only ever contain one child type)
java json jackson
I think it's best to explain with an example.
I have a JSON object I want to deserialize that contains a list of type interface and which type is in the list, but I'm not sure how to get the deserializer to determine which concrete type is in the list:
Type to deserialize
public class MyClass
private MyEnum type; // A or B
private List<Parent> objects;
Interface
public interface Parent
Children
public ChildA implements Parent
public ChildB implements Parent
I know I can use the type with JsonSubTypes with the type if it wasn't a List, such as:
@JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, property = "type", visible = true)
@JsonSubTypes(
@JsonSubTypes.Type(value = ChildA.class, name = "A"),
@JsonSubTypes.Type(value = ChildB.class, name = "B")
)
And the same thing if type is inside the Parent type. But is there a way to assist the deserializer in determining the type in the list when the type is outside the Parent class? (the list will only ever contain one child type)
java json jackson
java json jackson
edited Nov 13 '18 at 3:16
MiketheCalamity
asked Nov 13 '18 at 2:43
MiketheCalamityMiketheCalamity
424722
424722
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Jackson struggles with type erasure on collections with JsonTypeInfo (Here's a Jackson Scala bug report). However, arrays are strongly typed so if possible, you could switch to using an array instead. It seems the simplest way.
Alternatively, writing a smart JsonDeserializer capable of instantiating the correct class could be used with the @JsonDeserialize(contentUsing=...) annotation. Internally, the deserializer could still fall back to using Jackson's readValue method. So it really would be just responsible for choosing the correct type. This answers of this question give examples of reverting to standard deserialization from a custom deserializer.
The only issue I can't seem to work out is using the deserializer withcontentUsageis that thetypeflag is outside theParentclass.
– MiketheCalamity
Nov 13 '18 at 20:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273028%2fjackson-deserialize-object-with-a-list-of-polymorphic-types%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Jackson struggles with type erasure on collections with JsonTypeInfo (Here's a Jackson Scala bug report). However, arrays are strongly typed so if possible, you could switch to using an array instead. It seems the simplest way.
Alternatively, writing a smart JsonDeserializer capable of instantiating the correct class could be used with the @JsonDeserialize(contentUsing=...) annotation. Internally, the deserializer could still fall back to using Jackson's readValue method. So it really would be just responsible for choosing the correct type. This answers of this question give examples of reverting to standard deserialization from a custom deserializer.
The only issue I can't seem to work out is using the deserializer withcontentUsageis that thetypeflag is outside theParentclass.
– MiketheCalamity
Nov 13 '18 at 20:08
add a comment |
Jackson struggles with type erasure on collections with JsonTypeInfo (Here's a Jackson Scala bug report). However, arrays are strongly typed so if possible, you could switch to using an array instead. It seems the simplest way.
Alternatively, writing a smart JsonDeserializer capable of instantiating the correct class could be used with the @JsonDeserialize(contentUsing=...) annotation. Internally, the deserializer could still fall back to using Jackson's readValue method. So it really would be just responsible for choosing the correct type. This answers of this question give examples of reverting to standard deserialization from a custom deserializer.
The only issue I can't seem to work out is using the deserializer withcontentUsageis that thetypeflag is outside theParentclass.
– MiketheCalamity
Nov 13 '18 at 20:08
add a comment |
Jackson struggles with type erasure on collections with JsonTypeInfo (Here's a Jackson Scala bug report). However, arrays are strongly typed so if possible, you could switch to using an array instead. It seems the simplest way.
Alternatively, writing a smart JsonDeserializer capable of instantiating the correct class could be used with the @JsonDeserialize(contentUsing=...) annotation. Internally, the deserializer could still fall back to using Jackson's readValue method. So it really would be just responsible for choosing the correct type. This answers of this question give examples of reverting to standard deserialization from a custom deserializer.
Jackson struggles with type erasure on collections with JsonTypeInfo (Here's a Jackson Scala bug report). However, arrays are strongly typed so if possible, you could switch to using an array instead. It seems the simplest way.
Alternatively, writing a smart JsonDeserializer capable of instantiating the correct class could be used with the @JsonDeserialize(contentUsing=...) annotation. Internally, the deserializer could still fall back to using Jackson's readValue method. So it really would be just responsible for choosing the correct type. This answers of this question give examples of reverting to standard deserialization from a custom deserializer.
edited Nov 13 '18 at 5:22
answered Nov 13 '18 at 3:53
Alex TaylorAlex Taylor
5,53531533
5,53531533
The only issue I can't seem to work out is using the deserializer withcontentUsageis that thetypeflag is outside theParentclass.
– MiketheCalamity
Nov 13 '18 at 20:08
add a comment |
The only issue I can't seem to work out is using the deserializer withcontentUsageis that thetypeflag is outside theParentclass.
– MiketheCalamity
Nov 13 '18 at 20:08
The only issue I can't seem to work out is using the deserializer with
contentUsage is that the type flag is outside the Parent class.– MiketheCalamity
Nov 13 '18 at 20:08
The only issue I can't seem to work out is using the deserializer with
contentUsage is that the type flag is outside the Parent class.– MiketheCalamity
Nov 13 '18 at 20:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273028%2fjackson-deserialize-object-with-a-list-of-polymorphic-types%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown