基于 FASTJSON2 开发的 YetAnotherConfigLib 序列化器。
FASTJSON2 Serializer for YACL (fastjson4yacl) 是一个库类模组,旨在允许其他模组使用 FastJSON2 进行配置文件的(反)序列化。它本身不执行任何操作,需要其他模组来调用 fastjson4yacl。
FASTJSON2 Serializer for YACL 也可在 Evening Star Lib 中找到。
加入 Fluxer 服务器以获取社区交流与模组支持。
用法
按照版本页面所述作为依赖添加*
// >= 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 配置文件。正在迁移...!");
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("配置已迁移,请仔细检查其准确性!");
Files.deleteIfExists(Path.of(YACLPlatform.getConfigDir() + "/aiutd.json5"));
AIUTD.LOG.info("旧配置已删除");
}
} catch (IOException e) {
AIUTD.LOG.error("配置迁移失败!请手动进行迁移!", e);
}
try {
if (Files.exists(Path.of(YACLPlatform.getConfigDir() + "/aiutd-enduser.json5"))){
AIUTD.LOG.info("发现 Pre-2.5.0 最终用户配置文件。正在迁移...!");
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"));
Config.HANDLER.save();
AIUTD.LOG.info("最终用户配置已迁移,请仔细检查其准确性!");
Files.deleteIfExists(Path.of(YACLPlatform.getConfigDir() + "/aiutd.json5"));
AIUTD.LOG.info("旧最终用户配置已删除");
}
} catch (IOException e) {
AIUTD.LOG.error("最终用户配置迁移失败!请手动进行迁移!", e);
}
}
}
常见问题
- 这比使用内置的 Gson 序列化器更快吗?
- 简而言之,是的。稍详细地说,仅从库的性能来看,确实更快。但在实际应用中情况会复杂一些,可能取决于具体配置的大小。在单个模组中使用它会有显著差异吗?不会很明显;如果你同时使用多个基于它的模组,可能会看到更明显的提升。
- 既然性能提升不大,为什么还要做这个?
- 谁知道呢,因为我能做?自由意志之类的。
- WAST 在 3/4 的测试中得分更高,为什么不使用它?†
- 因为我本来就熟悉 FastJSON,不需要学习新东西就能制作它。如果有一天我需要(或想要)学习 WAST,也许我会为它制作相应的支持。
*直到我搭建好另一个 maven 仓库
†截至 2026 年 6 月 28 日
你是这个资源的作者?登录后可认领此资源。
相似资源
Ixeris
decce6
缓冲的原始输入和线程化的事件轮询
优化
658
Woot Revived
pharuxtan
一个用于收集生物掉落物的多区块模组。
生物科技
349
玉Jade ?
Snownee
显示你正在注视的方块的信息。(适用于 Minecraft 1.16+ 的 Hwyla/Waila 分支)
实用前置
341
Fast Noise
omo50
原版世界生成优化模组
优化世界生成
242
物品管理器Just Enough Items (JEI)
mezz
查看物品与配方
前置实用
240
KubeJS
Lat
使用 JavaScript 编辑配方、添加自定义物品、编写世界事件!
前置实用
239
评论 0
登录后参与评分
还没有评论,来抢沙发吧