How can I prevent gson from converting integers to doubles

前端 未结 6 673
再見小時候
再見小時候 2020-12-05 07:37

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          


        
6条回答
  •  执念已碎
    2020-12-05 08:22

    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 {
    
    public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
        @SuppressWarnings("unchecked")
        @Override public  TypeAdapter create(Gson gson, TypeToken type) {
            if (Map.class.isAssignableFrom(type.getRawType())) {
                return (TypeAdapter) new CustomizedObjectTypeAdapter();
            }
            return null;
        }
    };
    
    private final TypeAdapter delegate = new Gson().getAdapter(Object.class);
    
    @Override
    public void write(JsonWriter out, Object value) throws IOException {
        delegate.write(out, value);
    }
    
    @Override
    public Object read(JsonReader in) throws IOException {
        JsonToken token = in.peek();
        switch (token) {
            case BEGIN_ARRAY:
                List list = new ArrayList();
                in.beginArray();
                while (in.hasNext()) {
                    list.add(read(in));
                }
                in.endArray();
                return list;
    
            case BEGIN_OBJECT:
                Map map = new LinkedTreeMap();
                in.beginObject();
                while (in.hasNext()) {
                    map.put(in.nextName(), read(in));
                }
                in.endObject();
                return map;
    
            case STRING:
                return in.nextString();
    
            case NUMBER:
                //return in.nextDouble();
                String n = in.nextString();
                if (n.indexOf('.') != -1) {
                    return Double.parseDouble(n);
                }
                return Long.parseLong(n);
    
            case BOOLEAN:
                return in.nextBoolean();
    
            case NULL:
                in.nextNull();
                return null;
    
            default:
                throw new IllegalStateException();
        }
    }
    }
    
    
    

    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
                }
             }
          ]
       }
    }
    

    提交回复
    热议问题