JsonIdentityInfo with virtual attributes for composite or compound keys

26 views
Skip to first unread message

Thø Jø

unread,
Apr 19, 2020, 2:05:59 PM4/19/20
to jackson-user
Hey,

I currently resolve circulare dependencies with JsonIdentityInfo and new generated UUID. I would like use existing primary key. My problem is that my PK is a composite key of normally two attributes (String or int) "id" and "version". I cannot change the classes because of 3rd party lib. I can only use Mixin or something else.

public class Parent {
  // this is my identiy composite key
  ParentPK pk;
  String bez;
  Date created;
  Child child;

  // constructor, getter and setter
}

public class ParentPK {
  int parentId;
  int version;

  // constructor, getter and setter
}

public class Child {
  String childId;
  Parent parent;

  // constructor, getter and setter
}

How can I use my ParentPK as identity in Jackson? I've tried to use JsonIdentityInfo in combination with virtual attributes and JsonAppend. But his won't work because the property does not exist in POJO. In my mind I've several ideas as solutions but don't know whether they can implemented in Jackson:
  • Use existing ParentPK JSON representation e.g. {id:"99", version:"v1"} as JsonIdentityInfo and use equals method in Jackson to compare objects will be the best because no additional attribute will be added to parent. But I don't know whether custom objects are possible as identity?
  • Create virtual String attribute "compositeKey" e.g. "99-v1 and use it as JsonIdentityInfo. But the virtual attribute via JsonAppend cannot be used in combination with JsonIdentityInfo.

Have anyone an idea to implement this?

Thx
Thomas

Tatu Saloranta

unread,
Apr 19, 2020, 2:19:43 PM4/19/20
to jackson-user
Unfortunately `@JsonIdentityInfo` was designed with the assumption
that Ids are always scalars (Strings or possibly integers), so there
is no support for such composite keys. There is a work-around for
specific case of "boxed" single-property JSON object to support a json
convention/standard (name of which escapes me now), but nothing beyond
that.

But maybe someone else has written something for "full Object" use
case. Underlying components of identity handling might still be
usable.

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