Gson: skip node level for serialization

Viewed 100

I have some json which contains fields with custom names, e.g.:

{
   "user_id": 123,
   "user_name": "John",
   "field_with_custom_name_1": "value1",
   "field_with_custom_name_2": "value2",
   "field_with_custom_name_3": "value3"
}

For describe this json the next model was created:

public class UserData {

   @SerializedName("user_id")
   private int userId;

   @SerializedName("user_name")
   private String userName;

   private Map<String, String> customFields;
}

But after serialization we have json with the next structure:

{
   "user_id": 123,
   "user_name": "John",
   "customFields": {
      "field_with_custom_name_1": "value1",
      "field_with_custom_name_2": "value2",
      "field_with_custom_name_3": "value3"
   }
}

Can you suggest how to ignore "customFields" level in the result?

1 Answers

You can use the following annotation to exclude a field from serialization and deserialization:

@Expose (serialize = false, deserialize = false)
Related