diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplEnum.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplEnum.java index 0d947708a5..e015241a11 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplEnum.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplEnum.java @@ -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) { diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2100/Issue2154.java b/core/src/test/java/com/alibaba/fastjson2/issues_2100/Issue2154.java new file mode 100644 index 0000000000..cc62179fdb --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2100/Issue2154.java @@ -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; + } + } +}