jackson-databind: deserialization of non-static inner fails. Why?

2,019 views
Skip to first unread message

Kees Kuip

unread,
Jul 8, 2021, 11:49:21 AM7/8/21
to jackson-user

The serialization of inner classes works fine if the inner-class definition is in the same class.
This works:
<pre>
  static class Person{
    public String name;
    public Address address = new Address();

    class Address{
      public String city;
      public Address({
      }
    }
  }
</pre>

But deserialization of a the subclass of person fails.
<pre>
  static class Person2
      extends Person{
    public Address address2 = new Address();
  }
</pre>

The fail message is:
 com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `Test$Person$Address`: non-static inner classes like this can only by instantiated using default, no-argument constructor

Anybody knows how to fix this problem? (I do not want to create a static inner class though)

Tatu Saloranta

unread,
Jul 8, 2021, 11:52:25 AM7/8/21
to jackson-user
The underlying reason is due to non-static inner classes having a
hidden "parent-this" pointer, linking to the enclosing class.
You can look at the class file generated for that inner class to see
this extra argument to the constructor.

There is no way to support this in general, now or in future. It is a
fundamental limitation.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages