Fixed potential charset bug again.

These may cause annoying garbled characters in the future so that I fixed them as bugs.
This commit is contained in:
SpaceTeleport 2022-03-26 11:51:30 +08:00
parent 7dc316e7e4
commit 0568510a88

View file

@ -30,7 +30,7 @@ public class JsonFactory {
public static JsonObject getJsonObject(InputStream stream) { public static JsonObject getJsonObject(InputStream stream) {
try { try {
Reader reader = new InputStreamReader(stream); Reader reader = new InputStreamReader(stream,StandardCharsets.UTF_8);
JsonElement json = loadJson(reader); JsonElement json = loadJson(reader);
if (json != null && json.isJsonObject()) { if (json != null && json.isJsonObject()) {
JsonObject jsonObject = json.getAsJsonObject(); JsonObject jsonObject = json.getAsJsonObject();
@ -83,7 +83,7 @@ public class JsonFactory {
public static JsonElement loadJson(File jsonFile) { public static JsonElement loadJson(File jsonFile) {
if (jsonFile.exists()) { if (jsonFile.exists()) {
try (Reader reader = new FileReader(jsonFile)) { try (Reader reader = new FileReader(jsonFile, StandardCharsets.UTF_8)) {
return loadJson(reader); return loadJson(reader);
} }
catch (Exception ex) { catch (Exception ex) {
@ -98,7 +98,7 @@ public class JsonFactory {
} }
public static void storeJson(File jsonFile, JsonElement jsonObject) { public static void storeJson(File jsonFile, JsonElement jsonObject) {
try (FileWriter writer = new FileWriter(jsonFile)) { try (FileWriter writer = new FileWriter(jsonFile,StandardCharsets.UTF_8)) {
String json = GSON.toJson(jsonObject); String json = GSON.toJson(jsonObject);
writer.write(json); writer.write(json);
writer.flush(); writer.flush();
@ -109,7 +109,7 @@ public class JsonFactory {
} }
public static void storeJson(OutputStream outStream, JsonElement jsonObject) { public static void storeJson(OutputStream outStream, JsonElement jsonObject) {
OutputStreamWriter writer = new OutputStreamWriter(outStream); OutputStreamWriter writer = new OutputStreamWriter(outStream,StandardCharsets.UTF_8);
GSON.toJson(jsonObject, writer); GSON.toJson(jsonObject, writer);
try { try {
writer.flush(); writer.flush();