I\'ve got integers in my json, and I do not want gson to convert them to doubles. The following does not work:
@Test
public void keepsIntsAsIs(){
String
This worked for me, I have a "specs" field which is a Map:
public class MyClass {
public Map specs;
}
Before the fix I was getting this output for a list of these objects:
{
"hits":{
"content":[
{
"specs":{
"fiscalHorsePower":4.0,
"nbOfDoors":5.0,
"consumption":4.3
}
}
]
}
}
fiscalHorsePower and nbOfDoors are integer.
Here is the fix I used, first create a new Adapter and a Factory:
public class CustomizedObjectTypeAdapter extends TypeAdapter
And then register the factory:
Gson gson = new GsonBuilder().registerTypeAdapterFactory(CustomizedObjectTypeAdapter.FACTORY);
and here is the result with the fix:
{
"hits":{
"content":[
{
"specs":{
"fiscalHorsePower":4,
"nbOfDoors":5,
"consumption":4.3
}
}
]
}
}