FASTJSON2 Serializer for YACL

模组

· 作者 VesMaybeVesper

一个基于 FASTJSON2 的 YetAnotherConfigLib 配置序列化器

0 本站下载 0 关注 更新于 2 个月前

最新 1.0.8-neoforge+26.1 · 26.3 · NeoForge

一键开服(送7天)
基于 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 日

你是这个资源的作者?登录后可认领此资源。

评论 0

0.0
暂无评分
5 星
0
4 星
0
3 星
0
2 星
0
1 星
0
我的评分

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源