Skip to content

Commit

Permalink
fix non public enum deserialize error, for issue #2154
Browse files Browse the repository at this point in the history
  • Loading branch information
rowstop authored and wenshao committed Jan 6, 2024
1 parent d3eec0b commit 37a1dc8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public ObjectReaderImplEnum(
) {
this.enumClass = enumClass;
this.createMethod = createMethod;
if (valueField instanceof AccessibleObject) {
((AccessibleObject) valueField).setAccessible(true);
}
this.valueField = valueField;
Type valueFieldType = null;
if (valueField instanceof Field) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.alibaba.fastjson2.issues_2100;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

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

/**
* @author 张治保
* @since 2024/1/6
*/
public class Issue2154 {
// failure
@Test
public void intEnumDeserialize1() {
Bean1 bean1 = JSON.parseObject("{\"type\":102}", Bean1.class);
assertEquals(102, bean1.type.getCode());
}

// success
@Test
public void intEnumDeserialize2() {
// add this
{
Bean1 bean = new Bean1();
bean.type = Type.S;
JSON.toJSONString(bean);
}

intEnumDeserialize1();
}

class Bean1 {
public Type type;
}

enum Type {
X(101),
M(102),
S(103);

private final int code;

Type(int code) {
this.code = code;
}

@JSONField(value = true)
public int getCode() {
return code;
}
}
}

0 comments on commit 37a1dc8

Please sign in to comment.