Skip to content

Commit

Permalink
fix: 修复JSON.put/add传入List<JSON>数据导致堆栈溢出问题
Browse files Browse the repository at this point in the history
  • Loading branch information
qw623577789 committed Dec 8, 2022
1 parent b49da2c commit 0ffc3e2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = "team.qtk"
version = "1.14.1"
version = "1.14.2"
sourceCompatibility = 11
targetCompatibility = 11

Expand Down
34 changes: 33 additions & 1 deletion src/main/java/team/qtk/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class JSON {
Expand Down Expand Up @@ -189,6 +190,21 @@ public JSON put(String id, Object value) {
objectNode.set(id, null);
} else if (value instanceof JSON) {
objectNode.set(id, ((JSON) value).getJacksonNode());
} else if (value instanceof List) {
objectNode.set(
id,
jackson.valueToTree(
((List<?>) value).stream().map(item -> {
if (item == null) {
return null;
} else if (item instanceof JSON) {
return ((JSON) item).getJacksonNode();
} else {
return jackson.valueToTree(item);
}
}).collect(Collectors.toList())
)
);
} else {
objectNode.set(id, jackson.valueToTree(value));
}
Expand All @@ -210,7 +226,23 @@ public JSON add(Object... value) {
arrayNode.add(jackson.valueToTree(null));
} else if (item instanceof JSON) {
arrayNode.add(((JSON) item).getJacksonNode());
} else {
}
else if (item instanceof List) {
arrayNode.add(
jackson.valueToTree(
((List<?>) item).stream().map(i -> {
if (i == null) {
return null;
} else if (i instanceof JSON) {
return ((JSON) i).getJacksonNode();
} else {
return jackson.valueToTree(i);
}
}).collect(Collectors.toList())
)
);
}
else {
arrayNode.add(jackson.valueToTree(item));
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/team/qtk/json/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -3659,6 +3660,16 @@ void dateTimeGet() {
assertEquals(json.getLocalDateTime(".date"), LocalDateTime.parse("2022-07-07T00:00:00"));
}

@Test
void fixListJSONPutOrAdd() {
var obj = JSON.parse("{\"id\":\"424C6EB8FDBE4A478C1E420806525D19\",\"from\":40,\"name\":\"test3\",\"alias\":\"82sdsadsadasdsadsaddsadsadsadsacxzcasdsadswqewqewqsdsadsadsaewqe\",\"image\":\"http://dummyimage.com/400x400\",\"price\":13,\"equity\":\"magna dolor\",\"status\":1,\"pattern\":0,\"urlFrom\":0,\"category\":0,\"createdAt\":1670293312,\"describes\":[\"quisnulladasdsadsadsadsadsadsa\",\"quisnulladasdsadsadsadsadsadsa\",\"quisnulladasdsadsadsadsadsadsa\"],\"isDeleted\":0,\"tocBuyUrl\":\"http://vclp.nc/ridspj\",\"supplierId\":0,\"commodityCode\":\"82sdsadsadasdsadsaddsadsadsadsacxzcasdsadswqewqewqsdsadsadsaewqe\",\"createdCoreOrgId\":\"000\"}")
.getJSON();

System.out.println(JSON.sPut("timestamp", List.of(obj, 1)).toString());
System.out.println(JSON.sAdd(List.of(obj, 1)).toString());

}

@Data
@NoArgsConstructor
@AllArgsConstructor
Expand Down Expand Up @@ -3692,9 +3703,9 @@ void newObjectAssign() {
A aObj = A.builder().a("a").b(A.B.builder().b("b").build()).build();

String assign1 = "{\"a\":\"a1\",\"b\":{\"b\":\"b1\"}}";
Assertions.assertEquals( json.merge(aObj, assign1).toString(), "{\"a\":\"a1\",\"b\":{\"b\":\"b1\"}}");
Assertions.assertEquals( json.toString(),"{\"a\":\"a1\",\"b\":{\"b\":\"b1\"}}");
Assertions.assertEquals( JSON.parse(aObj).toString(), "{\"a\":\"a\",\"b\":{\"b\":\"b\"}}");
Assertions.assertEquals(json.merge(aObj, assign1).toString(), "{\"a\":\"a1\",\"b\":{\"b\":\"b1\"}}");
Assertions.assertEquals(json.toString(), "{\"a\":\"a1\",\"b\":{\"b\":\"b1\"}}");
Assertions.assertEquals(JSON.parse(aObj).toString(), "{\"a\":\"a\",\"b\":{\"b\":\"b\"}}");

A assign2 = A.builder().a("a2").b(A.B.builder().b("b2").build()).build();
Assertions.assertEquals(JSON.assign(JSON.createObject(), assign1, assign2).toString(), "{\"a\":\"a2\",\"b\":{\"b\":\"b2\"}}");
Expand Down

0 comments on commit 0ffc3e2

Please sign in to comment.