fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG] 反序列化时JSONObject字段出现{"h":{***}}结构

Open litao11046 opened this issue 1 year ago • 5 comments

问题描述

使用最新版本fastjson2 (2.0.46)将JSONObject对象反序列化为Java对象时,对于对象中的JSONObject字段,该字段会多出{"h":{***}}结构,与反序列化不一致

环境信息

fastjson2 版本:2.0.46

重现步骤

  1. 测试案例准备
@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;
	}

}
  1. 使用如下方法进行验证
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"}

litao11046 avatar Feb 04 '24 03:02 litao11046

没复现, 你检查下哪里配置的不一致

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 avatar Feb 05 '24 01:02 rowstop

@rowstop 你这个测试案例,多次运行,就会重现重来

litao11046 avatar Feb 05 '24 06:02 litao11046

@rowstop 多次运行你的案例,重新截图 图片1

litao11046 avatar Feb 05 '24 06:02 litao11046

给这个方法加上@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;
        }
    }

rowstop avatar Feb 05 '24 08:02 rowstop

请后续优化一下吧,添加@JSONField(deserialize = false)的方式,对代码有侵入性

litao11046 avatar Feb 05 '24 09:02 litao11046

https://github.com/alibaba/fastjson2/releases/tag/2.0.47 问题已修复,请用新版本

wenshao avatar Feb 24 '24 02:02 wenshao