Configure Jackson Mapper from Xml - json

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.

Related

JsonConverter in Manatee Json

I am using Manatee Json for de-/serialization. I want to do use the similar functionality which is available in Netwonsoft where one can override JsonConverter Read and Write Json. Do we have an example on how to do that in Manatee?
Manatee.Json works a bit differently than Json.Net.
With Json.Net, you implement a reader or writer where you translate directly between the JSON and the object.
With Manatee.Json, you don't have to worry about the JSON so much. Instead you translate to a JsonValue.
You have a couple primary options on how you can do this:
If you own the object (you have the .cs file for it), the best approach is to have it implement IJsonSerializable. The serializer prioritizes for this.
If you don't own the object, you can create an ISerializer implementation to perform the translation.
The serialization docs give a lot more detail.

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.

Efficient way of parsing Jax-ws Restful Response

I need to parse the jax-ws rest response and I tried the following two ways of parsing the response.Both works good.But I am in need to know the best efficient way of implementation.Please provide me your view.
First Approach:
Use getEntity Object and get the response as Input Stream.
Using Jackson ObjectMapper readValue() -covert the inputstream to java
object.
Using getters and setters of nested java class get the response objects member values.
Second Approach:
Use getEntity Object and get the response as Input Stream and and
convert the Input Stream to String.
Using Google Json API,convert the string to json object.
Using Json parser and get the nested objects member values.
I would say the first approach is better for two reasons:
You don't go through the intermediate process of reading the response payload into String
The setter methods that are called during Jackson deserialization may perform validation on input and throw appropriate exceptions, so you do validation during deserialization.
Maybe not a general answer to this question but another variant of what you're describing under "First approach". I would start with a generic data structure and would only introduce an extra bean if necessary. I wouldn't use String to pass structured data around.
Use jackson to convert the JSON response to a
Map<String,Object> or JsonNode.
Advantage:
You don't need to implement a specialized bean class. Even a very simple bean can become unhandy over time (if format changes or new nested structures are added to the json response, etc.). It also introduces some kind of metaphor to your code which sometimes helps but also can be misleading.
Map<String,Object> is in the JDK and offers a good interface to access data. You don't have to change any interfaces even if the JSON format changes.
You can always pass your data in form of a Map<String,Object>
Disadvantage
Data Encapsulation. The map is a very close representation of the input data and therefore offers not same level of abstraction like a bean.

Spring MVC Jackson Json #JsonIgnore programmatically

I have a rest web service written in spring mvc and using jackson json for the output. To ignore an object while serialising to json, I know I can annotate the field or class with #Jsonignore. But I have an array of objects the I want to return and whether to ignore some objects from the final json output depends on some condition. For example: ignore if the object.hasTitle() == null.
What would be the best way to achieve this in spring mvc?
One solution whould be to provide a custom (de)serializer where you can remove/add fields on specific conditions.
Right way to write JSON deserializer in Spring or extend it

Handling Unknown JSON Properties with Jackson

For deserializing json with unknown field into an object there's #JsonAnySetter.
But what if I read such json into my object, modify some known fields and write it back to json?
The unknown properties will be lost.
How do I handle such cases? Is it possible to map an object or do I have to read the data into a JsonNode or Map?
Unmarshalling into a custom java class has its advantages and disadvantages. It's gives you nice static typing, but it's well, static. The javadoc for #JsonAnySetter suggests that it's similar to JAXB's #XmlAnyElement, but unlike #XmlAnyElement, the data objects don't contain naming information, so it's a one-way street.
if you need to handle dynamic JSON streams, then you need to bite the bullet and use Map or JsonNode.
It is now possible to use #JsonAnyGetter to provide a method which allows serialization of dynamic properties:
#JsonAnyGetter
public Map<String, String> getDynamicProperties() {
return dynamicProperties; // a field like this exists
}
There's this RFE for Jackson: http://jira.codehaus.org/browse/JACKSON-292 to add such a feature. Makes total sense when you think about it.