Force Spring to use Jackson - json

We are using spring 4.3. Inside the restController we're obtaining a HashMap for the JSON payload, and then explicitly converting it into specific pojos, since the pojos have to retrieved at runtime. The problem we're facing is Spring 4.1 started using GSON instead of Jackson, now instead of casting to Hashmap GSON is converting our json to LinkedTreeMap. Is there anyway we can force GSON to use HASHMAP instead of LinkedTreeMap or force spring framework to use Jackson for serialization instead of GSON?
Moreover when does GSON uses LinkedTreeMap and when Hashmap?
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap
Thanks

Related

Which Jackson API helps to convert Scala object into JSON object. I have used it for Java to JSON conversion

I want to use Jackson API to convert scala object to json. I have used the Jackson API 1.9.13 (jackson-core-asl, jackson-mapper-asl jars) to convert java object to json and vice versa. However I am not able to find jars to convert from scala object to json. Does any one have idea about this? Thanks.
I created a Java class and wrote a function to build JSON using Jackson API 1.9.13. The function returns the Json String.
Then created the object in Scala and called the Java-To-Json conversion function in the Java class. It worked just fine.

flexjson deserialization in play framework

I am trying to use flexjson in a playframework java project. But I am unable to deserialize json string to object. I am getting Could not load class exception while trying to deserialize caused by class not fount exception. I am using google guice for dependency injection does it have any thing to do with this exception.
Resolved after upgrading the lib to 3.1

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

How do I make Jackson's build() method pretty-print its JSON output?

Merged with When using Spring MVC for REST, how do you enable Jackson to pretty-print rendered JSON?.
I use Spring, Jersey and Jackson to provide an API that produces JSON.
My #Component has a #Get method that returns Response.ok(entity).build().
That output is very compact. How do I make that output pretty / formatted?

Gson serializing Spring beans

I am using Gson 1.6 and Spring Framework 3.0 for a Java web app on WebSphere 6.1. I have some Spring beans for which the actual instance is a CGLIB proxy. When I attempt to serialize these beans via Gson, the non-primitive properties of the class are not serialized. Instead I get something like:
{
"CGLIB$BOUND":true,
"CGLIB$CONSTRUCTED":true,
"booleanProperty":true,
"anotherBooleanProperty":true,
}
where I was expecting something more like
{
"stringProperty":"stringValue"
"integerObjectProperty":17,
"booleanProperty":true,
"anotherBooleanProperty":true,
}
When I serialize a non-proxied POJO, the output is exactly as I'd expect. How can I get Gson to generate the output I expect?
I'd say your problem is the result of a bad practice.
Spring Beans are usually defined by behaviour, not state. And you should only serialize Classes that have State, not behaviour.
Refactor your code, transfer the state from the Beans to Value Objects, and serialize those.
I would consider trying out another JSON processor, Jackson (http://jackson.codehaus.org), since it has some support for dealing with cglib proxied objects. And Spring supports Jackson so you have less code to write, compared to gson-based version.