From 2fa8ee07e55b476f778a63633f556bbf6b023ab7 Mon Sep 17 00:00:00 2001 From: "ganyu.gy" Date: Wed, 30 Dec 2020 21:14:57 +0800 Subject: [PATCH] Fix issue#3601 --- .../fastjson/serializer/SerializeConfig.java | 9 ++---- .../serializer/issues3601/TestEntity.java | 9 ++++++ .../serializer/issues3601/TestEnum.java | 32 +++++++++++++++++++ .../serializer/issues3601/TestIssue3601.java | 19 +++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEntity.java create mode 100644 src/test/java/com/alibaba/fastjson/serializer/issues3601/TestEnum.java create mode 100644 src/test/java/com/alibaba/fastjson/serializer/issues3601/TestIssue3601.java 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