On Thu, Feb 16, 2017 at 1:43 PM, David Hayek <
david...@sial.com> wrote:
> If I have to serialize an object that I can not annotate and that contains
> parent-child relationship, such as a DefaultMutableTreeNode, I can avoid the
> infinite recursion/stack overflow problem by supplying an ObjectIdGenerator
> from the findObjectIdInfo() method in a subclass of the
> JacksonAnnotationIntrospector. The ID value is in the resulting JSON object.
>
> There's an example here:
>
http://stackoverflow.com/questions/27316485/using-jsonidentityinfo-without-annotations
>
> (In addition, with the DefaultMutableTreeNode class I have to supply a
> custom BeanPropertyWriter to handle exceptions thrown when serializing
> objects like this. See:
>
http://stackoverflow.com/questions/35359430/how-to-make-jackson-ignore-properties-if-the-getters-throw-exceptions/35416682)
>
> I have 3 constraints I'm trying to satisfy:
>
> 1. I can not annotate objects in various dependent libraries.
> 2. I can not enumerate ahead-of-time all of the possible objects that have
> parent-child relationships
> 3. I do not want the virtual ID value from an ObjectIdGenerator in the
> serialized JSON object, although I still need a way to keep track of them.
How would the id be known on deserialization then, if it is not
written as part of data?
Is it calculated from other fields?
> To satisfy 1 and 2 I was thinking of using an ObjectIdGenerator for all
> objects, regardless of whether they contain parent-child relationships or
> whether I had access to the source files. This would simplify development,
> but are there performance penalties for doing this?
>
> I'm not sure how to proceed with #3. What objects do I need to sub-class in
> order to get this to work? It looks like the actual output is done by the
> WritableObjectId object, which is returned from the
> DefaultSerializerProvider.findObjectId() method. But the WritableObjectId is
> declared final so I can't override its writeAsField() method. Is there
> another way to do this?
Current system is designed for Object Id to be included in data, so
even if sub-classing was allowed, changing
behavior in sub-class would be a fragile solution.
I am not necessarily against removing `final` keyword, but just
suggesting that it might be better to add explicit support which could
(and should) be tested via unit tests, to avoid accidental breakages.
So I guess I would be open to improvement ideas that would achieve
this with less work than many overrides; but also if you want, please
file an RFE for just making `WritableObjectId` non-final. Ideally this
should go in 2.9, but I think this is one of cases where change in
patch would be acceptable (reverse of making method final would not be
-- but this change can not break anything wrt code that worked with
earlier patches).
-+ Tatu +-
>
> Thanks for your help.
>
>
>
> This message and any attachment are confidential and may be privileged or
> otherwise protected from disclosure. If you are not the intended recipient,
> you must not copy this message or attachment or disclose the contents to any
> other person. If you have received this transmission in error, please notify
> the sender immediately and delete the message and any attachment from your
> system. Merck KGaA, Darmstadt, Germany and any of its subsidiaries do not
> accept liability for any omissions or errors in this message which may arise
> as a result of E-Mail-transmission or for damages resulting from any
> unauthorized changes of the content of this message and any attachment
> thereto. Merck KGaA, Darmstadt, Germany and any of its subsidiaries do not
> guarantee that this message is free of viruses and does not accept liability
> for any damages caused by any virus transmitted therewith.
>
> Click
http://www.emdgroup.com/disclaimer to access the German, French,
> Spanish and Portuguese versions of this disclaimer.
>
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
jackson-user...@googlegroups.com.
> To post to this group, send email to
jackso...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.