title: Custom POJO serializer using Gson @JsonAdapter annotation
body: <p>I am using the @JsonAdapter annotation of Gson library to serialize/deserialize my JSON objects to POJO.</p>
  
  <p>The deserialization is working fine, but the serialization is giving me this</p>
  
  <pre><code> {
      "name": "ana",
      "email": "test@test.co.uk",
      "address": {
          "address_line1": "123 Some Address",
          "address_line2": "",
          "address_city": "My city",
          "address_postalcode": "CV31 9GP"
      }
  }
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>{
      "name": "ana",
      "email": "test@test.co.uk",
      "address_line1": "123 Some Address",
      "address_line2": "",
      "address_city": "My city",
      "address_postalcode": "CV31 9GP"
  }
  </code></pre>
  
  <p>This is because I'm returning a JsonElement, but I can't see another way to serialize my Address object.</p>
  
  <p>Is there any way to return the outer key-value pairs?</p>
  
  <p><strong>User.java:</strong></p>
  
  <pre><code>public class User() {
      private String name;
      private String email;
  
      @JsonAdapter(AddressSerializer.class)
      private Address address;
      ...
  }
  </code></pre>
  
  <p><strong>AddressSerializer.java</strong></p>
  
  <pre><code>public class AddressSerializer implements JsonSerializer&lt;Address&gt; {
  
      @Override
      public JsonElement serialize(Address src, Type typeOfSrc, JsonSerializationContext context) {
          JsonObject jsonAddress = new JsonObject();
  
          jsonAddress.addProperty("address_line1", src.getAddressLine1());
          jsonAddress.addProperty("address_line2", src.getAddressLine2());
          jsonAddress.addProperty("address_city", src.getCity());
          jsonAddress.addProperty("address_postalcode", src.getPostcode());
  
          return jsonAddress;
      }
  }
  </code></pre>