I have a class that takes in a String parameter. I then use sharedpreferences to preserve the String. To do this, I converted the input String using the gson.toJson method and then stored it into the sharedpreferences as a json. However, my getter method for the String requires the conversion of the json object of the String back to gson. The conversion works when there are no spaces in the initial input String. However, I get this error when there is a space within the initial input String
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 6 path $
Here is the class and parameter:
public City(String name)
{
this.name=name;
Gson gson=new Gson();
sharedPreferences=context.getSharedPreferences("alternate_db",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString(NAME_KEY,gson.toJson(this.name));
editor.apply();
}
Here is the getter method for the String:
public String getName()
{
Gson gson=new Gson();
Type type=new TypeToken<String>(){}.getType();
return gson.fromJson(sharedPreferences.getString(NAME_KEY,null),type);
}