Skip to content

fix(Utils): 处理配置/模板文件中的 UTF-8 BOM - #141

Merged
Dituon merged 1 commit into
Dituon:devfrom
KaRuichin:dev
Oct 13, 2026
Merged

Dituon merged 1 commit into
Dituon:devfrom
KaRuichin:dev

Conversation

@KaRuichin

Copy link
Copy Markdown
Contributor

问题描述

ganma 模板的 template.json 是以 UTF-8 with BOM 格式保存的,这导致在启动时的加载模板过程中会输出以下报错:

TIME INFO/Petpet-Onebot: Petpet 正在重载数据...
TIME WARN/TemplateManger: Failed to load template ganma
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 0: Expected start of the object '{', but had '' instead at path: $
JSON input: {
  "type": "image",
  "metad.....
        at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
        at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:598)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.fail$default(AbstractJsonLexer.kt:596)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.fail$kotlinx_serialization_json(AbstractJsonLexer.kt:233)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.fail$kotlinx_serialization_json$default(AbstractJsonLexer.kt:228)
        at kotlinx.serialization.json.internal.AbstractJsonLexer.unexpectedToken(AbstractJsonLexer.kt:225)
        at kotlinx.serialization.json.internal.StringJsonLexer.consumeNextToken(StringJsonLexer.kt:74)
        at kotlinx.serialization.json.internal.StreamingJsonDecoder.beginStructure(StreamingJsonDecoder.kt:102)
        at moe.dituon.petpet.template.PetpetTemplate$$serializer.deserialize(PetpetTemplate.kt:35)
        at moe.dituon.petpet.template.PetpetTemplate$$serializer.deserialize(PetpetTemplate.kt:35)
        at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:69)
        at kotlinx.serialization.json.Json.decodeFromString(Json.kt:107)
        at moe.dituon.petpet.template.PetpetTemplate$Companion.fromJson(PetpetTemplate.kt:87)
        at moe.dituon.petpet.template.PetpetTemplate$Companion.fromJsonFile(PetpetTemplate.kt:95)
        at moe.dituon.petpet.template.PetpetTemplate.fromJsonFile(PetpetTemplate.kt)
        at moe.dituon.petpet.service.TemplateManger.addTemplate(TemplateManger.java:115)
        at moe.dituon.petpet.service.TemplateManger.addTemplates(TemplateManger.java:76)
        at moe.dituon.petpet.service.TemplateManger.addTemplates(TemplateManger.java:70)
        at moe.dituon.petpet.service.UpdatableBaseService.addTemplates(UpdatableBaseService.java:16)
        at moe.dituon.petpet.bot.qq.onebot.MainKt.main$loadService(Main.kt:66)
        at moe.dituon.petpet.bot.qq.onebot.MainKt.access$main$loadService(Main.kt:1)
        at moe.dituon.petpet.bot.qq.onebot.MainKt$main$2.invokeSuspend(Main.kt:94)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

解决方案

很抱歉由于我的Java技术还不是很好,所以修复过程主要是经过 AI 的协助。
以下AI对本次修复的描述:

新增 trimBom 与 readTextWithoutBom 工具方法,并在读取
配置、模板及远程索引文件时去除 BOM,避免部分编辑器
(如 Windows 记事本)保存文件时写入的 BOM 导致 JSON
解析抛出 JsonDecodingException。

修复测试

我尝试构建并运行之后,数据文件文件夹不变,没有再次出现报错:

2026-10-12 15:25:42 INFO/Petpet-Onebot: ▌ 已连接到服务器 ┈━═☆
2026-10-12 15:25:42 INFO/BaseHttpServer: HTTP 图像服务器已在端口 2233 上启动
2026-10-12 15:25:43 INFO/FontManager: Font register success: Aller-Bold.ttf, Handel-Gothic.ttf, MiSans-Bold.ttf
2026-10-12 15:25:43 WARN/QQBotService: 无法找到默认模板, 将使用默认 forward_text 回复方案
2026-10-12 15:25:43 INFO/Petpet-Onebot:

    ██████╗ ███████╗████████╗██████╗ ███████╗████████╗
    ██╔══██╗██╔════╝╚══██╔══╝██╔══██╗██╔════╝╚══██╔══╝
    ██████╔╝█████╗     ██║   ██████╔╝█████╗     ██║
    ██╔═══╝ ██╔══╝     ██║   ██╔═══╝ ██╔══╝     ██║
    ██║     ███████╗   ██║   ██║     ███████╗   ██║
    ╚═╝     ╚══════╝   ╚═╝   ╚═╝     ╚══════╝   ╚═╝   v1.0.0-beta3

2026-10-12 15:25:43 INFO/Petpet-Onebot: Petpet Onebot 客户端启动成功:
2026-10-12 15:25:43 INFO/Petpet-Onebot: 已加载 150 模板; 随机表列包含 101 模板;
2026-10-12 15:25:43 INFO/Petpet-Onebot: 已注册 2 脚本; 默认模板为 null;
2026-10-12 15:25:43 INFO/Petpet-Onebot: 已加载 331 字体; 默认字体为 MiSans;
2026-10-12 15:25:43 INFO/Petpet-Onebot: Petpet Onebot 客户端启动完毕, 发送 pet 以触发默认模板...

新增 `trimBom` 与 `readTextWithoutBom` 工具方法,并在读取
配置、模板及远程索引文件时去除 BOM,避免部分编辑器
(如 Windows 记事本)保存文件时写入的 BOM 导致 JSON
解析抛出 JsonDecodingException。
Copilot AI balanced review requested due to automatic review settings October 12, 2026 07:28
@vercel

vercel Bot commented Oct 12, 2026

Copy link
Copy Markdown

@KaRuichin is attempting to deploy a commit to the dituon's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Dituon
Dituon merged commit 77c8f34 into Dituon:dev Oct 13, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants