JsonTypeInfo ignored when serializing a plain list
See original GitHub issueJsonTypeInfo seems to be ignored entirely if the serialized value is a list (and not embedded in another bean). This is present with the most recent 2.8.9.
Repro:
public class TypeSerializationTest {
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(name = "sub1", value = Sub1.class),
@JsonSubTypes.Type(name = "sub2", value = Sub2.class)})
public static class Superclass {}
public static class Sub1 extends Superclass {}
public static class Sub2 extends Superclass {}
class ListHolder {
@JsonProperty
ArrayList<Superclass> list;
}
@Test
public void testRoundtripClassName() throws IOException {
ArrayList<Superclass> list = new ArrayList<>(Arrays.asList(
new Sub1(),
new Sub2()));
ListHolder foo = new ListHolder();
foo.list = list;
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(foo));
System.out.println(mapper.writeValueAsString(foo.list));
}
}
Produces this:
{"list":[{"type":"sub1"},{"type":"sub2"}]}
[{},{}]
I’d expect the second line to have the ‘type’ property information as well?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why does Jackson polymorphic serialization not work in lists?
The parent class configures @JsonTypeInfo to use an existing property as the marker to break the ambiguity of the sub types
Read more >@JsonTypeInfo doesn't serialize property value in a List
I want to serialize value with `@JsonTypeName`. When serialize with `List`, the property value is not shown. public class Main {.
Read more >Jackson-js: Powerful JavaScript decorators to serialize ...
@JsonIgnoreProperties can be used as a class-level decorator that marks a property or a list of properties that will be ignored during serialization...
Read more >Jackson Annotation Examples - Baeldung
@JsonIgnoreProperties is a class-level annotation that marks a property or a list of properties that Jackson will ignore. Let's look at a quick ......
Read more >FasterXML/jackson-databind - Gitter
public class Wrapper { @JsonTypeInfo(...) Object value; }. and so you would construct non-generic Wrapper with polymorphic value to serialize, deserialize ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
Ok, this needs to go in FAQ. It gets asked on monthly basis. 😃
Yes, this is as expected: the only type that Jackson can see (due to Java Type Erasure) is
ArrayList<?>
. Since?
is about same asObject
, and has no polymorphic type indicators, type is not listed. Same applies to all generic types, but not allList
s (orMap
s etc).There are couple of work-arounds here:
Remove generic-ness by sub-typing, using:
public class MyFooList extends ArrayList<Superclass> { }
Specifying type information on write call:
mapper.writerFor(new TypeReference<List<Superclass>>() { }) .writeValueAsString();
both of which work. But in general it may be best to simply avoid use of generic types as root value if possible.
@dweiss No problem at all. I can see how this is surprising and sub-optimal. I just would like to make it easier to figure this out since it is quite intricate. And I may be one of few people who know it fully, having implemented the system… took a while and is partly complicated due to configurability of various aspect.