i have a class name Rule and i am trying to write and read from GSON so i add Gson library (Gson-2.6.2) to my project. here is my code. but when i try to use them it returns an error( or exception). here is my code :
public void writeOnJSON(Rule rule) {
Gson gson = new Gson();
String json = gson.toJson(rule);
try {
FileWriter writer = new FileWriter(JSON_FILE);
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void readFromJSON() {
Gson gson = new Gson();
try {
RulesList.getRuleArrayList().clear();
System.out.println("Reading JSON from a file");
BufferedReader br = new BufferedReader(
new FileReader(JSON_FILE));
//convert the json string back to object
Rule rule = gson.fromJson(br, Rule.class);
RulesList.getRuleArrayList().add(rule);
} catch (IOException e) {
e.printStackTrace();
}
}
and here is the error enter image description here
Read more here: https://stackoverflow.com/questions/66273312/exception-occurd-while-writing-and-reading-from-gson
Content Attribution
This content was originally published by Mont at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.