Skip to content

Commit

Permalink
fix jsonpath 1.x compatible, for issue #2542
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 8, 2024
1 parent 666becc commit 30190df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public static <T> T read(String json, String path, Type clazz) {
}

public static Object eval(Object rootObject, String path) {
if (rootObject instanceof String) {
return com.alibaba.fastjson2.JSONPath.eval((String) rootObject, path);
}
com.alibaba.fastjson2.JSONPath jsonPath = com.alibaba.fastjson2.JSONPath.of(path);
return jsonPath.eval(rootObject);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;

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

public class Issue2542 {
@Test
public void test() {
String temp = "{\n" +
" \"code\": \"1003\", \n" +
" \"data\": [1, 2], \n" +
"}\n";
assertEquals(
JSONPath.eval(temp, "$.data"),
com.alibaba.fastjson.JSONPath.eval(temp, "$.data")
);
}
}

0 comments on commit 30190df

Please sign in to comment.