Skip to content

Commit

Permalink
bug fixed for JSONType#serializeEnumAsJavaBean
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 16, 2017
1 parent f8e77b1 commit aa857bc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static class Context {

private Map<String, Integer> variants = new HashMap<String, Integer>();
private int variantIndex = 9;
private boolean nonContext;
private final boolean nonContext;

public Context(FieldInfo[] getters, //
SerializeBeanInfo beanInfo, //
Expand All @@ -72,7 +72,7 @@ public Context(FieldInfo[] getters, //
this.className = className;
this.beanInfo = beanInfo;
this.writeDirect = writeDirect;
this.nonContext = nonContext;
this.nonContext = nonContext || beanInfo.beanType.isEnum();
}

public int var(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ protected void write(JSONSerializer serializer, //
}

SerialContext parent = serializer.context;
serializer.setContext(parent, object, fieldName, this.beanInfo.features, features);
if (!this.beanInfo.beanType.isEnum()) {
serializer.setContext(parent, object, fieldName, this.beanInfo.features, features);
}

final boolean writeAsArray = isWriteAsArray(serializer, features);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public void test_field() throws Exception {
assertEquals("{\"orderType\":{\"remark\":\"结算单\",\"value\":2}}", text);
}

public void test_field_2() throws Exception {
Model model = new Model();
model.orderType = OrderType.SettleBill;
model.orderType1 = OrderType.SettleBill;
String text = JSON.toJSONString(model);
assertEquals("{\"orderType\":{\"remark\":\"结算单\",\"value\":2},\"orderType1\":{\"remark\":\"结算单\",\"value\":2}}", text);
}

@JSONType(serializeEnumAsJavaBean = true)
public static enum OrderType {
PayOrder(1, "支付订单"), //
Expand All @@ -36,5 +44,6 @@ private OrderType(int value, String remark) {

public static class Model {
public OrderType orderType;
public OrderType orderType1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public void test_field() throws Exception {
assertEquals("{\"orderType\":{\"remark\":\"结算单\",\"value\":2}}", text);
}

public void test_field_2() throws Exception {
Model model = new Model();
model.orderType = OrderType.SettleBill;
model.orderType1 = OrderType.SettleBill;
String text = JSON.toJSONString(model);
assertEquals("{\"orderType\":{\"remark\":\"结算单\",\"value\":2},\"orderType1\":{\"remark\":\"结算单\",\"value\":2}}", text);
}

@JSONType(serializeEnumAsJavaBean = true)
private static enum OrderType {
PayOrder(1, "支付订单"), //
Expand All @@ -37,5 +45,6 @@ private OrderType(int value, String remark) {

private static class Model {
public OrderType orderType;
public OrderType orderType1;
}
}

0 comments on commit aa857bc

Please sign in to comment.