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:
class Exclude implements ExclusionStrategy {
@Override
public boolean shouldSkipClass(Class<?> arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean shouldSkipField(FieldAttributes field) {
SerializedName sn = field.getAnnotation(SerializedName.class);
if(sn != null)
return false;
return true;
}}
Suppose we have sample Json:
private final String jsonSample= "{ \"Sample\": { \"field1\":1, \"field2\":2}}";
here is sample code:
private void parseJson(){
Exclude ex = new Exclude();
Gson gson = new GsonBuilder().create();
GsonObject gObject = gson.fromJson(jsonSample, GsonObject.class);
}
class GsonObject{
@SerializedName("Sample")
public Smpl smpl;
// private Button btn; <-- Uncomment this line, and you will get your error!
class Smpl{
@SerializedName("field1")
int fl1;
@SerializedName("field2")
int fl2;
}
}
But after adding exclude strategy I've written above:
Exclude ex = new Exclude();
Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();
All works correct.
Finally we has the following test app:
public class MainActivity extends Activity {
private final String jsonSample= "{ \"Sample\": { \"field1\":1, \"field2\":2}}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parseJson();
}
private void parseJson(){
Exclude ex = new Exclude();
// Gson gson = new GsonBuilder().create(); <-- without EX strategy it will try to serrialaze Button field in our GsonObject, and throws an exeption
Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();
GsonObject gObject = gson.fromJson(jsonSample, GsonObject.class);
Toast.makeText(this, "Gson" + gObject.smpl.fl1 + " " + gObject.smpl.fl2, Toast.LENGTH_LONG).show();
}
class GsonObject{
@SerializedName("Sample")
public Smpl smpl;
private Button btn; // <-- this field is our reason of this strange exception on Gson serialization
class Smpl{
@SerializedName("field1")
int fl1;
@SerializedName("field2")
int fl2;
}
}
}
class Exclude implements ExclusionStrategy {
@Override
public boolean shouldSkipClass(Class<?> arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean shouldSkipField(FieldAttributes field) {
SerializedName ns = field.getAnnotation(SerializedName.class);
if(ns != null)
return false;
return true;
}
}
p.s. Sorry for my English)
Post a Comment for "Gson Throws An Exception On > 4.0 Devices"