Home > OS >  Why is my LinkedHashMap being incorrectly converted to JSON and saved in SharedPreferences?
Why is my LinkedHashMap being incorrectly converted to JSON and saved in SharedPreferences?

Time:12-16

I'm trying to save an object in SharedPreferences but first converting the object to JSON as shown below. However, the object is being both incorrectly converted and saved.

    private void saveHobbySchedule() {
        String jsonString = new Gson().toJson(hobbySchedule);
        Log.e("Saving Hobby Schedule: ", jsonString);
        SharedPreferences sharedPreferences = getSharedPreferences("UserData", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("hobbySchedule", jsonString);
        editor.apply();
    }

The JSON looks like this:

E/Saving Hobby Schedule:: {"com.example.hobbie.ObjectModels.Hobby@a64126":["MONDAY @ 15:36","WEDNESDAY @ 15:36","THURSDAY @ 15:36"]}

This makes it impossible to save and retrieve the hobbySchedule variable.

If it helps, hobbySchedule is declared and initialized like this:

LinkedHashMap<Hobby, ArrayList<String>> hobbySchedule = new LinkedHashMap<>();

Here's the code for the Hobby class as well.

public class Hobby implements Serializable {
    private final String hobbyName;
    private final String hobbyEmoji;
    private final String hobbyCategory;
    private final String hobbyDescription;
    private final String hobbyShortDescription;
    private final String hobbyPracticeLocation;
    private final String hobbyImageURL;

    public Hobby() {
        this.hobbyName = "";
        this.hobbyEmoji = "";
        this.hobbyCategory = "";
        this.hobbyDescription = "";
        this.hobbyShortDescription = "";
        this.hobbyPracticeLocation = "";
        this.hobbyImageURL = "";
    }

    public Hobby(String hobbyName, String hobbyEmoji, String hobbyCategory, String hobbyDescription,
                 String hobbyShortDescription, String hobbyPracticeLocation, String hobbyImageURL) {
        this.hobbyName = hobbyName;
        this.hobbyEmoji = hobbyEmoji;
        this.hobbyCategory = hobbyCategory;
        this.hobbyDescription = hobbyDescription;
        this.hobbyShortDescription = hobbyShortDescription;
        this.hobbyPracticeLocation = hobbyPracticeLocation;
        this.hobbyImageURL = hobbyImageURL;
    }

Why is the name of the Hobby file address being converted into JSON and saved instead of the values? Please help, thank you!

CodePudding user response:

JSON only supports strings as JSON object property names. Gson therefore by default calls the toString() method to convert non-String Map keys to strings.

If instances of your Hobby class can easily be represented as string, you could override its toString() method. For deserialization you will however likely have to register a custom TypeAdapter.

Otherwise you can uses GsonBuilder.enableComplexMapKeySerialization() which will serialize your Map as JSON array.

  • Related