Skip to content

Commit

Permalink
Fix issue#3607
Browse files Browse the repository at this point in the history
  • Loading branch information
github-ganyu committed Jan 6, 2021
1 parent 28b674e commit 15ac107
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
36 changes: 32 additions & 4 deletions src/main/java/com/alibaba/fastjson/JSONPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,25 @@ public class JSONPath implements JSONAware {
private SerializeConfig serializeConfig;
private ParserConfig parserConfig;

private boolean ignoreNullValue;

public JSONPath(String path){
this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance());
this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance(), true);
}

public JSONPath(String path, boolean ignoreNullValue){
this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance(), ignoreNullValue);
}

public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig){
public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig, boolean ignoreNullValue){
if (path == null || path.length() == 0) {
throw new JSONPathException("json-path can not be null or empty");
}

this.path = path;
this.serializeConfig = serializeConfig;
this.parserConfig = parserConfig;
this.ignoreNullValue = ignoreNullValue;
}

protected void init() {
Expand Down Expand Up @@ -612,6 +619,11 @@ public static Object eval(Object rootObject, String path) {
return jsonpath.eval(rootObject);
}

public static Object eval(Object rootObject, String path, boolean ignoreNullValue) {
JSONPath jsonpath = compile(path, ignoreNullValue);
return jsonpath.eval(rootObject);
}

public static int size(Object rootObject, String path) {
JSONPath jsonpath = compile(path);
Object result = jsonpath.eval(rootObject);
Expand Down Expand Up @@ -676,6 +688,22 @@ public static JSONPath compile(String path) {
return jsonpath;
}

public static JSONPath compile(String path, boolean ignoreNullValue) {
if (path == null) {
throw new JSONPathException("jsonpath can not be null");
}

JSONPath jsonpath = pathCache.get(path);
if (jsonpath == null) {
jsonpath = new JSONPath(path, ignoreNullValue);
if (pathCache.size() < 1024) {
pathCache.putIfAbsent(path, jsonpath);
jsonpath = pathCache.get(path);
}
}
return jsonpath;
}

/**
* @since 1.2.9
* @param json
Expand Down Expand Up @@ -3779,7 +3807,7 @@ protected Object getPropertyValue(Object currentObject, String propertyName, lon
fieldValues = new JSONArray(list.size());
}
fieldValues.addAll(collection);
} else {
} else if (itemValue != null || !ignoreNullValue) {
if (fieldValues == null) {
fieldValues = new JSONArray(list.size());
}
Expand Down Expand Up @@ -3816,7 +3844,7 @@ protected Object getPropertyValue(Object currentObject, String propertyName, lon
if (itemValue instanceof Collection) {
Collection collection = (Collection) itemValue;
fieldValues.addAll(collection);
} else if (itemValue != null) {
} else if (itemValue != null || !ignoreNullValue) {
fieldValues.add(itemValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void testIssue3607() {
"}", TestData.class);


List<String> evalResult = (List<String>) JSONPath.eval(testData, "$.data.dataRows[*].dataFields[*].value");
List<String> evalResult = (List<String>) JSONPath.eval(testData, "$.data.dataRows[*].dataFields[*].value", false);
Assert.assertEquals(testData.getData().getDataRows().get(0).getDataFields().size(), evalResult.size());

}
Expand Down

0 comments on commit 15ac107

Please sign in to comment.