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

FastJson的自省功能:@type如果没有在“{”之后的第一位,则反序列化得不到预计结果 #406

Closed
jslet opened this issue Aug 25, 2015 · 3 comments
Milestone

Comments

@jslet
Copy link

jslet commented Aug 25, 2015

我们在使用FastJson时发现自省功能有个这样的限制:@type只能放在“{”后面的第一位,如:
{“@type":"com.xxx.Foo", "name": "Foo"} 反序列化后得到的是 Foo对象,但交换一下顺序,改为:
{ "name": "Foo", “@type":"com.xxx.Foo"}反序列化后得到的是 Foo对象,但Foo里面的属性没有值。

期望是:@type可以放在任何位置都能到正确的对象。
有这个要求主要是:Object C++里产生的json无法保证@type在第一位

经过跟踪代码,发现DefaultJSONParser.java的300行实例化后,没有把之前的参数传入的(Map object)里的值设置回

 if (deserializer instanceof ASMJavaBeanDeserializer) {
         instance = ((ASMJavaBeanDeserializer) deserializer).createInstance(this, clazz);
 } else if (deserializer instanceof JavaBeanDeserializer) {
         instance = ((JavaBeanDeserializer) deserializer).createInstance(this, clazz);
  }

   if (instance == null) {
        if (clazz == Cloneable.class) {
              instance = new HashMap();
        } else {
              instance = clazz.newInstance();
       }
   }
//希望在此位置把object里的值设置回instance里,类似于:
//org.apache.commons.beanutils.populate(instance, object); 

    return instance;
@jslet jslet changed the title 自省功能(@type如果没有在第一位,则反序列化失败) FastJson的自省功能:@type如果没有在“{”之后的第一位,则反序列化得不到预计结果 Aug 25, 2015
@qifanyang
Copy link
Contributor

测试@type放在后面是可以的,反序列化正常,属性也读出来了,给出你的测试例子吧

"{\"name\":\"Foo\",\"@type\":\"com.alibaba.json.test.issue.Foo\"}";

@089apps
Copy link

089apps commented Oct 18, 2015

I have the same problem here.
Duplicate: #291

@wenshao
Copy link
Member

wenshao commented Apr 13, 2016

bug fixed. included in 1.2.9 & 1.1.49.android

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

4 participants