From e6ff30a96ba22cae7df224dfaf1182daa89ad4c0 Mon Sep 17 00:00:00 2001 From: cnscoo Date: Sat, 23 Nov 2024 21:51:27 +0800 Subject: [PATCH] =?UTF-8?q?#3172=20Fix:=20=E6=9E=9A=E4=B8=BE=E7=B1=BB=20Va?= =?UTF-8?q?lueField=20=E7=9A=84=E7=B1=BB=E5=9E=8B=E4=B8=BA=20Integer=20?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=97=A0=E6=B3=95=E5=8F=8D=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=20String=20=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reader/ObjectReaderImplEnum.java | 12 ++++++----- .../fastjson2/issues_3100/Issue3172Kt.kt | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3172Kt.kt 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 bf5d53bc3a..0abbc966ed 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 0000000000..c36cdee44d --- /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) + } +}