Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've found, that if your instance <strong>t</strong> extends from any <strong>android</strong> view component(such as Button, for example) or has field of class, that extends from any <strong>android</strong> view component, you will get such exception</p> <p>For some reason, this library can't serialize <strong>view</strong> components any more. So, you can add exclude strategy. For example, this strategy will exclude all fields, except fields with Annotation @SerializedName:</p> <pre><code>class Exclude implements ExclusionStrategy { @Override public boolean shouldSkipClass(Class&lt;?&gt; 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; }} </code></pre> <p>Suppose we have sample Json:</p> <pre><code>private final String jsonSample= "{ \"Sample\": { \"field1\":1, \"field2\":2}}"; </code></pre> <p>here is sample code:</p> <pre><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; &lt;-- Uncomment this line, and you will get your error! class Smpl{ @SerializedName("field1") int fl1; @SerializedName("field2") int fl2; } } </code></pre> <p>But after adding <strong>exclude strategy</strong> I've written above:</p> <pre><code> Exclude ex = new Exclude(); Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create(); </code></pre> <p>All works correct.</p> <p>Finally we has the following test app:</p> <pre><code>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(); &lt;-- 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; // &lt;-- 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&lt;?&gt; 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; } } </code></pre> <p>p.s. Sorry for my English)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload