Skip to content

修复请求json的key为JSONArray时类型转换异常问题 #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025

Conversation

DenineLu
Copy link

@DenineLu DenineLu commented Apr 8, 2025

问题描述

当request json如下时
"{[]:{"page": 2, "table1":{}}}"

在AbstractObjectParser中parse(String name, boolean isReuse)方法,获取request key时会出现class com.alibaba.fastjson.JSONArray cannot be cast to class java.lang.String 异常,如图所示
image

原因是对于低版本的fastjson,如1.2.21,允许自动进行JSONArray to String的转换,当前使用的1.2.83版本已禁止该隐式转换。

@DenineLu
Copy link
Author

DenineLu commented Apr 8, 2025

@TommyLemon cc

@TommyLemon TommyLemon merged commit f39d269 into Tencent:master Apr 11, 2025
1 check passed
@TommyLemon
Copy link
Collaborator

TommyLemon commented Apr 12, 2025

感谢小红书工程师的贡献,已合并~
不过 [] 这种特殊字符作为 key 没有引号包裹,不符合 JSON 规范,建议改成 "[]" ,这样其实也不会触发这个 bug。
APIJSON 8 已去除 fastjson,可以使用 jackson, gson, fastjson2 等各种其它 JSON 库了
#810

3 个 Demo 已使用 APIJSON 8
https://github.com/APIJSON/APIJSON-Demo/commits/master/APIJSON-Java-Server
Image

当然这个也导致初始化麻烦一些,需要重写 parseObject, parseArray, toJSONString 等方法,用具体的 JSON 库来反序列化,
打算后续提供 apijson-fastjson2, apijson-jackson, apijson-gson 等对应插件,引入后可以简化这部分代码:
https://github.com/APIJSON/APIJSON-Demo/blob/eb051b0df273f30479b10d1238d666e4ed2efadd/APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/DemoApplication.java#L81-L127

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.

2 participants