Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

[WIP] fix: should encode defaults in websocket json #234

Closed
wants to merge 1 commit into from

Conversation

Mythologyli
Copy link
Contributor

@Mythologyli Mythologyli commented Feb 16, 2024

请勿合并!目前发现此 PR 会导致发送图片后 bot 断开与 nonebot2 的连接,原因待查

val GlobalJson = Json {
    ignoreUnknownKeys = true // 忽略未知key
    isLenient = true // 宽松模式
    allowSpecialFloatingPointValues = true // 允许特殊浮点数值(如NaN)
    encodeDefaults = false // 不编码默认值
    prettyPrint = false // 格式化输出
    coerceInputValues = true // 强制输入值
}

此设置中的 encodeDefaults = false 使得 0、空字符串不会被编码到 json 字符串中,导致配合 adapter-onebot 使用时出现一些错误

例如:

由于我对 OpenShamrock 了解不深,暂时不知更改此设置是否会导致某些副作用

@Mythologyli Mythologyli changed the title fix: should encode defaults in websocket json [WIP] fix: should encode defaults in websocket json Feb 16, 2024
@whitechi73
Copy link
Owner

将默认值设置为对应类型最小值,则可以编码数字0
将字符串设置为null-able即可编码空字符串

data class Test(
   val f1: Long = Long.MIN_VALUE, // f1为0的时候将编码f1在json中
   val f2: Long = 0, //f2为0,不会被编码
   val f3: String = "", //f3为空字符串时不会被编码
   val f4: String? = null, // f4为空字符串会被编码
)

一切取决于右边的默认值,如果当前值是默认值则不会编码,早期shamrock大量数据类的Long, Int类型的默认值是0,导致0不会被编码,以至于出现不符合标准的错误。

@whitechi73 whitechi73 self-requested a review February 16, 2024 10:24
@whitechi73 whitechi73 added the bug Something isn't working label Feb 16, 2024
@whitechi73 whitechi73 linked an issue Feb 16, 2024 that may be closed by this pull request
@Mythologyli
Copy link
Contributor Author

所以目前还是维持 encodeDefaults = false,然后在需要编码的地方做特别处理吗?

@whitechi73
Copy link
Owner

所以目前还是维持 encodeDefaults = false,然后在需要编码的地方做特别处理吗?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: 部分事件不符合Onebot V11标准
2 participants