Skip to content

Mapping to POJO‐like Objects

Jan Bernitt edited this page Jan 25, 2024 · 2 revisions

Warning

The documentation below has not been updated to the newest version

Mapping to actual POJO objects defined as class would negate many of the key benefits of the on demand parsing and accessing the virtual tree API provides. This is why the mapping feature is slightly different in this library.

The target types are defined as interfaces, but this time with abstract methods, for example:

interface Account extends JsonObject
{
  boolean isEnabled();
  String getUsername();
  List<Membership> memberships();
}

interface Membership extends JsonObject
{
   String getGroup();
   Date getJoinDate();
}

This would match JSON of the structure:

{
  "enabled": true,
  "username": "TomTraubert",
  "memberships": [{ "group": "BigTimers", "joinDate": "1973-02-05" }]
}

The return types of the abstract methods support most common JDK types such as primitives and their wrappers, strings, dates and collections. Further types can be added to the default "mapper" using JsonTypedAccess.GLOBAL.add.

A complete custom mapping can be created via new JsonTypedAccess() and using init() and add(...) or by writing your own implementation of a JsonTypedAccessStore. A custom store is then used by creating your root via JsonValue.of(json, myStore).

Note that the mapping to target types defined by abstract interface method can be mixed with using default methods and lazily mapped collection types such as the JsonList.

A POJO-like interace can extend any of the JsonValue tree types. It makes most sense to either chose JsonValue to inherit as little further methods, or to extends JsonObject to also allow use of the generic object accessor methods on the POJO-like data type.

Clone this wiki locally