将 myClass.ChatColor 对象与 .json 文件相互转换会导致不需要的前导 `Â` 字符

时间:2021-06-03 02:45:24

标签: java json gson minecraft bukkit

我将 ChatColor 对象存储在一个类对象中。使用 Gson api 成功将它们转换为 json 后,我希望能够从这个存储的 json 文件中实例化内存中的 ChatColor 对象。

使用下面的代码,我得到了使用 toString 方法返回 §6 的 ChatColor 对象。 他们应该返回相同但减去前导  字符

为了让这个问题更容易阅读,我将示例缩减为每个 Theme 对象一个 ChatColor 对象。

Theme.java

package core.data.objects;

import java.util.Map;
import net.md_5.bungee.api.ChatColor;

public class Theme {

    private ChatColor primary, secondary, tertiary, clear, faded, succeed, fail;

    public Theme(Map<String, ChatColor> thisMap) {
        this.primary = thisMap.get("primary");
    }

    public ChatColor getPrimary() { return primary; }

    public void setPrimary(ChatColor primary) { this.primary = primary; }
}

ThemeManager.java

package core.data;

import core.data.objects.Theme;

import java.io.*;
import java.util.Map;
import java.util.HashMap;

import com.google.gson.Gson;
import net.md_5.bungee.api.ChatColor;

public class ThemeManager {
    public static Theme currentTheme;

    public static void load() throws IOException {
        try {
            currentTheme = getThemeFromJSON(FileManager.defaultThemeFile);

            System.out.println(ChatColor.GOLD);
            System.out.println(currentTheme.getPrimary());

        } catch (Exception e) {
            currentTheme = createDefaultTheme();
            System.out.println("WARN getThemeFromJSON Exception");
            throw new IOException(e.getMessage());
        }
    }

    public static Theme createDefaultTheme() {
        Map<String, ChatColor> thisMap = new HashMap<>();

        thisMap.putIfAbsent("primary", ChatColor.GOLD);
        return new Theme(thisMap);
    }

    public static void writeThemeToJSON(Theme thisTheme, File thisFile) throws IOException {
        Gson gson = new Gson();

        Writer writer = new FileWriter(thisFile, false);
        gson.toJson(thisTheme, writer);
        writer.flush(); writer.close();
    }

    public static Theme getThemeFromJSON(File thisFile) throws FileNotFoundException {
        Gson gson = new Gson();
        Reader reader = new FileReader(thisFile);
        return gson.fromJson(reader, Theme.class);
    }
}

ThemeManager.load() 的控制台输出

[20:20:56 INFO]: §6
[20:20:56 INFO]: §6

保存的 .json 文件示例

{
  "primary": {
    "toString": "§6",
    "name": "gold",
    "ordinal": 6,
    "color": {
      "value": -22016,
      "falpha": 0.0
    }
  }
}

 不知从何而来!

1 个答案:

答案 0 :(得分:0)

解决方案来源:@dave_thompson_085

项目(和 .json 文件)以 UTF-8 编码,但我没有使用 UTF-8 编码读取文件。显然,这不是 FileReader 对象的默认编码。

通过改变这个:

public static Theme getThemeFromJSON(File thisFile) throws FileNotFoundException {
    Gson gson = new Gson();
    Reader reader = new FileReader(thisFile);
    return gson.fromJson(reader, Theme.class);
}

为此:

public static Theme getThemeFromJSON(File thisFile) throws FileNotFoundException {
    Gson gson = new Gson();
    Reader reader = new InputStreamReader (
                new FileInputStream (thisFile), StandardCharsets.UTF_8);
    return gson.fromJson(reader, Theme.class);
}

,来自 ThemeManager.load() 的 stdout 行现在都按预期显示 §6

但是,请注意,在使用 aThemeObject.getPrimary() 时,无法使用返回的 ChatColor 对象代替对 ChatColor.COLOR 的常规调用。

尝试这样做只会导致“null”出现在游戏中显示的文本中。

为了解决这个问题,我将 get 方法修改为:

public ChatColor getPrimary() {
        return ChatColor.getByChar(primary.toString().charAt(1));
    }

索引 1 处的字符是 getByChar 方法用于返回克隆 chatcolor 对象的颜色代码。

这将阻止在 theme.json 文件中使用自定义 falpha 值和自定义颜色值,但确保从调用 Theme.getPrimary() 生成的 ChatColor 对象始终可以用来代替对 ChatColor.COLOR 的常规调用