-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[QUESTION]使用parseObject反序列化一个类,类中包含一个枚举类,会出现null的情况,请问该怎么解决这个问题? #2239
Comments
空字符串会当做空值处理 |
我想问,如果出现null,能否指定一个初始值?该如何指定?避免后续方法出现问题 |
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.47-SNAPSHOT/ @Test
public void test() {
assertEquals(Type.NONE, JSON.parseObject("{}", Bean.class).type);
assertEquals(Type.NONE, JSON.parseObject("{\"type\":null}", Bean.class).type);
assertEquals(Type.NONE, JSON.parseObject("{\"type\":\"\"}", Bean.class).type);
}
public static class Bean {
@JSONField(defaultValue = "NONE")
public Type type;
}
public enum Type {
NONE, ONE, TWO
} |
能达到需求,但需要将属性从private改成public才可以. test方法中仅保留 执行后就提示如下 com.alibaba.fastjson2.JSONException: illegal defaultValue : NONE, class com.gf.fastjson2.FastJson2Test$Type
|
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.47-SNAPSHOT/ |
问题已解决! |
请描述您的问题
询问有关本项目的使用和其他方面的相关问题。
结果如图
![null问题](https://private-user-images.githubusercontent.com/63693013/303977919-1cd97af6-33ad-4d04-a482-98c994fc859e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2Nzk4MDcsIm5iZiI6MTczOTY3OTUwNywicGF0aCI6Ii82MzY5MzAxMy8zMDM5Nzc5MTktMWNkOTdhZjYtMzNhZC00ZDA0LWE0ODItOThjOTk0ZmM4NTllLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE2VDA0MTgyN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI3YTA5MjJkMGRhY2Q1MzRmOTZkMjFiN2EzYzQ3YmVjM2MwZmM1OTgwYmMxOTRkNmRiNDFiYjJjNGFiMWUzYjYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.I7aENrpQ4o7XhxxaXn5haD_ZDaxx7RRVCKJXYDj5n24)
The text was updated successfully, but these errors were encountered: