Skip to content

Commit f3585af

Browse files
committed
backport alibaba#1222 from standard version.
1 parent 2ff1e2b commit f3585af

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/main/java/com/alibaba/fastjson/util/FieldInfo.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.lang.reflect.TypeVariable;
1313
import java.util.Arrays;
1414

15+
import com.alibaba.fastjson.JSONAware;
1516
import com.alibaba.fastjson.annotation.JSONField;
1617

1718
public class FieldInfo implements Comparable<FieldInfo> {
@@ -55,7 +56,7 @@ public FieldInfo(String name, //
5556
this.field = field;
5657
this.ordinal = ordinal;
5758

58-
isEnum = fieldClass.isEnum();
59+
isEnum = fieldClass.isEnum() && !JSONAware.class.isAssignableFrom(fieldClass);
5960

6061
fieldAnnotation = null;
6162
methodAnnotation = null;
@@ -208,7 +209,7 @@ public FieldInfo(String name, //
208209
this.fieldClass = TypeUtils.getClass(genericFieldType);
209210
this.fieldType = genericFieldType;
210211

211-
isEnum = fieldClass.isEnum();
212+
isEnum = fieldClass.isEnum() && !JSONAware.class.isAssignableFrom(fieldClass);
212213
return;
213214
}
214215
}
@@ -229,7 +230,9 @@ public FieldInfo(String name, //
229230
this.fieldType = genericFieldType;
230231
this.fieldClass = fieldClass;
231232

232-
isEnum = (!fieldClass.isArray()) && fieldClass.isEnum();
233+
isEnum = (!fieldClass.isArray())
234+
&& fieldClass.isEnum()
235+
&& !JSONAware.class.isAssignableFrom(fieldClass);
233236
}
234237

235238
public static Type getFieldType(Class<?> clazz, Type type, Type fieldType) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.alibaba.json.bvt.issue_1200;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONAware;
5+
import com.alibaba.fastjson.serializer.SerializerFeature;
6+
import junit.framework.TestCase;
7+
8+
/**
9+
* Created by wenshao on 01/06/2017.
10+
*/
11+
public class Issue1222 extends TestCase {
12+
public void test_for_issue() throws Exception {
13+
Model model = new Model();
14+
model.type = Type.A;
15+
String text = JSON.toJSONString(model, SerializerFeature.WriteEnumUsingToString);
16+
assertEquals("{\"type\":\"TypeA\"}", text);
17+
}
18+
19+
public static class Model {
20+
public Type type;
21+
}
22+
23+
public static enum Type implements JSONAware {
24+
A, B;
25+
26+
public String toJSONString() {
27+
return "\"Type" + this.name() + "\"";
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)