Skip to content

Commit

Permalink
Merge pull request #3606 from github-ganyu/master
Browse files Browse the repository at this point in the history
Fix issue#3601
  • Loading branch information
wenshao committed Jan 7, 2021
2 parents a7f1f5c + 15ac107 commit 7ac0e06
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 10 deletions.
36 changes: 32 additions & 4 deletions src/main/java/com/alibaba/fastjson/JSONPath.java
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 if (itemValue != null) {
} 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
Expand Up @@ -77,7 +77,7 @@ public class SerializeConfig {
};

private List<Module> modules = new ArrayList<Module>();

public String getTypeKey() {
return typeKey;
}
Expand Down Expand Up @@ -852,12 +852,9 @@ private static Member getEnumValueField(Class clazz) {
for (Field field : clazz.getFields()) {
JSONField jsonField = field.getAnnotation(JSONField.class);

// Returns null if @JSONField is on the enumeration field
if (jsonField != null) {
if (member != null) {
return null;
}

member = field;
return null;
}
}

Expand Down
@@ -0,0 +1,189 @@
package com.alibaba.fastjson.jsonpath.issue3607;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

/**
* @author : ganyu
* <p> @Date: 2021/1/6 10:57 </p>
*/
public class TestIssue3607 {

@Test
public void testIssue3607() {
TestData testData = JSON.parseObject("{\n" +
" \"data\": {\n" +
" \"dataRows\": [\n" +
" {\n" +
" \"dataFields\": [\n" +
" {\n" +
" \"id\": 1332,\n" +
" \"name\": \"gmtRegular\",\n" +
" \"status\": \"success\",\n" +
" \"valueType\": \"DATE\"\n" +
" },\n" +
" {\n" +
" \"id\": 302,\n" +
" \"name\": \"deptNo\",\n" +
" \"status\": \"success\",\n" +
" \"value\": \"C3736\",\n" +
" \"valueType\": \"STRING\"\n" +
" },\n" +
" {\n" +
" \"id\": 143,\n" +
" \"name\": \"gmtOrigRegular\",\n" +
" \"status\": \"success\",\n" +
" \"value\": 1621126800000,\n" +
" \"valueType\": \"DATE\"\n" +
" },\n" +
" {\n" +
" \"id\": 135,\n" +
" \"name\": \"name\",\n" +
" \"status\": \"success\",\n" +
" \"value\": \"\",\n" +
" \"valueType\": \"STRING\"\n" +
" },\n" +
" {\n" +
" \"id\": 133,\n" +
" \"name\": \"workNo\",\n" +
" \"status\": \"success\",\n" +
" \"value\": \"29*6\",\n" +
" \"valueType\": \"STRING\"\n" +
" },\n" +
" {\n" +
" \"id\": 140,\n" +
" \"name\": \"gmtEntry\",\n" +
" \"status\": \"success\",\n" +
" \"value\": 1605456000000,\n" +
" \"valueType\": \"DATE\"\n" +
" },\n" +
" {\n" +
" \"id\": 199,\n" +
" \"name\": \"superWorkNo\",\n" +
" \"status\": \"success\",\n" +
" \"value\": \"240397\",\n" +
" \"valueType\": \"STRING\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"status\": \"success\",\n" +
" \"success\": true\n" +
"}", TestData.class);


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

}

static class TestData {
private Test1 data;
private String status;
private Boolean success;

public Test1 getData() {
return data;
}

public void setData(Test1 data) {
this.data = data;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Boolean getSuccess() {
return success;
}

public void setSuccess(Boolean success) {
this.success = success;
}
}

static class Test1 {
List<Test2> dataRows;

public List<Test2> getDataRows() {
return dataRows;
}

public void setDataRows(List<Test2> dataRows) {
this.dataRows = dataRows;
}
}

static class Test2 {
List<Test3> dataFields;

public List<Test3> getDataFields() {
return dataFields;
}

public void setDataFields(List<Test3> dataFields) {
this.dataFields = dataFields;
}
}

static class Test3 {
private Integer id;
private String name;
private String status;
private String value;
private String valueType;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getValueType() {
return valueType;
}

public void setValueType(String valueType) {
this.valueType = valueType;
}
}

}

@@ -0,0 +1,9 @@
package com.alibaba.fastjson.serializer.issues3601;

import lombok.Data;

@Data
public class TestEntity {
private TestEnum testEnum;
private String testName;
}
@@ -0,0 +1,32 @@
package com.alibaba.fastjson.serializer.issues3601;

import com.alibaba.fastjson.annotation.JSONField;

public enum TestEnum {

@JSONField
test1("1"),
// @JSONField
test2("2");

private String title;

TestEnum(String title) {
this.title = title;
}

// @JSONField
public String getTitle() {
return title;
}

private Integer i = 100;

@JSONField
public Integer getI() {
return i;
}

}


@@ -0,0 +1,19 @@
package com.alibaba.fastjson.serializer.issues3601;

import com.alibaba.fastjson.JSON;
import org.junit.Assert;
import org.junit.Test;

public class TestIssue3601 {

@Test
public void enumTest() {
TestEntity testEntity = new TestEntity();
testEntity.setTestName("ganyu");
testEntity.setTestEnum(TestEnum.test1);
String json = JSON.toJSONString(testEntity);
System.out.println(json);
Assert.assertEquals("{\"testEnum\":\"test1\",\"testName\":\"ganyu\"}", json);
}

}

0 comments on commit 7ac0e06

Please sign in to comment.