Skip to content

Commit

Permalink
#3172 Fix: 枚举类 ValueField 的类型为 Integer 时,无法反序列化 String 类型的值
Browse files Browse the repository at this point in the history
  • Loading branch information
cnscoo authored and wenshao committed Nov 24, 2024
1 parent 0f74507 commit e6ff30a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit e6ff30a

Please sign in to comment.