Skip to content

Commit

Permalink
paths use array-filter syntax, for issue #2228
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Feb 8, 2024
1 parent 41da0e5 commit b4e4811
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
27 changes: 25 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static void remove(Object rootObject, String path) {

public static Map<String, Object> paths(Object javaObject) {
Map<Object, String> values = new IdentityHashMap<>();
Map<String, Object> paths = new HashMap<>();
Map<String, Object> paths = new LinkedHashMap<>();

RootPath.INSTANCE.paths(values, paths, "$", javaObject);
return paths;
Expand Down Expand Up @@ -160,8 +160,31 @@ void paths(Map<Object, String> values, Map<String, Object> paths, String parent,
Map.Entry entry = (Map.Entry) entryObj;
Object key = entry.getKey();

String path;
if (key instanceof String) {
String path = parent + "." + key;
String strKey = (String) key;
boolean escape = strKey.isEmpty();
if (!escape) {
char c0 = strKey.charAt(0);
escape = !((c0 >= 'a' && c0 <= 'z') || (c0 >= 'A' && c0 <= 'Z') || c0 == '_');
if (!escape) {
for (int i = 1; i < strKey.length(); i++) {
char ch = strKey.charAt(i);
escape = !((ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')
|| ch == '_');
if (escape) {
break;
}
}
}
}
if (escape) {
path = parent + '[' + JSON.toJSONString(strKey, JSONWriter.Feature.UseSingleQuotes) + ']';
} else {
path = parent + "." + strKey;
}
paths(values, paths, path, entry.getValue());
}
}
Expand Down
59 changes: 59 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/JSONPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JSONPathTest {
@Test
Expand Down Expand Up @@ -82,4 +84,61 @@ public static class Bean1 {

public static class Item {
}

@Test
public void test2() {
{
String key = "phone-Number";
String value = "1234567890";
JSONObject v = JSONObject.of(key, value);
Map<String, Object> paths = JSONPath.paths(v);
assertEquals(2, paths.size());
assertTrue(paths.containsKey("$"));
paths.remove("$");
Map.Entry<String, Object> entry = paths.entrySet().iterator().next();
assertEquals("$['" + key + "']", entry.getKey());
assertEquals(value, entry.getValue());
assertEquals(value, JSONPath.eval(v, entry.getKey()));
}
{
String key = "0123";
String value = "1234567890";
JSONObject v = JSONObject.of(key, value);
Map<String, Object> paths = JSONPath.paths(v);
assertEquals(2, paths.size());
assertTrue(paths.containsKey("$"));
paths.remove("$");
Map.Entry<String, Object> entry = paths.entrySet().iterator().next();
assertEquals("$['" + key + "']", entry.getKey());
assertEquals(value, entry.getValue());
assertEquals(value, JSONPath.eval(v, entry.getKey()));
}
{
String key = "a\tb";
String value = "1234567890";
JSONObject v = JSONObject.of(key, value);
Map<String, Object> paths = JSONPath.paths(v);
assertEquals(2, paths.size());
assertTrue(paths.containsKey("$"));
paths.remove("$");
Map.Entry<String, Object> entry = paths.entrySet().iterator().next();
assertEquals("$['a\\tb']", entry.getKey());
assertEquals(value, entry.getValue());
assertEquals(value, JSONPath.eval(v, entry.getKey()));
}
}

@Test
public void test3() throws Exception {
JSONArray array = JSONArray.of("a", "b", "c");
Map<String, Object> paths = JSONPath.paths(array);
assertEquals(4, paths.size());
assertTrue(paths.containsKey("$"));
paths.remove("$");
int i = 0;
for (Iterator<Map.Entry<String, Object>> it = paths.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Object> entry = it.next();
assertEquals("$[" + (i++) + "]", entry.getKey());
}
}
}

0 comments on commit b4e4811

Please sign in to comment.