Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ArrayList with Gson to String

I have an ArrayList which contains ArrayLists, each ArrayList in the Root list contains one ArrayList of Integers and one of Strings. I am converting it with Gson to a String to save it with SharedPreferences. But when I am reconverting it, Gson gives me 2.131558489E9 instead of the original int 2131558489. How can I fix this Problem? Best Regards.

Here is how I convert the ArrayList: levelPattern is the ArrayList

String levelPatternGson = new Gson().toJson(levelPattern);

And this is how I convert it back:

levelPattern = new Gson().fromJson(levelPatternGson, ArrayList.class);
like image 776
Chinaedu Onwukwe Avatar asked May 07 '16 17:05

Chinaedu Onwukwe


2 Answers

There is no difference in json standard between integers and doubles, there is only number type. That is why gson by default converts numbers to doubles if you don't give him what type you want.

Easy fix would be to use TypeToken and change data structure to multiple arrays or custom object (like in @totoro demo).

new Gson().fromJson(levelPatternGson, new TypeToken<List<Integer>>() {}.getType());

But you could also write custom List deserializer:

public static class ListDeserializerDoubleAsIntFix implements JsonDeserializer<List>{

    @Override  @SuppressWarnings("unchecked")
    public List deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        return (List) read(json);
    }

    public Object read(JsonElement in) {

        if(in.isJsonArray()){
            List<Object> list = new ArrayList<Object>();
            JsonArray arr = in.getAsJsonArray();
            for (JsonElement anArr : arr) {
                list.add(read(anArr));
            }
            return list;
        }else if(in.isJsonObject()){
            Map<String, Object> map = new LinkedTreeMap<String, Object>();
            JsonObject obj = in.getAsJsonObject();
            Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
            for(Map.Entry<String, JsonElement> entry: entitySet){
                map.put(entry.getKey(), read(entry.getValue()));
            }
            return map;
        }else if( in.isJsonPrimitive()){
            JsonPrimitive prim = in.getAsJsonPrimitive();
            if(prim.isBoolean()){
                return prim.getAsBoolean();
            }else if(prim.isString()){
                return prim.getAsString();
            }else if(prim.isNumber()){
                Number num = prim.getAsNumber();
                // here you can handle double int/long values
                // and return any type you want
                // this solution will transform 3.0 float to long values
                if(Math.ceil(num.doubleValue())  == num.longValue())
                    return num.longValue();
                else{
                    return num.doubleValue();
                }
            }
        }
        return null;
    }
}

and use it like this:

GsonBuilder builder=new GsonBuilder();
List<List> levelPattern = Arrays.asList(Arrays.asList(2131558489L, 2L, 3L), 
                                        Arrays.asList("one", "two", "three"));
String levelPatternGson = new Gson().toJson(levelPattern);
List levelPattern2 = new GsonBuilder()
        .registerTypeAdapter(List.class, new ListDeserializerDoubleAsIntFix())
        .create()
        .fromJson(levelPatternGson, List.class);

System.out.println(levelPattern2);

Json: [[2131558489,2,3],["one","two","three"]]

Output: [[2131558489, 2, 3], [one, two, three]]

like image 115
varren Avatar answered Oct 16 '22 05:10

varren


I am not sure I understand the question completely...

I am assuming your ArrayList is not using generics.

This solution is a generics version, using an Object to hold the two different typed ArrayLists.

class Test {

    static class Bar {
        private List<Integer> integers;
        private List<String> strings;
    }

    public static void main(String[] argv) {
        Type baseType = new TypeToken<List<Bar>>() {}.getType();
        List<Bar> foos = new ArrayList<>();

        Bar bar;

        bar = new Bar();
        bar.integers = Arrays.asList(1, 2, 3, 4);
        bar.strings = Arrays.asList("a", "b", "c", "d");
        foos.add(bar);

        bar = new Bar();
        bar.integers = Arrays.asList(5, 6, 7, 2131558489);
        bar.strings = Arrays.asList("e", "f", "g", "h");
        foos.add(bar);

        Gson gson = new Gson();
        String tmp = gson.toJson(foos, baseType);
        System.out.println(tmp);
        foos = gson.fromJson(tmp, baseType);
        System.out.print(foos.get(1).integers.get(3));
    }
}

Output

JSON: [{"integers":[1,2,3,4],"strings":["a","b","c","d"]},{"integers":[5,6,7,2131558489],"strings":["e","f","g","h"]}]

The Integer: 2131558489

like image 6
totoro Avatar answered Oct 16 '22 06:10

totoro