Skip to content

Commit

Permalink
enum support fastjson 1.x JSONField#name, for issue #2521
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 7, 2024
1 parent 3466474 commit 89a6c29
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
25 changes: 20 additions & 5 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,28 @@ public static String[] getEnumAnnotationNames(Class enumClass) {
String fieldName = field.getName();
for (int i = 0; i < enumConstants.length; i++) {
Enum e = enumConstants[i];
final int enumIndex = i;
String enumName = e.name();
if (fieldName.equals(enumName)) {
JSONField annotation = field.getAnnotation(JSONField.class);
if (annotation != null) {
String annotationName = annotation.name();
if (annotationName.length() != 0 && !annotationName.equals(enumName)) {
annotationNames[i] = annotationName;
for (Annotation annotation : field.getAnnotations()) {
Class annotationType = annotation.annotationType();
String annotationTypeName = annotationType.getName();
if ("com.alibaba.fastjson2.annotation.JSONField".equals(annotationTypeName)
|| "com.alibaba.fastjson.annotation.JSONField".equals(annotationTypeName)) {
BeanUtils.annotationMethods(annotationType, m -> {
String name = m.getName();
try {
Object result = m.invoke(annotation);
if ("name".equals(name)) {
String annotationName = (String) result;
if (annotationName.length() != 0 && !annotationName.equals(enumName)) {
annotationNames[enumIndex] = annotationName;
}
}
} catch (Exception ignored) {
// ignored
}
});
}
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2521 {
public enum Type {
@JSONField(name = "Rejected")
REJECTED,

@JSONField(name = "Pending")
PENDING,
}

public static class Bean {
public Type type;
}

public enum Type1 {
@com.alibaba.fastjson.annotation.JSONField(name = "Rejected")
REJECTED,

@com.alibaba.fastjson.annotation.JSONField(name = "Pending")
PENDING,
}

public static class Bean1 {
public Type1 type;
}

@Test
public void testMutated() {
String str = JSON.toJSONString(Type.REJECTED);
assertEquals("\"Rejected\"", str);
}

@Test
public void testMutatedfj() {
String str = com.alibaba.fastjson.JSON.toJSONString(Type1.REJECTED);
assertEquals("\"Rejected\"", str);
}

@Test
public void testMutated0() {
Bean bean = new Bean();
bean.type = Type.REJECTED;
String str = JSON.toJSONString(bean, JSONWriter.Feature.WriteEnumsUsingName);
assertEquals("{\"type\":\"Rejected\"}", str);
}

@Test
public void testMutated0fj() {
Bean1 bean1 = new Bean1();
bean1.type = Type1.REJECTED;
String str = com.alibaba.fastjson.JSON.toJSONString(bean1, SerializerFeature.WriteEnumUsingName);
assertEquals("{\"type\":\"Rejected\"}", str);
}
}

0 comments on commit 89a6c29

Please sign in to comment.