Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

enum反序列化特性需求 #1582

Open
StoneRivers opened this issue Nov 14, 2017 · 3 comments
Open

enum反序列化特性需求 #1582

StoneRivers opened this issue Nov 14, 2017 · 3 comments
Milestone

Comments

@StoneRivers
Copy link

StoneRivers commented Nov 14, 2017

请问一下fastjosn有类似jackson一样的 read unknown enum as null 或者 read unknown enum as default之类的特性。之前通过定制反序列化ObjectDeserializer实现了,现在通过 JSONObject.toJavaObject 进行反序列化不知如何定制了,请问如果需要定制该如何进行,或者有规划提供这些特性的支持吗。

@wenshao
Copy link
Member

wenshao commented Nov 22, 2017

https://github.com/alibaba/fastjson/releases/tag/1.2.41
新版本已经支持,请使用新版本

@StoneRivers
Copy link
Author

StoneRivers commented Nov 23, 2017

@wenshao 感谢您的回复,之前描述不太清楚。
直接使用JSONObject.parseObject(String text, Class<T> clazz)方法直接反序列化的确能满足要求

ColorContainer colorContainer1 = JSONObject.parseObject("{\"color\":\"error\"}", ColorContainer.class);

但如果先用JSONObject.parseObject(String text)先获取一个JSONObject,再使用其toJavaObject获取一个java对象则会报错。

JSONObject jsonObject = JSONObject.parseObject("{\"color\":\"error\"}");
ColorContainer colorContainer = jsonObject.toJavaObject(ColorContainer.class);

exception stack trace如下:

com.alibaba.fastjson.JSONException: can not cast to : enumjson.Color

	at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1002)
	at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:652)
	at com.alibaba.fastjson.JSON.toJavaObject(JSON.java:935)
	at enumjson.FastJson.test(FastJson.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.alibaba.fastjson.JSONException: can not cast to : enumjson.Color
	at com.alibaba.fastjson.util.TypeUtils.castToEnum(TypeUtils.java:818)
	at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:736)
	at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:831)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:889)
	at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1000)
	... 25 more
Caused by: java.lang.IllegalArgumentException: No enum constant enumjson.Color.error
	at java.lang.Enum.valueOf(Enum.java:238)
	at com.alibaba.fastjson.util.TypeUtils.castToEnum(TypeUtils.java:807)
	... 29 more

期待的结果是一个Color属性为null的ColorContainer对象。
上述代码中的对象定义如下:

package enumjson;

public enum Color {

    red,
    yellow,
    green

}
package enumjson;

public class ColorContainer {
    Color color;

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants