Search code examples
jsongenson

Want to include only some fields of an object that are being mapped to JSON by Genson


My Pojo class contains 50 fields and I need to convert only 10 fields to json.

Genson genson = new Genson.Builder().include("address4", User.class).create();
String json = genson.serialize(user);

"include()" method doesn't seem to be working. Kindly help.


Solution

  • Genson genson = new Genson.Builder()
                     .exclude(Object.class)//this excludes all object types
                     .include("address4", User.class).create();//then add only required fields
    String json = genson.serialize(user);