I'm saving my JSONObject to disk and then when I try to load it back to JSONObject, I get the following exception on this line of the loadSavedScheduleFromDisk method:
JSONObject jsonObject = gson.fromJson(reader, JSONObject.class);
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 74040 path $.nameValuePairs..values[32].values[2].nameValuePairs.disciplineVersionId
Here is my code:
private void loadSavedScheduleFromDisk(Activity act) {
try {
File file = new File(((Context)act).getCacheDir()+"/"+SCHED_CACHE_FILENAME);
Gson gson = new Gson();
InputStreamReader freader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
JsonReader reader = new JsonReader(freader);//new FileReader(file));
reader.setLenient(true);
JSONObject jsonObject = gson.fromJson(reader, JSONObject.class);
parseSchedule(jsonObject);
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveScheduleToDisk(Activity act, JSONObject jsonObject )
{
try {
File file = new File(((Context)act).getCacheDir()+"/"+SCHED_CACHE_FILENAME);
//Writer writer = new FileWriter(file);
Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8"));
Gson gson = new GsonBuilder().create();
gson.toJson(jsonObject, writer);
} catch (Exception e) {
e.printStackTrace();
}
}
It is quite symmetrical and I can't understand why it doesn't work - I think if I use API to save my JSONObject to disk and it saves OK, then how come I can't load same data back?