You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@johnzon.apache.org by Mark Struberg <st...@yahoo.de.INVALID> on 2016/04/05 23:33:33 UTC
Mapping objects with cycles
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
Re: Mapping objects with cycles
Posted by Romain Manni-Bucau <rm...@gmail.com>.
just check jsonp before, know they were talking about it and if so we
should rely on the spec. If not it sounds good for me.
Romain Manni-Bucau
@rmannibucau | Blog | Github | LinkedIn | Tomitriber
2016-04-06 9:11 GMT+02:00 Mark Struberg <st...@yahoo.de.invalid>:
> Using JsonPointers for this is a good idea.
>
>
> I'm also fine with the 3 configurable modes.
>
> With JsonPointers the sample would result in the following string:
>
> {"name":"reinhard","marriedTo":{"name":"anna","marriedTo":"/"}}
>
> We could store a IdentityMap<Object, String> to map from the Object to it's corresponding JsonPointer during the processing.
>
>
> LieGrue,
> strub
>
>
>
>
>
>> On Wednesday, 6 April 2016, 0:59, Romain Manni-Bucau <rm...@gmail.com> wrote:
>> > JsonPointer is the clean solution to that.
>>
>> Also I would like the default to fail - stackoverflow - and 2 flags to 1.
>> skip 2. use jsonpointer.
>>
>> Maintaining ref can have issues and == can fail in several cases, that s
>> why I want this default matching model 1-1.
>>
>> Finally to be complete flag should be an annot or global mapperbuilder (+
>> chain until front integrations) option.
>>
>> Le 5 avr. 2016 23:34, "Mark Struberg"
>> <st...@yahoo.de.invalid> a écrit :
>>
>>> 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
>>>
>>
Re: Mapping objects with cycles
Posted by Mark Struberg <st...@yahoo.de.INVALID>.
Using JsonPointers for this is a good idea.
I'm also fine with the 3 configurable modes.
With JsonPointers the sample would result in the following string:
{"name":"reinhard","marriedTo":{"name":"anna","marriedTo":"/"}}
We could store a IdentityMap<Object, String> to map from the Object to it's corresponding JsonPointer during the processing.
LieGrue,
strub
> On Wednesday, 6 April 2016, 0:59, Romain Manni-Bucau <rm...@gmail.com> wrote:
> > JsonPointer is the clean solution to that.
>
> Also I would like the default to fail - stackoverflow - and 2 flags to 1.
> skip 2. use jsonpointer.
>
> Maintaining ref can have issues and == can fail in several cases, that s
> why I want this default matching model 1-1.
>
> Finally to be complete flag should be an annot or global mapperbuilder (+
> chain until front integrations) option.
>
> Le 5 avr. 2016 23:34, "Mark Struberg"
> <st...@yahoo.de.invalid> a écrit :
>
>> 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
>>
>
Re: Mapping objects with cycles
Posted by Romain Manni-Bucau <rm...@gmail.com>.
JsonPointer is the clean solution to that.
Also I would like the default to fail - stackoverflow - and 2 flags to 1.
skip 2. use jsonpointer.
Maintaining ref can have issues and == can fail in several cases, that s
why I want this default matching model 1-1.
Finally to be complete flag should be an annot or global mapperbuilder (+
chain until front integrations) option.
Le 5 avr. 2016 23:34, "Mark Struberg" <st...@yahoo.de.invalid> a écrit :
> 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
>