title: How to deserialize a nested JSON object as a String with Gson, Retrofit, Active Android
body: <p>I am using a ActiveAndroid to cache response from Retrofit network calls.</p>
  
  <p>I made a class which looks like :-</p>
  
  <pre><code>class Article extends Model {
      @Column(name = "title")
      @SerializedName("title")
      @Expose
      public String title;
  
      @Column(name = "authors")
      @SerializedName("authors")
      @Expose
      private String authors;
  }
  </code></pre>
  
  <p>The corresponding JSON is something like :-</p>
  
  <pre><code>{
    "title":"Some title",
    "authors": [{
      "name": "Name",
      "twitter":"@whatever"
    }]
  }
  </code></pre>
  
  <p>The sane way would be to use a has many relationship between <code>Author</code> and <code>Article</code> class, but authors are not being saved to database because they don't have <code>@Expose</code> annotation (or even a variable) in the <code>Article</code> class, and there is no way to have that annotation because has many calls will be a method call here which returns a <code>List&lt;Author&gt;</code>.</p>
  
  <pre><code>// This will not work
  @Expose
  public List&lt;Author&gt; authors() {
      return getMany(Author.class, "Article");
  }
  </code></pre>
  
  <p>So the other way I thought was to store the JSON object in database as a string and serialize/deserialize for access.</p>
  
  <p>That requires me to have the <code>authors</code> variable a <code>List&lt;Author&gt;</code> class, and provide a <a href="https://github.com/pardom/ActiveAndroid/wiki/Type-serializers" rel="nofollow noreferrer">type serializer</a> for it.</p>
  
  <p>The problem with this approach is I have no way of knowing the Generic type being saved into the database. I can't return a <code>List&lt;Author&gt;</code> type because just like authors, there are some more JSON Arrays in the whole of JSON.</p>
  
  <p>So my the final solution that I have thought of is to store it as a string, treat it as a string and make a method to deserialize for access. Like this :-</p>
  
  <pre><code>private List&lt;Author&gt; _authors;
  public List&lt;Author&gt; authors() {
      if (_authors == null) {
          Gson gson = ServiceGenerator.gsonBuilder.create();
          Type type = new TypeToken&lt;List&lt;Author&gt;&gt;(){}.getType();
          _authors = gson.fromJson(authors, type);
      }
  
      return _authors;
  }
  </code></pre>
  
  <p>But when I try to do this, GSON throws an error :-</p>
  
  <p><code>com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY</code></p>
  
  <p>Basically saying that I am trying to treat a JSON array as a String.</p>
  
  <p>So my question is how to treat some of the JSON Arrays as Strings with GSON converters.</p>
  
  <p>My Retrofit Builder which I am using is :-</p>
  
  <pre><code>public static final GsonBuilder gsonBuilder = new GsonBuilder()
              .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
              .excludeFieldsWithoutExposeAnnotation();
  
  private static final Retrofit.Builder builder = new Retrofit.Builder()
          .baseUrl(API_BASE_URL)
          .addConverterFactory(GsonConverterFactory.create(gsonBuilder.create()));
  </code></pre>
  
  <p>Thanks for taking a look!</p>