pluto icon indicating copy to clipboard operation
pluto copied to clipboard

An Int is converted to a double

Open ReggieLei opened this issue 3 years ago • 5 comments

com.plutolib.plugins:network:2.0.3 response -> body Int is converted to a double ....

ReggieLei avatar Aug 05 '22 09:08 ReggieLei

hey @ReggieLei, can you share more details & steps to reproduce the issue.

srtvprateek avatar Aug 05 '22 12:08 srtvprateek

Right back [{ "deviceId": "905436c790da4766a1c8e7d213f6a765", "deviceCode": "NIC1C2206022", "fourGOnline": 0, "wifiOnline": 1, "bluetoothOnline": 1, "wifiStrength": 18, "fourGStrength": 0, "pileStatus": -1, "isAdmin": 1, "isForbid": 1, }]

pluto catch back RESPONSE *** Body *** [ { "deviceId": "905436c790da4766a1c8e7d213f6a765", "deviceCode": "NIC1C2206022", "fourGOnline": 0.0, "wifiOnline": 1.0, "bluetoothOnline": 1.0, "wifiStrength": 18.0, "fourGStrength": 0.0, "pileStatus": -1.0, "isAdmin": 1.0, "isForbid": 1.0, } ]


report generated by Pluto (https://plutolib.com)

All Int is converted to double

ReggieLei avatar Aug 08 '22 10:08 ReggieLei

thanks for posting the issue @ReggieLei .

JSON is a type agnostic representation. As per official JSON docs

JSON does not have distinct types for integers and floating-point values. Therefore, the presence or absence of a decimal point is not enough to distinguish between integers and non-integers. https://json-schema.org/understanding-json-schema/reference/numeric.html

That said, we ll definitely look into it, if we can differentiate between Integer & Double.

srtvprateek avatar Aug 08 '22 21:08 srtvprateek

Maybe you can try (https://github.com/alibaba/fastjson2)

or extends to rewrite TypeAdapter for Gson public class MapTypeAdapter extends TypeAdapter<Object> {

private final TypeAdapter<Object> delegate = new Gson().getAdapter(Object.class);

@Override
public Object read(JsonReader in) throws IOException {
    JsonToken token = in.peek();
    switch (token) {
        case BEGIN_ARRAY:
            List<Object> list = new ArrayList<>();
            in.beginArray();
            while (in.hasNext()) {
                list.add(read(in));
            }
            in.endArray();
            return list;

        case BEGIN_OBJECT:
            Map<String, Object> 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:
            double dbNum = in.nextDouble();

            if (dbNum > Long.MAX_VALUE) {
                return String.valueOf(dbNum);
            }

            long lngNum = (long) dbNum;
            if (dbNum == lngNum) {
                return String.valueOf(lngNum);
            } else {
                return String.valueOf(dbNum);
            }

        case BOOLEAN:
            return in.nextBoolean();

        case NULL:
            in.nextNull();
            return null;

        default:
            throw new IllegalStateException();
    }
}

@Override
public void write(JsonWriter out, Object value) throws IOException {
    delegate.write(out,value);
}

} public T gsonToMap(String strJson) { Gson gson = new GsonBuilder() .registerTypeAdapter(new TypeToken<T>(){}.getType(),new MapTypeAdapter()).create(); return gson.fromJson(strJson, new TypeToken<T>() { }.getType()); }

String json = "{"identifier":"18111111111","opType":1,"platform":0}"; Map<String, Object> map = new MyType<Map<String, Object>>().gsonToMap(json);

ReggieLei avatar Aug 09 '22 03:08 ReggieLei

sure @ReggieLei, thanks for the suggestion, will look into this

srtvprateek avatar Aug 09 '22 05:08 srtvprateek