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 bf5d53bc3..0abbc966e 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplEnum.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplEnum.java @@ -49,9 +49,8 @@ public ObjectReaderImplEnum( this.valueFieldType = valueFieldType; if (valueFieldType != null) { - if (valueFieldType == String.class) { - stringValues = new String[enums.length]; - } else { + stringValues = new String[enums.length]; + if (valueFieldType != String.class) { intValues = new long[enums.length]; } @@ -67,8 +66,11 @@ public ObjectReaderImplEnum( if (valueFieldType == String.class) { stringValues[i] = (String) fieldValue; - } else if (fieldValue instanceof Number) { - intValues[i] = ((Number) fieldValue).longValue(); + } else { + stringValues[i] = fieldValue == null ? null : fieldValue.toString(); + if (fieldValue instanceof Number) { + intValues[i] = ((Number) fieldValue).longValue(); + } } } catch (Exception ignored) { // ignored diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3172Kt.kt b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3172Kt.kt new file mode 100644 index 000000000..c36cdee44 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3172Kt.kt @@ -0,0 +1,21 @@ +package com.alibaba.fastjson2.issues_3100 + +import com.alibaba.fastjson2.JSON +import com.fasterxml.jackson.annotation.JsonValue +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +enum class UserStatus(@JsonValue val value: Int, val label: String) { + NORMAL(1, "正常"), + LOCKED(2, "锁定"), +} + +class EnumTest { + @Test + fun testEnum() { + val value = "\"2\"" + val status = JSON.parseObject(value, UserStatus::class.java) + println("Enum Item: $status") // null, version=2.0.53 + Assertions.assertEquals(UserStatus.LOCKED, status) + } +}