Dee Nix
Dee Nix

Reputation: 168

Gson HashMap<String, List<String>> to serialize and deserialize getting error

I want to store some data in string format and again retrive object from that string using gson in android.

My storing function is

public void storeData(HashMap<String, List<String>> data) {
    String d = new Gson().toJson(data);
    storeToXYZ(d);
}

Getting data function

public HashMap<String, List<String>> getData() {
    String d = getFromXYZ();
    // Assume not default data present
    if(!d.equals("")) {
        Type type = new TypeToken<HashMap<String, List<String>>>(){}.getType();
        return new Gson().fromJson(d, type);
    }
    return null;
}

I am getting error at this line in getData() function

return new Gson().fromJson(d, type);

Stacktrace

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap

Thanks in advance.

Upvotes: 0

Views: 1789

Answers (2)

Goltsev Eugene
Goltsev Eugene

Reputation: 3637

What do I have in my project:

1) private HashMap<String, File> imagesMap;<br>
2) sharedPreferencesEditor.putString(IMAGES_MAP_KEY, new Gson().toJson(imagesMap)).apply();<br>
3) imagesMap = new Gson().fromJson(imagesMapString, new TypeToken<HashMap<String, File>>(){}.getType());

It works well.

BTW, I use

compile 'com.google.code.gson:gson:2.8.1'

in dependencies.

Please check gson version, also please ensure, that you transfer exactly HashMap into storeData().


Okay, let's try another way:
I created new project, added gson in dependencies. And here is code of MainActivity:

package com.example.eugenegoltsev.hashmaptest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private HashMap<String, List<String>> data;
    private String jsonString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        storeData(data);
        HashMap<String, List<String>> data2 = getData();
    }

    private void initData() {
        data = new HashMap<>();

        List<String> list1 = new ArrayList<>();
        list1.add("l1 one");
        list1.add("l1 two");
        list1.add("l1 three");

        List<String> list2 = new ArrayList<>();
        list2.add("l2 one");
        list2.add("l2 two");
        list2.add("l2 three");

        data.put("First", list1);
        data.put("Second", list2);
    }

    public void storeData(HashMap<String, List<String>> data) {
        jsonString = new Gson().toJson(data);
    }

    public HashMap<String, List<String>> getData() {
        Type type = new TypeToken<HashMap<String, List<String>>>(){}.getType();
        return new Gson().fromJson(jsonString, type);
    }
}

getData() works well, just like it should.
So your problem is somewhere in other place.

You can create test HashMap, like in my code and test with it.

Upvotes: 0

Maxim Shoustin
Maxim Shoustin

Reputation: 77930

Try to change HashMap to Map:

Type type = new TypeToken<Map<String, List<String>>>(){}.getType();

Details:

From LinkedTreeMap.java:

LinkedTreeMap<K, V> extends AbstractMap<K, V>

The AbstractMap inherits from Map interface and not from HashMap

enter image description here

Upvotes: 1

Related Questions