johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Struberg <strub...@yahoo.de.INVALID>
Subject Mapping objects with cycles
Date Tue, 05 Apr 2016 21:33:33 GMT
Hi!

While discussing with Reinhard we came over an interesting use case:

public class Person {
  private String name;
  private Person marriedTo;
}

Person reinhard = new Person("reinhard");
Person anna = new Person("anna");

reinhard.setMarriedTo(anna);
anna.serMarriedTo(reinhard);

String json = Mapper.writeObjectAsString(reinhard);



Currently this sample would lead to an endless loop, right?

BUT, we can do something against it: We keep a Map<Object, Integer>. For each object
written we increment the number;
IF an object was already written before (comparison using == and not equals()!) then we write
a 'placeholder'

{"name":"reinhard","marriedTo":{"name":"anna","marriedTo":"1"}}

Not sure if we can use a simple number or whether we need to use a placeholder object with
e.g. "//javaType". 

But you get the idea I hope.

Any thoughts?

LieGrue,
strub

Mime
View raw message