
YACL 的 FASTJSON2 序列化器
使用FASTJSON2的YetAnotherConfigLib配置序列化器
使用说明
如版本页面所述,将其作为依赖项添加*
// >= 1.21.11
public static ConfigClassHandler<Config> HANDLER = ConfigClassHandler.createBuilder(Config.class)
.id(Identifier.fromNamespaceAndPath(MOD_ID, "config"))
.serializer(config -> FastJsonConfigSerializerBuilder.create(config)
.setPath(YACLPlatform.getConfigDir().resolve("modid.json"))
.build())
.build();
// <= 1.21.10
public static ConfigClassHandler<Config> HANDLER = ConfigClassHandler.createBuilder(Config.class)
.id(ResourceLoccation.fromNamespaceAndPath(MOD_ID, "config"))
.serializer(config -> FastJsonConfigSerializerBuilder.create(config)
.setPath(YACLPlatform.getConfigDir().resolve("modid.json"))
.build())
.build();JSON5
FastJSON2 不提供对 JSON5 的任何支持。如果你出于任何原因需要/想要 JSON5,建议继续使用 YACL 内置的 Gson 序列化器。如果你正从 JSON5 配置文件迁移回常规 JSON,这里有一个示例,展示了如何从我的模组 Am I Up To Date? 中自动迁移用户配置:
// 将文件与字段名称替换为你模组的
public class ConfigMigration {
public static void migrate(){
try {
if (Files.exists(Path.of(YACLPlatform.getConfigDir() + "/aiutd.json5"))){
AIUTD.LOG.info("Pre-2.5.0 config found. Migrating...!");
String JSONContent = Files.readString(Path.of(YACLPlatform.getConfigDir() + "/aiutd.json5"));
JSONObject oldConfig = JSON.parseObject(JSONContent, JSONReader.Feature.AllowUnQuotedFieldNames);
Config.HANDLER.load();
Config.menuAlert = Boolean.parseBoolean(oldConfig.getString("menuAlert"));
Config.chatAlert = Boolean.parseBoolean(oldConfig.getString("chatAlert"));
Config.multiLoaderBool = Boolean.parseBoolean(oldConfig.getString("multiLoaderBool"));
Config.multiLoader = Config.LoaderEnum.valueOf(oldConfig.getString("multiLoader"));
Config.localVersion = oldConfig.getString("localVersion");
Config.modpackId = oldConfig.getString("versionAPI");
Config.multiVersion = Boolean.parseBoolean(oldConfig.getString("multiVersion"));
Config.useModpackName = Boolean.parseBoolean(oldConfig.getString("useModpackName"));
Config.modpackName = oldConfig.getString("modpackName");
Config.useCustomMessage = Boolean.parseBoolean(oldConfig.getString("useCustomMessage"));
Config.customMessage = oldConfig.getString("customMessage");
Config.linkChangelog = Boolean.parseBoolean(oldConfig.getString("linkChangelog"));
Config.ignoreColor = Config.colorEnum.valueOf(oldConfig.getString("ignoreColor"));
Config.messageColor = Config.colorEnum.valueOf(oldConfig.getString("messageColor"));
Config.changelogColor = Config.colorEnum.valueOf(oldConfig.getString("changelogColor"));
Config.HANDLER.save();
AIUTD.LOG.info("Config migrated, please double check it for accuracy!");
Files.deleteIfExists(Path.of(YACLPlatform.getConfigDir() + "/aiutd.json5"));
AIUTD.LOG.info("Old config has been deleted");
}
} catch (IOException e) {
AIUTD.LOG.error("Failed to migrate config! Please do so manually!", e);
}
try {
if (Files.exists(Path.of(YACLPlatform.getConfigDir() + "/aiutd-enduser.json5"))){
AIUTD.LOG.info("Pre-2.5.0 end user config found. Migrating...!");
String JSONContent = Files.readString(Path.of(YACLPlatform.getConfigDir() + "/aiutd-enduser.json5"));
JSONObject oldUserConfig = JSON.parseObject(JSONContent, JSONReader.Feature.AllowUnQuotedFieldNames);
EndUserConfig.USERCONFIG.load();
EndUserConfig.shouldIgnore = Boolean.parseBoolean(oldUserConfig.getString("shouldIgnore"));
AIUTD.LOG.info("End user config migrated, please double check it for accuracy!");
Files.deleteIfExists(Path.of(YACLPlatform.getConfigDir() + "/aiutd.json5"));
AIUTD.LOG.info("Old end user config has been deleted");
}
} catch (IOException e) {
AIUTD.LOG.error("Failed to migrate end user config! Please do so manually!", e);
}
}
}常见问题解答
- 这比使用内置的 Gson 序列化器更快吗?
- 简而言之,是的。详细来说,基于纯库性能,是的。实际上,这很难说,很可能取决于配置的大小。在单个模组中使用时,它会产生显著的差异吗?不明显;如果有多个模组使用它,你可能会开始看到更实在的效果。
- 如果对性能提升不大,为什么还要做这个?
- 不知道,因为我可以?自由意志之类的。
- WAST 在 3/4 的测试中得分更高,为什么不用它?†
- 因为我已熟悉 FastJSON,无需学习新东西就能制作它。如果某天我需要(或想要)学习 WAST,也许到时会为它做一些东西。
*在我设置另一个 Maven 仓库之前
†截至 2026 年 6 月 28 日
正在加载版本记录…
正在加载评论…
评论在新手盒子客户端中发表,这里同步展示。