Issue with JsonTypeInfo and Lists
See original GitHub issueWhen I try and serialize a list of objects that have JsonTypeInfo defined, the objects in the list do not have the JsonTypeInfo property set. I am having problems with this against both 1.9.4 and 2.0.2, if there is a way to do this that I missed in the documentation, please let me know.
diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestAbstractTypeNames.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestAbstractTypeNames.java
index 5a257a1..57213b4 100644
--- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestAbstractTypeNames.java
+++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestAbstractTypeNames.java
@@ -9,6 +9,8 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Unit tests for checking how combination of interfaces, implementation
@@ -93,6 +95,21 @@ public class TestAbstractTypeNames extends BaseMapTest
/**********************************************************
*/
+ public void testCollection() throws Exception
+ {
+ ObjectMapper mapper = new ObjectMapper();
+ List<DefaultEmployee> friends = new ArrayList<DefaultEmployee>();
+ friends.add(new DefaultEmployee("Joe Hildebrandt", null, "MDA"));
+
+ String singleObject = mapper.writeValueAsString(friends.get(0));
+ ObjectNode output = mapper.readValue(singleObject, ObjectNode.class);
+ assertEquals("Employee", output.get("userType").asText());
+
+ String list = mapper.writeValueAsString(friends);
+ ArrayNode listOutput = mapper.readValue(list, ArrayNode.class);
+ assertNotNull("Expected " + listOutput.get(0) + " to include userType", listOutput.get(0).get("userType"));
+ }
+
// Testing [JACKSON-498], partial fix
public void testEmptyCollection() throws Exception
{
Issue Analytics
- State:
- Created 11 years ago
- Comments:19 (9 by maintainers)
Top Results From Across the Web
Jackson Wrongly Adding JsonTypeInfo into List - Stack Overflow
I'm trying to serialize and deserialize some polymorphic data into json using Jackson. Most subclasses should have a type-info attached, ...
Read more >@JsonTypeInfo doesn't serialize property value in a List
Note, however, that this problem does NOT happen with generic types referenced as Java class properties -- it is strictly limited to cases...
Read more >Java JSON 04 Jackson Inheritance using JsonTypeInfo
How to use the Jackson library to read and write an arraylist of objects of different types to a JSON file and how...
Read more >Using @JsonTypeInfo annotation to handle polymorphic types
Jackson JSON - Using @JsonTypeInfo annotation to handle polymorphic types ... public class View { private List<Shape> shapes; .
Read more >Jackson Tips: custom List serialization | by @cowtowncoder
It is especially powerful in avoiding dreaded “Generic Types as Root Values” problem where, for example: @JsonTypeInfo(...) // we have a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ilgrosso Actually this is due to type-erasure: what Jackson sees is a
List<?>
, and since?
in there can only be considered same asjava.lang.Object
, no type information is included (as there is no@JsonTypeInfo
injava.lang.Object
definition. There are ways to get around that:ObjectWriter
withJavaType
forList<Child1>
)List
that is not generic; likeclass ChildList extends ArrayList<Child1> { }
, pass thatand if so, generic types of
List
can be determined correctly to produce expected output. So there is no bug with this handling (unless one consider Java’s Type Erasure to be bug; but that’s not something I can do anything about).At this point I will close this bug as descriptions and comments have been bouncing to various things; let’s open new specific entries for specific remaining problems (with 2.2.2 and above).
I solved likely above, but I cannot assume that this is best practice…