Skip to content
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

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

Closed
litao11046 opened this issue Feb 4, 2024 · 6 comments
Closed

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

litao11046 opened this issue Feb 4, 2024 · 6 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@litao11046
Copy link

问题描述

使用最新版本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 litao11046 added the bug Something isn't working label Feb 4, 2024
@rowstop
Copy link
Contributor

rowstop commented Feb 5, 2024

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

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();
    }
}

@litao11046
Copy link
Author

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

@litao11046
Copy link
Author

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

@rowstop
Copy link
Contributor

rowstop commented Feb 5, 2024

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

@litao11046
Copy link
Author

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

rowstop added a commit to rowstop/fastjson2 that referenced this issue Feb 5, 2024
rowstop added a commit to rowstop/fastjson2 that referenced this issue Feb 5, 2024
wenshao pushed a commit that referenced this issue Feb 5, 2024
wenshao pushed a commit that referenced this issue Feb 5, 2024
@wenshao wenshao added the fixed label Feb 9, 2024
@wenshao wenshao added this to the 2.0.47 milestone Feb 9, 2024
@wenshao
Copy link
Member

wenshao commented Feb 24, 2024

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

@wenshao wenshao closed this as completed Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants