Infinite loop with Johnzon JSON serialization - json

I'm designing a very simple web app with a REST web service that utilizes JPA to interact with a PostgreSQL database and runs in TomEE. My JPA entities have bidirectional mappings and I want my REST service to consume/produce those JPA entities as XML and JSON.
XML serialization works fine because I'm using the #XmlTransient annotation on one side of each bidirectional mapping in order to prevent an infinite loop during serialization.
Unfortunately, during JSON serialization I enter an infinite loop and a StackOverflowError is generated. I assumed that since TomEE uses Apache CXF that it would also use Jettison and I thought Jettison respected the #XmlTransient annotation.
However, it looks like TomEE is actually using Johnzon and that doesn't seem to respect the #XmlTransient annotation. How can I tell Johnzon to ignore certain fields? Could I somehow use the #JsonbTransient annotation from the JSON-B spec? I'd prefer not to link against Johnzon but I tried that in order to use the #JohnzonIgnore annotation without effect. Am I better off forcing TomEE to use Jettison? Any suggestions?
You can reproduce this bug for yourself because the rest-example that TomEE posted on their web site has the same issue, http://tomee.apache.org/examples-trunk/rest-example/README.html.

First you can use #javax.json.bind.annotation.JsonbVisibility to switch to fields.
Johnzon also supports cyclic references by enabling 'johnzon.deduplicateObjects'.
It basically replaces any cyclic objeccts with JsonPointers and automatically back on deserialisation.
A detailed description can be found in the JavaDocs
https://github.com/apache/johnzon/blob/master/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/MapperBuilder.java#L486

Related

Changing JSON Provider (Spring / Spring-Boot)

Is it possible to use Jettison with Spring / Spring-Boot instead of default JSON Provider Jackson? I have one such requirement to match Json output with an very old project which used Jettison.
If yes, can i get some pointers/hints please?
It's possible but you're going to be doing a lot of hacking and writing boiler-plate configuration code as Jackson is embedded pretty deep inside Spring.
It might be easier for you to leverage Jackson's functionality to serialize/deserialize your data in the format you need (instead of the format Jackson silently provides)?

Consistent JSON representations with JAXB and JAX-RS?

I am writing a Java EE app to be run within either Glassfish or JBoss, and I've been asked to provide JSON support for our JAX-RS clients.
Of course, simply enabling the JSON media type is simple enough but results in vendor-specific representations of my JAXB annotated models. So I'm looking for a way forward, one that ensures my tests work with both AS servers and that I still leverage JAXB/JAX-RS instead of writing lots of custom code.
I've tried adding a jaxb.properties file to select a specific implementation, but presumably shipping Jackson/MOXy/other JAXB library may conflict with provided libraries?
Any other ideas would be appreciated.

Hibernate / JPA friendly Jackson Serializer supporting Jackson 1.8

The custom BeanSerializerFactory in http://kyrill007.livejournal.com/2577.html is the only custom solution I found to allow directly throwing persistent beans to JSON via Spring 3.0, and it works, it only serializes non initialized (lazy) attributes / collections (this allows me to use the entity Pojo as a DTO, as I initialize only what I want, and what is not initialized, doesn't get serialized to JSON)
But this worked well with Jackson 1.6, and I wanted to upgrade to Jackson 1.8 to solve the issue with Java Generics (hopefully) and now that custom solution is not compiling.
So my questions are
What is the recomended way to auto serialize Entities to JSON without the need of DTOs
Is there an official Hibernate Aware Jackson BeanSerializerFactory besides the above
I'm starting to fear that if it's that hard to find, maybe my practice is not the best one
What is the recommended way to do RESTful Ajax then with Spring 3.0 MVC and JSON?
The problems to solve are
Not serializing lazy attributes / collections automatially (as the custom code above does)
Supporting Java Generics and some kind of a client side object schema / validation
What works on get should work on save, and allow partial objects graphs to be returned safely
Is there Anything? do I have to manually write DTOs for every Entity?, this is so non productive
While SO has lots of experts, you might consider also asking on Jackson users list. Kirill (author of the blog entry) is responsive, and there are other experts there as well.

Generate GWT Overlay types from Java objects

We're currently using GWT RPC for serialization on a GWT project but we're currently maintaining two sets of objects - the object that we need to convert for the database to retrieve/save and a version of the object that is safe for GWT RPC serialization (no enums/big decimal, etc.).
We're spending a lot of effort writing code that merely converts from one format to the other format. In addition it's pretty painful to make any changes to the data model because it has to be changed in two places.
I was thinking that we could use a combination of Spring 3.0 MVC and Jackson to replace the RPC calls with JSON calls. If we built JavaScript objects for GWT to hold this JSON data, then it would remove the need for any property conversion code. However we'd still have to maintain two sets of objects - one JavaScriptObject for the client side code and the server side representation.
To eliminate this layer, to take a Java object and have it produce a GWT JavaScriptObject with the JSNI getters/setters exposed. Is there a library out there that could do this automatically?
We eventually dropped GWT and went with a Spring MVC/jQuery solution, but I did find the protostuff library which looked like it could do most of what I was looking for.

Getting JSON from Jersey with circular dependencies

I am writing a service that uses Jersey and JAXB. My classes are annotated with #XMLRootElement, #XMLElement, etc. I have a circular dependency between two classes, so I have annotated the circular dependent property with #XMLTransient. So when I call my service I get xml as the default, which works perfectly. However, when I try using JSON, I get repeated lines like:
{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere","entries":[{"points":0,"wins":0,"losses":0,"ties":0,"leaderboard":{"name":"dere"," ... etc.
So it seems there is a problem with circular dependencies when I am using JSON. I would like to avoid the circular dependent item from showing up in the JSON output, like it is done in XML (because of the #XMLTransient annotation).
Can anyone provide any insight on how I would be able to achieve this?
Use #JsonIgnore instead of #XmlTransient to break the circular dependency.
I had a similar problem as you and this did the trick for me.
It's probably worth checking out Kris Zyp's JSON Referencing proposal. It was invented specifically to handle multiple references and circular references in JSON data.
(Note: Despite the article title, Dojo isn't required. The original proposal was on json.com, but that site is inaccessible to me at the moment.)
How you would implement this technique in Jersey is, unfortunately, an exercise left to the reader.