3

I am trying to store a list of objects into SharedPreferences, so therefore am using Gson to convert the list of objects into JSON and back again. However, when I store then retrieve the list of objects and the ListView's adapter is applied to the new List, I get the following error:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to appuccino.simplyscan.Objects.Folder
            at appuccino.simplyscan.Extra.DocumentAdapter.getView(DocumentAdapter.java:117)

where the error points at this line in the list's adapter:

Folder folder = folderList.get(position);

When storing the object list, I am using the following:

//folderList is a List<Folder> folderList = new ArrayList<>();
folderList.add(0, newFolder);
Gson gson = new Gson();
String newFoldersJson = gson.toJson(folderList);
PrefManager.putString(PrefManager.FOLDER_JSON, newFoldersJson);

where the last line simply stores the string into SharedPreferences. When retrieving the list from SharedPreferences, I am using the following:

 public static List<Folder> loadFolders(MainActivity main){
        //JSON containing a list of folder objects
        String foldersJSON = PrefManager.getString(PrefManager.FOLDER_JSON, "");
        if(!foldersJSON.isEmpty()){
            Gson gson = new Gson();
            List<Folder> folderList = gson.fromJson(foldersJSON, List.class);
            return folderList;
        }

        return new ArrayList<>();
    }

If it helps, here is how my Folder class is defined:

public class Folder {
    private String name;
    private List<String> docNameList;
    private transient List<Document> docList;

    public Folder(String n) {
        name = n;
        docList = new ArrayList<>();
        docNameList = new ArrayList<>();
    }

}
AggieDev
  • 5,015
  • 9
  • 26
  • 48
  • The error clearly shows it occurs in the `getView(...)` method of your `DocumentAdapter` class (specifically line 117). So why haven't you posted the full code for that method? – Squonk Jan 03 '15 at 16:49
  • An empty constructor for Folder may be needed. See: http://stackoverflow.com/a/4318722/2832027 – TTransmit Jan 03 '15 at 16:52
  • I have a similar issue right now, and I believe the problem is that when you use Gson to convert Json to Object.class like this `new Gson().fromJson(jsonstring, Object.class)` it won't create a java.lang.Object but a com.google.gson.internal.LinkedTreeMap. You probably serialize that LinkedTreeMap thinking it is a Java Object, which then fails during deserialization. [I'll investigate into this further now, if you found a solution in the meantime, let me know, please.] – Oliver Hausler Mar 03 '15 at 20:25

1 Answers1

-3

Not a answer to your question, but a suggestion. I am also trying to work with json in Android recently and researched a lot about which one to use jackson or gson. Seems like there are 2 advantages of jackson:

  1. Performance of jackson is better than gson.
  2. If you are using the same on server side then you get consistency.

Ref: [be-lazy-productive-android][1] http://java.dzone.com/articles/be-lazy-productive-android

nik
  • 17
  • 5