Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when reading from cached HashMap #4

Open
jaytj95 opened this issue Mar 15, 2017 · 1 comment
Open

Error when reading from cached HashMap #4

jaytj95 opened this issue Mar 15, 2017 · 1 comment

Comments

@jaytj95
Copy link

jaytj95 commented Mar 15, 2017

I'm getting a com.google.gson.internal.LinkedTreeMap cannot be cast to <CustomClass> error when reading a HashMap from cache.

Writing to file:

Map<String, Bus> buses = new HashMap<>();
//after adding data
CacheUtils.writeDataMapFile(CACHE_NAME, buses);

Reading from file:

buses = CacheUtils.readDataMapFile(CACHE_NAME);
Bus b = buses.get("Route 1"); //Error here
@jaytj95
Copy link
Author

jaytj95 commented Mar 15, 2017

Found a solution to my problem. Ended up storing the hashmap as a JSON string and then doing the Type conversion myself.

CacheUtils.writeFile(CACHE_NAME, new Gson().toJson(buses));
//.......
String busesStr = CacheUtils.readFile(CACHE_NAME);
Type type = new TypeToken<HashMap<String, Bus>>(){}.getType();
HashMap<String, Bus> buses = new Gson().fromJson(busesStr, type);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant