users@jersey.java.net

Re: [Jersey] Problem with circular dependencies when marshalling to JSON my JAXB annotated classes

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Thu, 22 Apr 2010 13:04:46 +0200

Hi Leo,

could you please file a bug and attach a simple reproducible test case?
My guess is that badgerfish keeps some namespace related information,
which is lost
when the mapped notation is used (there you need to configure the
namespace mapping explicitly).

It could also help if you just send me the problematic data in
xml/badgerfish/mapped format and i
will see if i can provide you with some configuration options to use.

~Jakub


Leo Romanoff wrote:
> Hi,
>
> I have a problem with JSON-serialization of my JAXB-annotated classes using
> JSONMarshaller class.
> I use the Notation.MAPPED notation for JSON.
>
> Some of my JAXB annotated classes are using @XmlID and @XmlIDRef annotations
> and in my object graph, some objects refer to others. Some times, these
> references are circular, i.e. object A refers to B and B in turn refers to
> A.
>
> If I use the usual JAXB serialization to XML with the same classes and
> object graph, everything works just fine. I can first marshall into XML and
> then unmarshall, and I get an object that is structurally the same.
>
> But when I select JSON and use MAPPED format, the following thing happens:
> - marshalling works fine
> - but unmarshalling from JSON sometime returns wrong results. Specifically,
> it looses some elements of the object graph for the cases, where circular
> dependencies are present. For example, when it reads the whole graph and at
> some point it reads A which refers to B, but B is not seen yet, the
> unmarshaller would assign null to the reference to B inside A. Later B is
> read, but this reference from A to B is never updated.
>
> BTW, BadgerFish serialization works with the same classes and objects
> properly. But it produces rather ugly looking JSON, which is bad for my
> specific application. My goal is to create JSON-representation of my objects
> and to store it inside MongoDB, which is a JSON-based NoSQL database.
> Unfortunately, BadgerFish format is rather bad for queries on JSON
> documents, where element names are supposed to be used. Therefore,
> preserving proper names for elements and attributes is very important.
>
> Questions:
> 1) Is it a known problem with JSON Mapped marshalling/unmarshalling?
> 2) May be some sort of a pass for resolving references should be done on the
> JSON structure, once all elements are read and known? In this case, all
> correct references could be resolved.
> 3) Are there any other ways to do proper JSON marshalling/unmarshalling for
> object graphs and classes with circular dependencies?
>
> Thanks in advance for any feedback!
>