Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Fix issue#3601
Browse files Browse the repository at this point in the history
  • Loading branch information
github-ganyu committed Dec 30, 2020
1 parent a7f1f5c commit 2fa8ee0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.alibaba.fastjson.serializer.issues3601;

import lombok.Data;

@Data
public class TestEntity {
private TestEnum testEnum;
private String testName;
}
Original file line number Diff line number Diff line change
@@ -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;
}

}


Original file line number Diff line number Diff line change
@@ -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 2fa8ee0

Please sign in to comment.