-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix non public enum deserialize error, for issue #2154
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
core/src/test/java/com/alibaba/fastjson2/issues_2100/Issue2154.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |