fastjson2
fastjson2 copied to clipboard
[BUG] 反序列化时JSONObject字段出现{"h":{***}}结构
问题描述
使用最新版本fastjson2 (2.0.46)将JSONObject对象反序列化为Java对象时,对于对象中的JSONObject字段,该字段会多出{"h":{***}}结构,与反序列化不一致
环境信息
fastjson2 版本:2.0.46
重现步骤
- 测试案例准备
@Data
public class DynFormDto {
private List<DynFormComponentDto> componentList;
private String notes;
}
@Data
public class DynFormComponentDto implements Serializable{
private JSONObject props; // 组件完整配置
// 这个方法会导致props字段反序列化结果出现{"h":{***}}结构
public DynFormComponentDto props(IFormComponent comp) {
this.props = (JSONObject) JSONObject.from(comp, JSONWriter.Feature.FieldBased);
return this;
}
}
- 使用如下方法进行验证
JSONObject dfDtoJson = new JSONObject();
dfDtoJson.put("componentList", new JSONArray().fluentAdd(new JSONObject().fluentPut("props", new JSONObject().fluentPut("compTypeName", "test11").fluentPut("name", "test22"))));
DynFormDto newDfDto = dfDtoJson.toJavaObject(DynFormDto.class);
System.out.println(newDfDto.getComponentList().get(0).getProps());
执行结果: {"h":{"compTypeName":"test11","name":"test22"}}
正常应该为: {"compTypeName":"test11","name":"test22"}
没复现, 你检查下哪里配置的不一致
public class Issue2230 {
@Test
void test(){
JSONObject dfDtoJson = new JSONObject();
dfDtoJson.put(
"componentList",
new JSONArray().fluentAdd(
new JSONObject().fluentPut(
"props",
new JSONObject()
.fluentPut("compTypeName", "test11")
.fluentPut("name", "test22")
)
)
);
DynFormDto newDfDto = dfDtoJson.toJavaObject(DynFormDto.class);
System.out.println(newDfDto.getComponentList().get(0).getProps());
}
@Data
static class DynFormDto {
private List<DynFormComponentDto> componentList;
private String notes;
}
@Data
static class DynFormComponentDto implements Serializable {
private JSONObject props; // 组件完整配置
// 这个方法会导致props字段反序列化结果出现{"h":{***}}结构
public DynFormComponentDto props(IFormComponent comp) {
this.props = (JSONObject) JSONObject.from(comp, JSONWriter.Feature.FieldBased);
return this;
}
}
interface IFormComponent {
String getCompTypeName();
String getName();
}
}
@rowstop 你这个测试案例,多次运行,就会重现重来
@rowstop 多次运行你的案例,重新截图
给这个方法加上@JSONField(deserialize = false)
@Data
static class DynFormComponentDto implements Serializable {
private JSONObject props; // 组件完整配置
// 这个方法会导致props字段反序列化结果出现{"h":{***}}结构
@JSONField(deserialize = false)
public DynFormComponentDto props(IFormComponent comp) {
this.props = JSONObject.from(comp, JSONWriter.Feature.FieldBased);
return this;
}
}
请后续优化一下吧,添加@JSONField(deserialize = false)的方式,对代码有侵入性
https://github.com/alibaba/fastjson2/releases/tag/2.0.47 问题已修复,请用新版本