0

I am trying to deserialize a JSON with all the fields set lower-case. The problem is: my POJO object has attributes set as camel-case, and when I try to deserialize with Gson.fromJson my camel-case attributes are not set.

Example JSON:

[ { "idpojo": 1, "namepojo": "test" } ] 

POJO Class:

public class Pojo { private Integer idPojo; private String namePojo; //constructors, getters and setters } 

Deserialization code:

List<T> objects = new ArrayList<>(); // At this point I only have an variable with a reference to a classe. Let's assume it is in fact a reference to Pojo class Class VARIABLE_WITH_REFERENCE_TO_A_CLASS = Pojo.class; Class pojoReference = VARIABLE_WITH_REFERENCE_TO_A_CLASS; String json = EXAMPLE_JSON_HERE; JSONArray jsonArray = new JSONArray(json); int len = jsonArray.length(); for (int i = 0; i < len; i++) { objects.add((T) new Gson().fromJson(jsonArray.get(i).toString(), pojoReference))); } 

The reason I am deserializing the JSON to a list of a generic class instead of a list of Pojo class is because at this point of my code I don't know what Class I should create my collection.

This code works fine. The problem is: the camel-case attributes from the Pojo class are not set after the deserialization.

What I have tried so far has been using the @SerializedName annotation on the fields, and also creating custom deserializers, but neither do the trick for me, because I really don't want/can write specific code for deserialization of objects.

Question:

Using generic, how can I deserialize a JSON object with lower-case attributes to a Java class (like Pojo.class) with camel-case attributes?

2 Answers 2

1

Documentation of fromJson() method says that the type of object you are deserializing the JSON into cannot be generic. You can specify a type and use the @SerializedName attribute like you mentioned to overcome your problem of mismatched casing, but only if you know the type of object you are creating from the JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input @Tyler, but I said, this is not a option for me.
1

Solved the problem using Jackson instead of Gson for deserialization.

Jackson must be version 2.5 or superior, also one must configure the mapper to accept caseinsensitive attributes.

New working code:

ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); List<T> objects = new ArrayList<>(); // At this point I only have an variable with a reference to a classe. Let's assume it is in fact a reference to Pojo class Class VARIABLE_WITH_REFERENCE_TO_A_CLASS = Pojo.class; Class pojoReference = VARIABLE_WITH_REFERENCE_TO_A_CLASS; String json = EXAMPLE_JSON_HERE; JSONArray jsonArray = new JSONArray(json); int len = jsonArray.length(); for (int i = 0; i < len; i++) { objects.add(mapper.readValue(new JSONObject(json).toString(), pojoReference)); } 

Thanks to everyone for your time!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.