Skip to content Skip to sidebar Skip to footer

Gson Throws An Exception On > 4.0 Devices

I'm using Gson successfully to place json into an object. It works like a charm on devices with android 2.2 (emulator and real device), when I deploy to android 4.0 and above (emul

Solution 1:

I've found, that if your instance t extends from any android view component(such as Button, for example) or has field of class, that extends from any android view component, you will get such exception

For some reason, this library can't serialize view components any more. So, you can add exclude strategy. For example, this strategy will exclude all fields, except fields with Annotation @SerializedName:

classExcludeimplementsExclusionStrategy {

@OverridepublicbooleanshouldSkipClass(Class<?> arg0) {
    // TODO Auto-generated method stubreturnfalse;
}

@OverridepublicbooleanshouldSkipField(FieldAttributes field) {
    SerializedName sn = field.getAnnotation(SerializedName.class);
    if(sn != null)
        returnfalse;
    returntrue;
}}

Suppose we have sample Json:

privatefinalString jsonSample="{ \"Sample\": { \"field1\":1, \"field2\":2}}";

here is sample code:

privatevoidparseJson(){
    Excludeex=newExclude();
    Gsongson=newGsonBuilder().create();

    GsonObjectgObject= gson.fromJson(jsonSample, GsonObject.class);

}

classGsonObject{
    @SerializedName("Sample")public Smpl smpl;

//  private Button btn;  <-- Uncomment this line, and you will get your error!classSmpl{
        @SerializedName("field1")int fl1;
        @SerializedName("field2")int fl2;
    }
}

But after adding exclude strategy I've written above:

Excludeex=newExclude();
    Gsongson=newGsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();

All works correct.

Finally we has the following test app:

publicclassMainActivityextendsActivity {

privatefinal String jsonSample= "{ \"Sample\": { \"field1\":1, \"field2\":2}}";

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    parseJson();
}

privatevoidparseJson(){
    Excludeex=newExclude();
//  Gson gson = new GsonBuilder().create(); <-- without EX strategy it will try to serrialaze Button field in our GsonObject, and throws an exeptionGsongson=newGsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();

    GsonObjectgObject= gson.fromJson(jsonSample, GsonObject.class);

    Toast.makeText(this, "Gson" + gObject.smpl.fl1 + " " + gObject.smpl.fl2, Toast.LENGTH_LONG).show();

}

classGsonObject{
    @SerializedName("Sample")public Smpl smpl;

    private Button btn; // <-- this field is our reason of this strange exception on Gson serializationclassSmpl{
        @SerializedName("field1")int fl1;
        @SerializedName("field2")int fl2;
    }
}
}

classExcludeimplementsExclusionStrategy {

    @OverridepublicbooleanshouldSkipClass(Class<?> arg0) {
        // TODO Auto-generated method stubreturnfalse;
    }

    @OverridepublicbooleanshouldSkipField(FieldAttributes field) {
        SerializedNamens= field.getAnnotation(SerializedName.class);
        if(ns != null)
            returnfalse;
        returntrue;
    }

}

p.s. Sorry for my English)

Post a Comment for "Gson Throws An Exception On > 4.0 Devices"