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] fastjson1.x升级到2.x时,将java对象转换成JSONObject对象时,对于List集合对象字段,转换后的集合字段的集合元素类型没有变 #2187

Closed
litao11046 opened this issue Jan 14, 2024 · 9 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@litao11046
Copy link

问题描述

简要描述您碰到的问题。
fastjson1.x升级到2.x时,将java对象转换成JSONObject对象时,对于List集合对象字段,转换后的集合字段的集合元素类型没有变。1.x版本会自动将集合元素的类型转换成JSONObject

JSONObject dfDtoJson = (JSONObject) JSON.toJSON(obj, JSONWriter.Feature.FieldBased);
@litao11046 litao11046 added the bug Something isn't working label Jan 14, 2024
@wenshao
Copy link
Member

wenshao commented Jan 14, 2024

能帮忙提供重现问题的testcase么?

@wenshao
Copy link
Member

wenshao commented Jan 14, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.46-SNAPSHOT/
问题已修复,请帮忙用2.0.46-SNAPSHOT版本验证,正式版本预计在3月初发布。

@wenshao wenshao added this to the 2.0.46 milestone Jan 14, 2024
@wenshao wenshao added the fixed label Jan 14, 2024
@litao11046
Copy link
Author

2.0.46-SNAPSHOT版本出现了死循环异常:

at com.alibaba.fastjson2.writer.ObjectWriterAdapter.toJSONObject(ObjectWriterAdapter.java:652)
at com.alibaba.fastjson2.JSON.toJSON(JSON.java:3495)
at com.alibaba.fastjson2.JSON.toJSON(JSON.java:3469)
at com.alibaba.fastjson2.writer.ObjectWriterAdapter.toJSONObject(ObjectWriterAdapter.java:652)
at com.alibaba.fastjson2.JSON.toJSON(JSON.java:3495)
at com.alibaba.fastjson2.JSON.toJSON(JSON.java:3469)

@wenshao
Copy link
Member

wenshao commented Jan 17, 2024

能帮构建重现死循环问题的testcase么?

@litao11046
Copy link
Author

testcase如下:

定义如下两个类:

@Data
public class DynFormDto implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private String sysName;
	private String name;
	private boolean enableTab;
	private int labelColSize;
	private String title; 
	private String hint;
	private List<DynFormComponentDto> componentList;
	private String notes;
}

@Data
public class DynFormComponentDto implements Serializable{
	private static final long serialVersionUID = 1L;
	private String name;
	private String nameText;
	private String functionName;
	private DynFormComponentStructType structType;
	private String compTypeName;
	private JSONObject props; // 组件完整配置
	private int widthColSize;
	protected int widthPercentage; //宽度百分比
	private String tabName;
	private boolean enabled;
	private long uid;
	private long parentFieldId;
	
    private List<DynFormComponentDto> subComponentList; //子组件集合
    
    // TODO 这个方法会引起异常
	public List<DynFormComponentDto> getFlatComponentDtos(){
		List<DynFormComponentDto> flatList = Lists.newArrayList();
		flatList.add(this);
		if(this.subComponentList!=null) {
			for(DynFormComponentDto tmpDto : this.subComponentList) {
				tmpDto.setParentFieldId(this.uid);
				flatList.addAll(tmpDto.getFlatComponentDtos());
			}
		}
		return flatList;
	}
}

上面的getFlatComponentDtos方法会引起死循环异常,测试方法如下:

	public static void main(String[] args) {
		String dfDtoJsonStr = "{\"componentList\":[{\"compTypeName\":\"Text\",\"enabled\":true,\"name\":\"notes\",\"nameText\":\"备注\",\"parentFieldId\":0,\"props\":{\"compTypeNameText\":\"文本框\",\"fieldName\":\"notes\",\"notes\":\"\",\"widthColSize\":2,\"widthPercentage\":\"80\",\"nameText\":\"备注\",\"type\":\"SingleField\",\"compTypeName\":\"Text\",\"parentFieldId\":0,\"enabled\":true,\"uid\":156592457733,\"structType\":\"Single\",\"valueType\":\"String\",\"name\":\"notes\",\"readonly\":false,\"required\":false},\"structType\":\"Single\",\"subComponentList\":[],\"uid\":156592457733,\"widthColSize\":2,\"widthPercentage\":0}],\"enableTab\":false,\"hint\":\"\",\"id\":0,\"labelColSize\":2,\"name\":\"test_test\",\"notes\":\"\",\"sysName\":\"StandingBook\",\"title\":\"\"}";
		DynFormDto dfDto = JSON.parseObject(dfDtoJsonStr, DynFormDto.class, JSONReader.Feature.FieldBased);
		JSONObject dfDtoJson = (JSONObject)JSON.toJSON(dfDto, JSONWriter.Feature.FieldBased); // 这一步出现死循环异常
	}

@wenshao
Copy link
Member

wenshao commented Jan 19, 2024

getFlatComponentDtos方法有new,这个无法避免死循环的,配置一个:@JSONField(serialize=false)

@litao11046
Copy link
Author

没明白为何无法避免,这个代码在fastjson1.x是能正常运行的

@wenshao wenshao added the fixed label Jan 27, 2024
@wenshao
Copy link
Member

wenshao commented Jan 29, 2024

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

@wenshao wenshao closed this as completed Jan 29, 2024
@litao11046
Copy link
Author

不行,依旧死循环

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

2 participants