Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2387: JSONPath eval(string,string) issue fix #2388

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.alibaba.fastjson.util.TypeUtils;
import com.alibaba.fastjson2.JSONReader;


import java.lang.reflect.Type;
import java.util.Map;

Expand Down Expand Up @@ -52,6 +53,11 @@ public static Object eval(Object rootObject, String path) {
return jsonPath.eval(rootObject);
}

public static Object eval(String rootObject, String path) {
com.alibaba.fastjson2.JSONPath jsonPath = com.alibaba.fastjson2.JSONPath.of(path);
return jsonPath.extract(rootObject);
}

public static boolean set(Object rootObject, String path, Object value) {
com.alibaba.fastjson2.JSONPath jsonPath = com.alibaba.fastjson2.JSONPath.of(path);
jsonPath.setReaderContext(JSON.createReadContext(JSON.DEFAULT_PARSER_FEATURE));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson.issue_2300;

import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.*;
import com.alibaba.fastjson.JSONPath;

public class Issue2387Test {
@Test
public void test_for_issue() throws Exception {
String data = "{\"userName\":\"testname\"}";
Object userName1 = JSONPath.eval(data, "$.userName");
assertEquals(userName1, "testname");
}

@Test
public void test_not_matching() {
String data = "{\"userName\":\"testname\"}";
Object userName1 = JSONPath.eval(data, "$.userName");
assertNotEquals(userName1, "testname1");
}

@Test
public void test_null() throws Exception {
String data = "{\"userName\":null}";
Object userName1 = JSONPath.eval(data, "$.userName");
assertNull(userName1);
}
}
Loading