Gson deserialization - attribute might be array or single object
<p>Using Gson (Android) to deserialize an external web-api I stumbled upon a problem.</p>

<p>Attribute <code>A</code> is a list of objects, as such:</p>

<pre><code>{
    A: [
       { /* object */ },
       { /* object */ },
    ]
}
</code></pre>

<p>However when <code>A</code> only contains one object the API returns it as a single object, like this:</p>

<pre><code>{
    A: { /* object */ }
}
</code></pre>

<p>In my target class I have <code>A</code> defined like this:</p>

<pre><code>public class Foo {
    private AObject[] A;
}
</code></pre>

<p>This works fine for when attribute is a proper array but fails with following exception for the single-object version:</p>

<pre><code>com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
</code></pre>

<p>Is the any simple way to deserialize the attribute to a array containing only one item? Or do I have to loop through the response and change it before deserialization?</p>

<p>Thanks</p>