question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with JsonTypeInfo and Lists

See original GitHub issue

When 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:closed
  • Created 11 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Aug 13, 2013

@ilgrosso Actually this is due to type-erasure: what Jackson sees is a List<?>, and since ? in there can only be considered same as java.lang.Object, no type information is included (as there is no @JsonTypeInfo in java.lang.Object definition. There are ways to get around that:

  1. Provide explicit root type (construct an ObjectWriter with JavaType for List<Child1>)
  2. Create sub-class of List that is not generic; like class ChildList extends ArrayList<Child1> { }, pass that

and 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).

0reactions
galcyuriocommented, Aug 31, 2017
private ObjectMapper mObjectMapper;
private ObjectWriter mObjectWriter;

...

mObjectMapper = new ObjectMapper();
mObjectWriter = mObjectMapper.writerFor(mObjectMapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
MyClass myClass = new MyClass(...);
mObjectMapper.writeValueAsString(myClass);
mObjectWriter.writeValueAsstring(myClass);
[{"answer":"asdf"},{"answers":[1,2,3]}]
[{"type":"shortTextResult","answer":"asdf"},{"type":"choiceResult","answers":[1,2,3]}]

I solved likely above, but I cannot assume that this is best practice…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found