JSON files use default encoding, may be incompatible across platforms
ryankoppenhaver opened this issue ยท 2 comments
JsonIO.readFromFile
and .writeToFile
create FileReader
and FileWriter
objects to handle file I/O. These classes always use the default character encoding, which can vary based on the operating system or the user's general system settings.
This is mostly going to go unnoticed, as the most common encodings are compatible for common (basic ASCII) characters. One major exception is color formatting, which uses the "section sign" as an marker. This character may be encoded as A7
in "extended ASCII" encodings such as ISO8859-1, but is encoded as C2 A7
in UTF-8.
Here is one instance of this issue in the wild. I've also been able to reproduce it with the following steps:
- Run Minecraft with
-Dfile.encoding=iso8859-1
. - Make some quests with color formatting, and quit.
- Open
QuestDatabase.json
with a hex editor. Note theA2
bytes. - Re-run Minecraft with
-Dfile.encoding=utf-8
. - Open the quest list, and see that the color escapes are mangled.
- Open
QuestDatabase.json
with a hex editor again. Note theA2
bytes have been replaced byEF BF BD
(the "replacement character" sequence).
I'd suggest explicitly specifying UTF-8 for both input and output. Something like:
// FileReader fr = new FileReader(file);
InputStream is = new FileInputStream(file);
Reader r = new InputStreamReader(is, Charset.forName("UTF-8"));
...and similar for writing.
yes, "The Ferret Business" is having the exact issue described here, Thank you ryankoppenhaver, for figuring it out.