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/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