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?