title: gson nested custom serializer
body: <p>I've a nested object that I cannot manage to serialize with gson. I made a custom serializer which is used with the parent object but with the child one I didn't manage to make it work.</p>
  
  <p>So in my code below the custom serializer is used to serialize a <code>Team</code>. That works. In this team I have a list of <code>User</code> that I want to serialize as well. Unfortunately the method <code>addProperty(String a, String b)</code> askes for 2 strings (or a primitive) so I cannot add a property members which is of the class <code>User</code>.</p>
  
  <p>Here is the code, first I create a <code>Gson</code> object and register the custom serializer for <code>Team</code>:</p>
  
  <pre><code>gson = new GsonBuilder().registerTypeAdapter(Team.class, new TeamSerializer())
              .create();
   gson.toJson(userData); // UserData instance.
  </code></pre>
  
  <p>This is the class that is gonna be serialized with the <code>Gson</code> object (I omitted irrelevant fields symbolized by //...):</p>
  
  <pre><code>public class UserData{
      public Team team;
      //...
  }
  </code></pre>
  
  <p>This is the custom serializer where I cannot add the property <code>members</code>:</p>
  
  <pre><code>public class TeamSerializer implements JsonSerializer&lt;Team&gt;{
      @Override
      public JsonElement serialize(Team src, Type type, JsonSerializationContext context) {
          JsonObject object = new JsonObject();
          object.addProperty("id", src.getIdteams());
          object.addProperty("name", src.getName());
          object.addProperty("tag", src.getTag());
          object.addProperty("picture", src.getProfilePic());
          //object.addProperty("members", src.getMembers());
          return object;
      }
  }
  </code></pre>
  
  <p>This is the <code>Team</code> class:</p>
  
  <pre><code>public class Team implements Serializable {
      private long idteams;
      private String name;
      private String tag;
      private String profilePic;
      private List&lt;User&gt; members;
      //...
  }
  </code></pre>