跳到正文

FASTJSON2 Serializer for YACL

为 YetAnotherConfigLib 提供基于 FASTJSON2 的配置序列化器

收藏

介绍

介绍由机器翻译自 Modrinth 原页面,仅供参考。
一个为YetAnotherConfigLib制作的序列化器,使用FASTJSON2实现。

Modrinth Downloads CurseForge Downloads

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?自动迁移用户的配置:

示例
// Replace file & field names with your mod's

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"));
                Config.HANDLER.save();

                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 在四项测试中的三项得分更高,为什么不用它?†
  • 因为我已经熟悉 FastJSON 了,做这个也不需要再学新东西。哪天我需要(或想要)学学 WAST,说不定到时候会为它做点什么。

*等我再配置一个 Maven 仓库之后

†截至 2026 年 6 月 28 日

常见问题

FASTJSON2 Serializer for YACL 支持哪些 Minecraft 版本?

FASTJSON2 Serializer for YACL 支持 Minecraft Java 版 1.20.1–26.3,共 17 个版本,可用的加载器:Fabric / Forge / NeoForge / Quilt。下载时注意选择和你游戏版本一致的文件。

FASTJSON2 Serializer for YACL 怎么安装?

先用 PCL2 或 HMCL 启动器安装对应版本的 Fabric / Forge / NeoForge / Quilt,再把下载的 .jar 文件放进游戏目录的 mods 文件夹,重新启动游戏即可。开启了版本隔离的,mods 文件夹在 versions/版本名 下面。运行环境:客户端和服务端都要装。

评论

登录后评论和评分

VesMaybeVesper 的其他作品

查看全部 →