Skip to content
Advertisement

Jackson XML deserialization of abstract type results in fields with null values

When trying to deserialize XML to an object that extends an abstract base class, I’m seeing that the list of references contains the expected number of elements, but all the fields on those objects are null.

This only happens if I create an XML reader for the abstract class. If I deserialize directly to the concrete implementation all the fields have the expected value.

I’ve added the minimum working example below

Expected output (as json for readability)

JavaScript

Actual output (as json for readability)

JavaScript

Test Data

JavaScript

Abstract base class

JavaScript

Concrete Implementation

JavaScript

Test Cases

JavaScript

Advertisement

Answer

Turns out there are multiple problems, that I’ve managed to solve.

  1. The Jackson version I was using (2.11) had some problems with multiple elements using the same tag, not in a wrapper. This is something I was aware of, and is the reason why my setter does “addAll” instead of just setting the list

This problem was solved by upgrading to 2.12, which means that it’s no longer necessary to do this to handle unwrapped elements.

  1. Jackson failed to properly deserialize the items, because the setter accepts a list, which apparently breaks due to some java generic mumbo jumbo (I was never able to figure out exactly why, just that it happens).

I solved this by having the setter accept a single element, and then adding that to the list

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement