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

反序列化时出现NullPointerException #1

Closed
sunng87 opened this issue Dec 9, 2011 · 6 comments
Closed

反序列化时出现NullPointerException #1

sunng87 opened this issue Dec 9, 2011 · 6 comments

Comments

@sunng87
Copy link

sunng87 commented Dec 9, 2011

你好,我在使用fastjson作Clojure RPC的序列化支持,我通过

(JSON/toJSONString {:a 1} (into-array Serializer/WriteClassName))

得到JSON字符串

{"@type":"clojure.lang.PersistentArrayMap",{"@type":"clojure.lang.Keyword","name":"a","sym":{"name":"a"}}:1}

然后通过parse进行反序列化遇到这个空指针异常

No message.
[Thrown class com.alibaba.fastjson.JSONException]

Restarts:
0: [QUIT] Quit to the SLIME top level
1: [CAUSE1] Invoke debugger on cause [Thrown class java.lang.NullPointerException]

Backtrace:
0: com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer.deserialze(DefaultObjectDeserializer.java:349)
1: com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer.deserialze(DefaultObjectDeserializer.java:251)
2: com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:251)
3: com.alibaba.fastjson.parser.AbstractJSONParser.parse(AbstractJSONParser.java:157)
4: com.alibaba.fastjson.parser.AbstractJSONParser.parse(AbstractJSONParser.java:135)
5: com.alibaba.fastjson.JSON.parse(JSON.java:87)
6: com.alibaba.fastjson.JSON.parse(JSON.java:78)
7: slacker.serialization.fastjson$parse_string.invoke(fastjson.clj:6)
8: user$eval1853.invoke(NO_SOURCE_FILE:1)
9: clojure.lang.Compiler.eval(Compiler.java:5424)
--more--

我使用的是maven仓库的1.1.9版本。

@wenshao
Copy link
Member

wenshao commented Dec 9, 2011

问题已经重现,正在处理中,由于对Clojure不熟悉,需要花一些时间学习Clojure

@sunng87
Copy link
Author

sunng87 commented Dec 10, 2011

我看了一下clojure的keyword类并不是一个标准的JavaBean,没有构造函数,没有setter,所以我猜在反序列化的时候可能会存在问题。不知道这个NPE是否是这个原因?
不知道fastjson有没有自定义serializer和deserializer的途径,类似

public interface Serializer<T> {
    public JSONObject serialize (T o);
    public T deserialize(JSONObject o);
}

@wenshao
Copy link
Member

wenshao commented Dec 10, 2011

有的,你自己实现com.alibaba.fastjson.parser.deserializer.AutowiredObjectDeserializer和com.alibaba.fastjson.serializer.AutowiredObjectSerializer,然后配置在下面这两个路径中:
META-INF/services/com.alibaba.fastjson.parser.deserializer.AutowiredObjectDeserializer
META-INF/services/com.alibaba.fastjson.serializer.AutowiredObjectSerializer

这个机制类似与jdk自动的ServiceLoader。fastjson内部的一些Serializer和Deserializer也是通过这种机制实现的。

@sunng87
Copy link
Author

sunng87 commented Dec 10, 2011

On Sat 10 Dec 2011 06:14:38 PM CST, 温少 wrote:

有的,你自己实现com.alibaba.fastjson.parser.deserializer.AutowiredObjectDeserializer和com.alibaba.fastjson.serializer.AutowiredObjectSerializer,然后配置在下面这两个路径中:
META-INF/services/com.alibaba.fastjson.parser.deserializer.AutowiredObjectDeserializer
META-INF/services/com.alibaba.fastjson.serializer.AutowiredObjectSerializer

这个机制类似与jdk自动的ServiceLoader。fastjson内部的一些Serializer和Deserializer也是通过这种机制实现的。


Reply to this email directly or view it on GitHub:
AlibabaTech/fastjson#1 (comment)

谢谢,我去了解一下!

Sun Ning
Software developer
Nanjing, China (N32°3'42'' E118°46'40'')
http://about.me/sunng/bio

@zhaoyao
Copy link

zhaoyao commented Jan 12, 2012

不知道这个和我遇到的是不是同一个问题 版本1.1.9

public class FieldMap extends HashMap<String, Object> {

    public FieldMap field(String field, Object val) {
        this.put(field, val);
        return this;
    }

    @SuppressWarnings("unchecked")
    public <T> T field(String field) {
        return (T) this.get(field);
    }

    public static FieldMap ofInstance(Object ins) {
        Preconditions.checkNotNull(ins);
        FieldMap fieldMap = new FieldMap();
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(ins);
        for (PropertyDescriptor descriptor : beanWrapper.getPropertyDescriptors()) {
            String propertyName = descriptor.getName();
            fieldMap.field(propertyName, beanWrapper.getPropertyValue(propertyName));
        }

        return fieldMap;
    }
}
JSON.toJSONString( m, SerializerFeature.WriteClassName );

Exception in thread "main" com.alibaba.fastjson.JSONException
amt com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer.deserialze(DefaultObjectDeserializer.java:349)
at com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer.deserialze(DefaultObjectDeserializer.java:251)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:251)
at com.alibaba.fastjson.parser.AbstractJSONParser.parse(AbstractJSONParser.java:157)
at com.alibaba.fastjson.parser.AbstractJSONParser.parse(AbstractJSONParser.java:135)
at com.alibaba.fastjson.JSON.parse(JSON.java:87)
at com.alibaba.fastjson.JSON.parse(JSON.java:78)
at com.matrixjoy.salon.module.payment.PaymentServiceImpl.main(PaymentServiceImpl.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NullPointerException
at com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer.parseObject(DefaultObjectDeserializer.java:164)
at com.alibaba.fastjson.parser.deserializer.DefaultObjectDeserializer.deserialze(DefaultObjectDeserializer.java:344)
... 12 more

@wenshao
Copy link
Member

wenshao commented Jan 18, 2012

这个bug 1.1.14已经修复了。

@sunng87 sunng87 closed this as completed Jan 18, 2012
wenshao pushed a commit that referenced this issue Sep 29, 2014
yakolee pushed a commit that referenced this issue Mar 9, 2015
wenshao pushed a commit that referenced this issue Jul 6, 2015
合并原作者的更新
wenshao pushed a commit that referenced this issue Mar 11, 2016
wenshao pushed a commit that referenced this issue Apr 21, 2016
bug fixed for null value
wenshao pushed a commit that referenced this issue May 4, 2016
aruanruan added a commit to aruanruan/fastjson that referenced this issue Nov 28, 2016
{"@type":"com.alibaba.fastjson.serializer.TestBean#0","data":{"@type":"com.alibaba.fastjson.JSONObject#1","key":"value"},"datas":[{"$ref":"$.data"},{"@type":"alibaba#1","key":"value"},{"@type":"alibaba#1","key":"value"}],"name":"tester"}
wenshao pushed a commit that referenced this issue Jul 30, 2017
pull the original to here
wenshao pushed a commit that referenced this issue Aug 23, 2017
update to 1.2.39-preview
ranger71 added a commit to ranger71/fastjson that referenced this issue Sep 20, 2017
Sanity checking: injected error causes failure in 2 tests.
The first line in each clone instance is different. The last line in each is the predicate of an if statement whose bodies are different, so choosing to leave that out of scope.
An attempt to extract a method for all the rest fails (two results).
Skipping the second line in each clone instance (one result) we get a nicely extracted method with 10 statements and two invocations.
wenshao pushed a commit that referenced this issue Feb 26, 2018
wenshao pushed a commit that referenced this issue May 16, 2020
xiongtj pushed a commit to fsxiaoke/fastjson that referenced this issue Aug 18, 2020
wenshao pushed a commit that referenced this issue Nov 2, 2020
wenshao pushed a commit that referenced this issue Jul 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants