Jackson to include 'class' field when serializing object to json - json

I need my Grails app to communicate (JSON) with client that uses Jackson. By default Grails include 'class' in the generated json. On the other hand, default Jackson's ObjectMapper doesn't recognize 'class', it throws error.
I've found solution, by adding #JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="class") on each POJO classes that are being used.
But, I don't prefer that approach. Because now my POJO will have dependency to Jackson's jar. Is there another easy alternative?
I was trying to figure out from objectMapper.getSerializationConfig(). ..., but still couldn't found the alternative to replace that annotation.
Thanks

Are you using the type info for polymorphic instantiation or are you ignoring that information? You could probably use instead #JsonIgnoreProperties({"class"}) on your classes.
If you really don't want to add annotation to your classes, you can use the mix-in mechanism. Although it is much more easier to add the annotation directly to your classes, if you control the classes of course.
mapper.addMixInAnnotations(YourClass.class, TypeInfoMixIn.class);
mapper.addMixInAnnotations(YourSecondClass.class, TypeInfoMixIn.class);
// etc.
#JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="class")
public class TypeInfoMixIn {}

Related

Is it possible to configure Jackson ObjectMapper to always use #JsonIdentityInfo

I understand that I can control the behavior of Jackson's ObjectMapper to include information about the identity of objects using the #JsonIdentityInfo annotation on a class.
Is it possible to achieve this by configuring the ObjectMapper itself to include type identity information for every object?
Is there a reason why the ObjectMapper should not be configured in this way (i.e. if there is no way to do this, is there a good reason for that)?

Is there a way to generate #jsonignore annotation for schema generated via avro?

Suppose I generate a domain object with several properties.I want to generate #jsonignore annotation for one of the object properties in the class defn.
I don't think you can directly add an additional annotation, but you can add a MixIn to your ObjectMapper.
See this answer for details: https://stackoverflow.com/a/56749899/1115279

Configure Jackson Mapper from Xml

I need to generate Json from the domain object. I can't add annotation in the domain classes.
Using Mixin is not a option because I have to ignore a lot of properties.
My approach was create a DTO object with the properties that I need. And populate the dto using dozer and then generate Json from the dto with jackson. It looks too much.
I would like to know if is posible to configure Jackson from xml, in order to generate json with the properties mapped in the xml, so it would not be necessary to use dto and dozer.
No. Jackson does not support external configuration files.
But you don't explain how or why you would use Dozer, or DTO. Why not just add properties you care about in a Map, and serialize that as JSON? Then you can use whatever mechanism you want to build/trim that Map.
Jackson can also convert values, so to create full Map with everything from another object, you can do:
Map<String,Object> map = objectMapper.convertValue(someBean, Map.class);
and maybe then only retain properties you want.

Jackson JSON library how to disable serialization of a given type

I am using JACKSON 1.6.3 version. I have one class which has a reference to itself and JACKSON serializer is failing with complains about "circular references".
I would like to disable serialization of this class. This is a third party class and I can not add any annotation to it, I am wondering if there is a way to disable this type of object being serialized. I am ok if the serializer ignores the entire object.
I fixed the issue by creating a custom JsonSerializer<T> for the type and registered it with ObjectMapper
mapper = new ObjectMapper();
CustomSerializerFactory factory = new CustomSerializerFactory();
factory.addSpecificMapping(<Type to be handled>, <Custom Serializer>);
mapper.setSerializerFactory(factory);
This can also be done using JacksonMixInAnnotations.
See the accepted answer on How can I tell jackson to ignore a property for which I don't have control over the source code?

JAXB A cycle is detected in the object graph

I want to convert my pojo to json with JAXB, my pojo have one to many relation, and when i convert my pojo to json, JAXB generate error "A cycle is detected in the object graph. This will cause infinitely deep XML".
I read from web that, this problem can be solved with help from #XmlID and #XmlIDREF, but there is one problem, my Id attribute is not String type but Long. and as far as i know #XmlID can be used only with String property.
Other web suggest using eclipselink MOXy, but MOXy cannot generate json.
As you mentioned in your question EclipseLink MOXy (I'm the tech lead) has the #XmlInverseReference annotation to solve the problem of bidirectional relationships. As of EclipseLink 2.4 MOXy can produce/consume JSON.
For More Information
http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
You have a cyclic reference problem in your definition.
Try putting #XmlTransient above the problematic definition.
Also, about XmlID and string type, see http://markmail.org/message/up6vrzjixxrvy5th.
The JAXB specification requires that the property marked with #XmlID be a String property.
MOXy impl allows to use long.
One hack to keep using full JAXB compliant implementation would be to duplicate your id in a String field (before serialising)
Don't know so much about JAXB but XStream makes you able to use different modes and some of these modes will give references to the xpath address (absolute or relative) of an element in your xml, if these elements are already displayed.
(And you can do Json with XStream)
I faced similar problem when I wanted to convert my POJO to JSON with JaxRS. The MoxyJsonProvider is the default option of eclipselink but it fails to parse the JSOG (where cycles exists in the JSON structure). Jackson Jaxb Provider does this better with ObjectMapper.
I have elaborated in this answer below, about how to invoke Jackson Provider instead of Moxy. You will need jackson packages in your pom xml.
https://stackoverflow.com/a/60319306/5076414