综合技术

Converts ArrayList from custom objects to JSON using the Gson library, ClassCastExcep…

微信扫一扫,分享到朋友圈

Converts ArrayList from custom objects to JSON using the Gson library, ClassCastExcep…
0

I have used the Google’s GSON library to convert an ArrayList
of my custom objects to JSON in order to store it into SharedPreferences
. The class of the objects stored in the ArrayList is like this:

class CustomObject {
  String name;
  Map<String, Long>  topGrades;
  Map<String, Long> lowestGrades;

  CustomObject(String name, Map<String, Long>  topGrades, Map<String, Long>  lowestGrades) {
    this.name = name;
    this.lowestGrades = lowestGrades;
    this.topGrades = topGrades;
  }
}

I saved an ArrayList
of objects of CustomObject
to SharedPreferences
like:

List<CustomObject> dataList = new ArrayList<>();
//Populate the list with objects of type CustomObject
...
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(
                    "tests.dataSharedPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String dataListJson = gson.toJson(dataList);
editor.putString("tests.dataListJsonKey", dataListJson);
editor.commit();

The I retrieved the List
from the JSON like:

SharedPreferences sharedPreferences = getActivity().getSharedPreferences("tests.dataSharedPreferences", Context.MODE_PRIVATE);
            String dataJson = sharedPreferences.getString("tests.dataListJsonKey", "");

            Gson gson = new Gson();
            final List<CustomObject> dataList = gson.fromJson(dataJson, ArrayList.class);

SO far so good. But now in order to get the names of all CustomObjects in dataList above into a String array, in the following loop, I tried to retrieve a CustomObject from the dataList, But I got ClassCastException:

String[] namesArray = new String[dataList.size()];

        for (int x=0; x<namesArray.length; x++) {
            CustomObject customObject = dataList.get(x);//********ClassCastException********************************
            namesArray[x] = customObject.name;
        }

I got

06-18 12:28:11.689: E/AndroidRuntime(1536): Caused by: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to tests.CustomObject

The question is why and what is the solution?

Gson must know the type of object to convert to. That is, in order to serialize it must know the exact type. When you pass the type as ArrayList.class it does’nt provide complete information.

One thing you can do is create a wrapper class which contains your arraylist. Then provide that class as your second parameter.

class Wrapper{
    ArrayList<CustomObject> dataList;
    //constrctor
    public Wrapper(){
       //empty constructor  for Gson
    }
    //setter for dataList
    public setDataList(ArrayList<CustomObject> dataList){
        this.dataList = dataList;
    }
}

// try this for serializing and deserializing

Wrapper wrapper = new Wrapper();
//populate list
wrapper.setDataList(list);

//serialize
String dataListJson = gson.toJson(wrapper);

//deserialize
Gson gson = new Gson();
Wrapper dataWrapper = gson.fromJson(dataListJson, Wrapper.class);
}

阅读原文...


Hello, buddy!

Defined value MVC3 for text type datetime type in opera

上一篇

ES6-Proxy与数据劫持(12)

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
Converts ArrayList from custom objects to JSON using the Gson library&comma; ClassCastExcep…

长按储存图像,分享给朋友