Deserialize nested object with GSON
<p>I'm trying to deserialize  the following structure</p>

<pre><code>{ meta: { keywords:  [a, b, c, d]}  ...  }
</code></pre>

<p>other valid structures are</p>

<pre><code>{ meta: { keywords: "a,b,c,d"}  ... }
</code></pre>

<p>and</p>

<pre><code>{ meta: {keywords: "a"}  ...}
</code></pre>

<p>I have this classes</p>

<pre><code>public class Data {
   @PropertyName("meta")
   MetaData meta;
   ...
}


public class  MetaData {
    List&lt;String&gt; keywords;
    ...
}
</code></pre>

<p>and a custom deserializer</p>

<pre><code>public static class CustomDeserilizer implements JsonDeserializer&lt;MetaData&gt;{
    @Override
    public MetaData deserialize(JsonElement json, Type typeOfT,  JsonDeserializationContext context) throws JsonParseException {
        List&lt;String&gt; keywords = null;
        Gson gson = new Gson();
        MetaData metaData = gson.fromJson(json, AppMetaData.class);
        JsonObject jsonObject = json.getAsJsonObject();

        if (jsonObject.has("keywords")) {
            JsonElement elem = jsonObject.get("keywords");
            if (elem != null &amp;&amp; !elem.isJsonNull()) {

                if (jsonObject.get("keywords").isJsonArray()) {
                    keywords = gson.fromJson(jsonObject.get("keywords"),   new TypeToken&lt;List&lt;String&gt;&gt;() {
                    }.getType());
                } else {
                    String keywordString = gson.fromJson(jsonObject.get("keywords"), String.class);
                    keywords = new ArrayList&lt;String&gt;();
                    list.addAll(Arrays.asList(keywordString.split(",")));
                }
            }
        }
       metaData.setKeywords(keywords);
}
</code></pre>

<p>Then I try to apply the deserilizer:</p>

<pre><code>Gson gson = new GsonBuilder()              
        .registerTypeAdapter(Data.class,new CustomDeserilizer())               
        .create();
</code></pre>

<p>But I get a parsing error , because is trying to deserialize Data instead of MetaData, how can I apply this deserializer to make it work right?</p>