UTF-16 的 Gson 反序列化问题
Gson Deserialization Issue With UTF-16
这行代码有问题:
try (OutputStreamWriter fileout = new OutputStreamWriter(new FileOutputStream(Paths.get(path.toString(), TAGS_FILE.toString()).toString()), "UTF-16")) {
fileout.write(gson.toJson(imageList, listType));
fileout.flush();
fileout.close();
}
我最初使用的是 UTF-8,它工作正常,加载正常,但必须更改为 UTF-16 以保留一些特殊字符。它仍然正确地写出文件,与 UTF-8 完全相同(除了完整的特殊字符),但是当它尝试将文件加载到另一个会话中时,我得到 "Expected BEGIN_ARRAY but was STRING..."
有办法解决这个问题吗?
此外,如果这有帮助:
private final Type listType = new TypeToken<TreeSet<MyClass>>(){}.getType();
TreeSet<MyClass> imageList;
更新:
private void move(File file, Path destination, boolean autoTag) {
String fileName = file.getName();
Matcher numberMatcher = leadingNumbersPattern.matcher(fileName);
// remove leading numbers
while (numberMatcher.find()) {
fileName = clean(fileName, leadingNumbersPattern);
}
Matcher artistMatcher = artistPattern.matcher(fileName);
Matcher newFileNameMatcher = newFileNamePattern.matcher(fileName);
if (artistMatcher.find() && newFileNameMatcher.find()) {
// set artist name
String artist = artistMatcher.group().substring(0, artistMatcher.group().length() - 1);
// set new picture name
String newFileName = newFileNameMatcher.group().substring(1);
Path newPath = Paths.get(destination.toString(), artist); // path to artist folder
new File(newPath.toString()).mkdirs(); // make artist folder
newPath = Paths.get(destination.toString(), artist, newFileName); // make path to new file location
try {
Files.move(file.toPath(), newPath, StandardCopyOption.REPLACE_EXISTING); // move file to new location
MyImage newImage = new MyImage(newPath.toString(), artist, newFileName);
改回 UTF-8 解决了这个问题。我不得不重新制作 json 文件;我想当我最初将所有内容转换为 UTF-8 时,泰语字符不知何故漏掉了。
编辑:
找到原因了!我用来反序列化文件的 load() 方法未设置为在 FileInputStream 上使用 UTF-8。添加这个完全解决了这个问题。
这行代码有问题:
try (OutputStreamWriter fileout = new OutputStreamWriter(new FileOutputStream(Paths.get(path.toString(), TAGS_FILE.toString()).toString()), "UTF-16")) {
fileout.write(gson.toJson(imageList, listType));
fileout.flush();
fileout.close();
}
我最初使用的是 UTF-8,它工作正常,加载正常,但必须更改为 UTF-16 以保留一些特殊字符。它仍然正确地写出文件,与 UTF-8 完全相同(除了完整的特殊字符),但是当它尝试将文件加载到另一个会话中时,我得到 "Expected BEGIN_ARRAY but was STRING..."
有办法解决这个问题吗?
此外,如果这有帮助:
private final Type listType = new TypeToken<TreeSet<MyClass>>(){}.getType();
TreeSet<MyClass> imageList;
更新:
private void move(File file, Path destination, boolean autoTag) {
String fileName = file.getName();
Matcher numberMatcher = leadingNumbersPattern.matcher(fileName);
// remove leading numbers
while (numberMatcher.find()) {
fileName = clean(fileName, leadingNumbersPattern);
}
Matcher artistMatcher = artistPattern.matcher(fileName);
Matcher newFileNameMatcher = newFileNamePattern.matcher(fileName);
if (artistMatcher.find() && newFileNameMatcher.find()) {
// set artist name
String artist = artistMatcher.group().substring(0, artistMatcher.group().length() - 1);
// set new picture name
String newFileName = newFileNameMatcher.group().substring(1);
Path newPath = Paths.get(destination.toString(), artist); // path to artist folder
new File(newPath.toString()).mkdirs(); // make artist folder
newPath = Paths.get(destination.toString(), artist, newFileName); // make path to new file location
try {
Files.move(file.toPath(), newPath, StandardCopyOption.REPLACE_EXISTING); // move file to new location
MyImage newImage = new MyImage(newPath.toString(), artist, newFileName);
改回 UTF-8 解决了这个问题。我不得不重新制作 json 文件;我想当我最初将所有内容转换为 UTF-8 时,泰语字符不知何故漏掉了。
编辑: 找到原因了!我用来反序列化文件的 load() 方法未设置为在 FileInputStream 上使用 UTF-8。添加这个完全解决了这个问题。