JFinalJson.setDefaultConvertDepth函数的疑惑!

@JFinal @杜福忠

JFinalJson.setDefaultConvertDepth(int depth)这个函数用来设置全局默认的转json的层次深度。但测试发现有一个疑惑:

踩坑记:我构造一个10层的树,每个节点还有一个children节点是数组,发现最后一层的children里两个元素变成了children:[null,null],但我看默认的全局解析深度是16层。我明明只有10层。

我设置JFinalJson.setDefaultConvertDepth(17),但渲染出来的只有也不对,设置成18,可以正常渲染到9层,设置20可以正常渲染到10层。这个数是两个两个的增加。

我测试了一个节点有两个key的值都是数组,也是这个规律。

搞迷糊了,麻烦谁懂解释一下

准备数据的代码:

List<Record> recList = new ArrayList<>();
for(int i=1;i<20;i++) {
	int id = i;
	int pid =  i - 1;
	String text = "Json"+i;
	Record rec = new Record();
	rec.put("id", id);
	rec.put("pid", pid);
	rec.put("text", text);
	recList.add(rec);
}
//构造树
List<JSONObject> tree = TreeUtil.buildTreeFromRecord(recList, "id", "id", "text", "text", "pid", 0L, "");
this.renderJson(tree);

其中,TreeUtil.buildTreeFromRecord函数就是根据List<Record>集合用stream递归构造树的静态函数。

当不设置JFinalJson.setDefaultConvertDepth(100);保持默认的时候,渲染出来的截图:


1.png

当设置JFinalJson.setDefaultConvertDepth(100);渲染出来的截图:

2.png

当设置JFinalJson.setDefaultConvertDepth(17);渲染出来的截图:

3.png

当设置JFinalJson.setDefaultConvertDepth(18);渲染出来的截图:

4.png

这就是我懵逼的原因!

评论区

杜福忠

2023-08-29 21:45

我也懵逼,要不贴点代码?我测试没问题呀
public static void main(String[] args) {
Okv base = null;
Okv dad = null;
String[] kin = {"媳妇", "弟弟", "妹妹"};
List<String> riches = Arrays.asList(new String[]{"房子", "车子", "票子"});

for (int i = 0; i <= 20; i++) {
Okv son = Okv.of("name", "杜" + i);
son.set("kin", kin);
son.set("riches", riches);
if (base == null){
base = son;
}else {
dad.set("son", son);
}
dad = son;
}
JFinalJson.setDefaultConvertDepth(18);
String json = JFinalJson.getJson().toJson(base);
System.out.println(json);
}
{"name":"杜0","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜1","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜2","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜3","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜4","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜5","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜6","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜7","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜8","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜9","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜10","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜11","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜12","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜13","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜14","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜15","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜16","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜17","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜18","kin":null,"riches":null,"son":null}}}}}}}}}}}}}}}}}}}

JFinal

2023-08-30 10:09

将 json 表示成一颗树,看一下树最深的地方是多少层

zzutligang

2023-09-04 08:29

@杜福忠 @JFinal 我的数据比较多,是业务数据。我抽空简化一下数据,把代码贴上上看看。谢谢了。

zzutligang

2023-09-05 09:50

@杜福忠 @JFinal 代码和结果截图我都发上来了,麻烦抽空帮看看,谢谢了!

杜福忠

2023-09-05 22:48

@zzutligang [ 是一层,{ 也是一层,所以 数组 套 map 是两层。并不是只算 { map对象

zzutligang

2023-09-06 09:25

@杜福忠 你这么说,我倒是也能理解!但为什么JFinalJson对象要设置这个最大深度呢?当然我可以通过JFinalJson.setDefaultConvertDepth(1000);设置最大深度。但又不敢设置太大,比如Integer.MAX_VALUE。FastJson,JackSon都没有这个最大深度限制,不知道这里设置一个很大的最大深度会不会有什么影响。

杜福忠

2023-09-06 10:43

@zzutligang 防止业务死循环,上面对象套对象,如果有两个相互是对方父级,那么转JSON字符串的时候就相互调用了,JVM就GG了

杜福忠

2023-09-06 10:52

@zzutligang 1K也不算大,你上面业务对象也不大,就算循环了,并发不大的情况下也可以正常渲染JSON,内存可以算出来的。虽然是业务问题,但JF已经考虑到了

zzutligang

2023-09-06 15:53

@杜福忠 我上面上那个演示,只是为了演示效果,实际我的业务数据转json的时候key很多,children里数组内容也会很多,总之数据量还是很大的。我觉得对象死循环,应该是业务代码的问题,框架不应该介入吧。你看fastjson和jackson不都没管嘛。反正知道是怎么回事就行了。理论上,我们的业务把JFinalJson.setDefaultConvertDepth(1000);设置为1000,也足够用了。只是心理上总觉得如鲠在喉(完全是心理作用)

热门反馈

扫码入社