diff --git a/src/main/java/com/alibaba/fastjson/JSONPath.java b/src/main/java/com/alibaba/fastjson/JSONPath.java index 7b52fd5b9f..2f555406c6 100644 --- a/src/main/java/com/alibaba/fastjson/JSONPath.java +++ b/src/main/java/com/alibaba/fastjson/JSONPath.java @@ -53,11 +53,17 @@ 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"); } @@ -65,6 +71,7 @@ public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parse this.path = path; this.serializeConfig = serializeConfig; this.parserConfig = parserConfig; + this.ignoreNullValue = ignoreNullValue; } protected void init() { @@ -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); @@ -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 @@ -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()); } @@ -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); } } diff --git a/src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java b/src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java index 21a2bd069e..2e2d8261f4 100644 --- a/src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java +++ b/src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java @@ -77,7 +77,7 @@ public class SerializeConfig { }; private List modules = new ArrayList(); - + public String getTypeKey() { return typeKey; } @@ -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; } } diff --git a/src/test/java/com/alibaba/fastjson/jsonpath/issue3607/TestIssue3607.java b/src/test/java/com/alibaba/fastjson/jsonpath/issue3607/TestIssue3607.java new file mode 100644 index 0000000000..d0c769981c --- /dev/null +++ b/src/test/java/com/alibaba/fastjson/jsonpath/issue3607/TestIssue3607.java @@ -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 + *

@Date: 2021/1/6 10:57

+ */ +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 evalResult = (List) 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 dataRows; + + public List getDataRows() { + return dataRows; + } + + public void setDataRows(List dataRows) { + this.dataRows = dataRows; + } + } + + static class Test2 { + List dataFields; + + public List getDataFields() { + return dataFields; + } + + public void setDataFields(List 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; + } + } + +} + diff --git a/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEntity.java b/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEntity.java new file mode 100644 index 0000000000..62a1bb011b --- /dev/null +++ b/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEntity.java @@ -0,0 +1,9 @@ +package com.alibaba.fastjson.serializer.issues3601; + +import lombok.Data; + +@Data +public class TestEntity { + private TestEnum testEnum; + private String testName; +} diff --git a/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEnum.java b/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEnum.java new file mode 100644 index 0000000000..f0792e9164 --- /dev/null +++ b/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEnum.java @@ -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; + } + +} + + diff --git a/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestIssue3601.java b/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestIssue3601.java new file mode 100644 index 0000000000..2618d78926 --- /dev/null +++ b/src/test/java/com/alibaba/fastjson/serializer/issues3601/TestIssue3601.java @@ -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); + } + +} \ No newline at end of file