Changing JSON Provider (Spring / Spring-Boot) - json

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)?

Related

Infinite loop with Johnzon JSON serialization

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

Is elemental json is not compatible with google gson?

My application's vaadin version is upgraded from 7.3.6 to 7.4.6 and my Json code wherever I was using the GSON is starting to break.
I did googling for the same I found the similar issue mentioned in this stackOverflowPost
So should I conclude that elemental.json is not compatible with GSON and org.json was compatible with GSON.
GSON is not GWT compatible, which makes sense since it requires reflection to do its work. Likewise, org.json is meant to be used on a normal JVM.
On the other hand, GWT knows it is running in the browser, so doesn't need to implement its own JSON parser, since the browser already has one. There are several ways to use JSON in GWT, and GSON, org.json, and the built-in JSON parser of the browser all speak the same JSON.
All are compatible with each other, though you can't simply use elemental on the server or GSON on the client, or reuse the same server types on the client.
What specifically is 'starting to break'? What errors do you get, and what does the data that you are trying to send look like?
(Also worth noting, some of the 'json' in the linked post uses ' quotes for properties and strings, which is not legal JSON and should not work in the first place with any proper JSON parser.)

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.