Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION]使用parseObject反序列化一个类,类中包含一个枚举类,会出现null的情况,请问该怎么解决这个问题? #2239

Closed
formatsystem opened this issue Feb 12, 2024 · 7 comments
Labels
enhancement New feature or request fixed question Further information is requested
Milestone

Comments

@formatsystem
Copy link

请描述您的问题

询问有关本项目的使用和其他方面的相关问题。

public class JsonClass {
    private String name;

    private EnumClass enumClass;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public EnumClass getEnumClass() {
        return enumClass;
    }

    public void setEnumClass(EnumClass enumClass) {
        this.enumClass = enumClass;
    }
}
public enum EnumClass {
    NONE, ONE, TWO;


    public String toValue() {
        switch (this) {
            case NONE:
                return "none";
            case ONE:
                return "one";
            case TWO:
                return "two";
            default:
                return "none";
        }
    }

    public EnumClass forValue(String value) {
        switch (value) {
            case "one":
                return ONE;
            case "two":
                return TWO;
            case "none":
                return NONE;
            default:
                return NONE;
        }
    }
}
    public void testParseObject(){
        String json = "{\"name\":\"hello\",\"enumClass\":\"\"}";

        JsonClass jsonClass = JSON.parseObject(json, JsonClass.class);
        System.out.println("--------> "+jsonClass.getEnumClass());
    }

结果如图
null问题

@formatsystem formatsystem added the question Further information is requested label Feb 12, 2024
@wenshao
Copy link
Member

wenshao commented Feb 13, 2024

空字符串会当做空值处理

@formatsystem
Copy link
Author

空字符串会当做空值处理

我想问,如果出现null,能否指定一个初始值?该如何指定?避免后续方法出现问题

@wenshao
Copy link
Member

wenshao commented Feb 13, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.47-SNAPSHOT/
更新到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
    }

@formatsystem
Copy link
Author

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.47-SNAPSHOT/ 更新到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才可以.
上方您给出的代码如果将type改成priavte 并生成对应的 get set方法执行后就会出现如下错误.

test方法中仅保留
Assert.assertEquals(Type.NONE, JSON.parseObject("{"type":""}", Bean.class).getType());

执行后就提示如下

com.alibaba.fastjson2.JSONException: illegal defaultValue : NONE, class com.gf.fastjson2.FastJson2Test$Type

at com.alibaba.fastjson2.reader.ObjectReaderCreator.createFieldReaderMethod(ObjectReaderCreator.java:2107)
at com.alibaba.fastjson2.reader.ObjectReaderCreator.createFieldReader(ObjectReaderCreator.java:1547)
at com.alibaba.fastjson2.reader.ObjectReaderCreator.lambda$createFieldReaders$4(ObjectReaderCreator.java:1654)
at com.alibaba.fastjson2.util.BeanUtils.setters(BeanUtils.java:573)
at com.alibaba.fastjson2.reader.ObjectReaderCreator.createFieldReaders(ObjectReaderCreator.java:1650)
at com.alibaba.fastjson2.reader.ObjectReaderCreatorASM.createObjectReader(ObjectReaderCreatorASM.java:259)
at com.alibaba.fastjson2.reader.ObjectReaderProvider.getObjectReaderInternal(ObjectReaderProvider.java:854)
at com.alibaba.fastjson2.reader.ObjectReaderProvider.getObjectReader(ObjectReaderProvider.java:746)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:780)
at com.gf.fastjson2.FastJson2Test.test(FastJson2Test.java:793)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

@wenshao
Copy link
Member

wenshao commented Feb 14, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.47-SNAPSHOT/
帮忙更新快照版本2.0.47-SNAPSHOT,问题已经修复,帮忙再验证下是否还有问题

@formatsystem
Copy link
Author

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.47-SNAPSHOT/ 帮忙更新快照版本2.0.47-SNAPSHOT,问题已经修复,帮忙再验证下是否还有问题

问题已解决!

@wenshao wenshao added the enhancement New feature or request label Feb 14, 2024
@wenshao wenshao added this to the 2.0.47 milestone Feb 14, 2024
@wenshao wenshao added the fixed label Feb 14, 2024
@wenshao
Copy link
Member

wenshao commented Feb 24, 2024

@wenshao wenshao closed this as completed Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants