JSON JAXB Object type serialization - json

I'm trying to serialize JSON via JAXB using CXF. The problem is when the element to transform is a Object type.
#XmlElement(name = "Value")
protected Object value;
the result in JSON format is
"Value":{"#xsi.type":"xs:string","$":"LED"}}
Any idea how I do to transform in simple json format?
"Value":"LED" o r "Value":1

It looks like you have enabled BadgerFish notation for your Jettison converter (I am not aware about BadgerFish notation support in Jackson or MOXy).
If you use JBoss #BadgerFish annotation, just remove it. If you use JSONJAXBContext#BADGERFISH option setting, then change it to MAPPED or NATURAL (check documentation).

Set writesiType=false in JSONProvider Bean configuration

Related

How to convert Model to JSON

When I naively use Jackson to convert to JSON i receive this exception:
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.cayenne.access.DefaultDataRowStoreFactory and no properties discovered to create BeanSerializer
Edit: I'd like to do something like this:
ObjectContext context = cayenneRuntime.newContext();
List<User> users = ObjectSelect.query(User.class).select(context);
JsonObject json = Json.mapper.convertValue(obj, Map.class)
Are there any existing solutions? Thanks
Considering that in a general case Cayenne gives you not just objects, but a virtual graph of objects, serialization to JSON becomes a more quirky topic than it initially appears.
The short answer: you'd have manually build JSON for whatever subgraph of your object graph.
While not a direct answer, it may be worth mentioning that Agrest framework (ex. LinkRest) supports rule-based serialization of Cayenne object graphs to JSON. But it is not a standalone component. I.e. it will only work if you use it for your REST services.

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.

how to parse a json file via jackson?

I am using Jackson to parse a JSON object. It works perfectly but in bean class one of the member variables is dynamic. This means sometimes my json attribute returns an array of strings and sometimes same attribute return map<String,Object>.
How do I define Setter & Getter method in Beans for this attribute?
Use getter/setter that take/return Object, Jackson will by default determine the type from the json stream.
The only problem is that Jackson will deserialize to a list and not an array (you can however convert it to an array in the setter).
If you really want an array by default, you can have a look at Genson library http://code.google.com/p/genson/

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