how to parse a json file via jackson? - json

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/

Related

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.

Deserialize JSON containing an anonymous function using .net

I am getting a .net run-time error when attempting to desrialize a JSON string that contains an anonymous function:
Invalid JSON primitive: function.
JSON string itself looks like this:
{
Action: "fadeIn",
Callback: function(){doSomething();}
}
This makes me wonder if it is allowed to have anonymous functions in JSON strings that are to be serialized in .net. More specfically I can only use the .net frameworks's own JavaScriptSerializer class for deserialization. Can anyone confirm this, or have a solution?
JSON is a data representation protocol, and as such it can only be used to represent data, not behavior (which is what functions are). As your deserializer told you, what you have is not valid JSON (it's a valid JavaScript object, though, which causes some confusion). Check the JSON spec for more details on this format.
So to your question - no, you cannot deserialize JSON containing function, because it's not JSON in the first place.

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.

JSON JAXB Object type serialization

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

Json.NET serialization pre and post calls

I am using Json.NET to serialize an object graph. For each object that is serialized or deserialized, I want to call a method on that object before the serialization takes place. For e.g. all my objects implement an interface INotified with a method OnSerializing. I want OnSerializing to be called before the object is serialized.
Is there a way to achieve this without having to touch the Json.NET source code?
The latest version of Json.NET supports serialization callbacks.