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

customize getFields() to return ordered Field Object #615

Merged
merged 1 commit into from
May 6, 2016
Merged

customize getFields() to return ordered Field Object #615

merged 1 commit into from
May 6, 2016

Conversation

pxb1988
Copy link

@pxb1988 pxb1988 commented May 6, 2016

In some android devices (MI 4(c), android-4.4.4;Huawei P6, android-4.2.2).
the return array of Class.getFields() are dependent on the class name.
When a class and its parent class have same field name, (and the class is obfuscate to different names)
the result is unpredictable.
this patch force the fields in child-fields-first order when deserialize

In some android devices (MI 4(c), android-4.4.4;Huawei P6, android-4.2.2).
the return array of Class.getFields() are dependent on the class name.
When a class and its parent class have same field name, (and the class is obfuscate to different names)
the result is unpredictable.
this patch force the fields in child-fields-first order when deserialize
@CLAassistant
Copy link

CLAassistant commented May 6, 2016

CLA assistant check
All committers have signed the CLA.

@wenshao
Copy link
Member

wenshao commented May 6, 2016

bug fixed in 1.1.51.android & 1.2.11, please use the newst version.

@pxb1988
Copy link
Author

pxb1988 commented May 6, 2016

The problem still occur in 1.2.11. I have tested this on MI 4c, android 4.4.4

I believe it is caused by the Class.getFields()

The elements in the array returned are not sorted and are not in any particular order.

When child and its parent class have a field with same name. the child's field may appear before parent's field on some devices/platforms; while on other devices/platforms, parent's field may appear before child's field.
But fastjson deserialize the value to the first appearance field, which cause an unpredictable situation.

here are steps to reproduce

  • create a helloworld app in Android Studio
  • add 1.2.11 as dependence in app/build.gradle
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.alibaba:fastjson:1.2.11'
}
  • modify the Activity
package com.example.bob.fastjsontest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;

public class MainActivity extends AppCompatActivity {

    public static class A extends B {
        public String extra;
    }

    public static class B {
        public String extra;

        public String getB() {
            return extra;
        }
    }

    public static class C extends B {
        public String extra;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String text = "{ extra:\"yes\" }";
        A a = JSON.parseObject(text, A.class);
        Log.d("T", "A: " + a.getB());
        C c = JSON.parseObject(text, C.class);
        Log.d("T", "C: " + c.getB());
    }
}
  • run it !
D/T       (20692): A: null
D/T       (20692): C: yes

@wenshao
Copy link
Member

wenshao commented May 6, 2016

please pull this merge request to android branch, not 1.2.4_android

@wenshao wenshao merged commit 4829958 into alibaba:1.2.4_android May 6, 2016
@wenshao
Copy link
Member

wenshao commented May 6, 2016

我合并了你的代码到1.2.4_android分支并且然后关闭了这个分支。你这个patch比较多系统方法调用,会降低首次反序列化的性能,我会参考你的代码改进重写,谢谢你的反馈。

wenshao pushed a commit that referenced this pull request May 6, 2016
wenshao added a commit that referenced this pull request May 6, 2016
wenshao pushed a commit that referenced this pull request Jul 17, 2019
wenshao added a commit that referenced this pull request Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants