fix support JSONPath.Sequence for JSONPathSegmentName, for issue #3375

This commit is contained in:
yanxutao89 2025-04-30 22:34:33 +08:00
parent e25d5238ea
commit fe5dd21521
2 changed files with 35 additions and 0 deletions

View File

@ -333,6 +333,10 @@ class JSONPathSegmentName
? context.root
: context.parent.value;
if (object instanceof JSONPath.Sequence) {
object = ((JSONPath.Sequence) object).values;
}
if (object instanceof Map) {
Map map = (Map) object;
Object origin = map.put(name, value);

View File

@ -0,0 +1,31 @@
package com.alibaba.fastjson2.issues_3300;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Issue3375 {
@Test
public void test() {
String json = "{\n" +
"\t\"name\": \"\",\n" +
"\t\"value\": [{\n" +
"\t\t\"id\": \"101\",\n" +
"\t\t\"additionalProperty\": [{\n" +
"\t\t\t\t\"token\": \"token1\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"token\": \"token2\"\n" +
"\t\t\t}\n" +
"\t\t]\n" +
"\t}]\n" +
"}";
Object object = JSON.parse(json);
String path = "$.value.additionalProperty[*].token";
JSONPath.set(object, path, "******");
Object extract = JSONPath.extract(JSON.toJSONString(object), path);
assertEquals("[\"******\",\"******\"]", JSON.toJSONString(extract));
}
}