I'm trying to serialize one very basic object to JSON with Gson.
Here is the class
@org.greenrobot.greendao.annotation.Entity
public class Giveaway {
@Id(autoincrement = true)
@Expose(serialize = false,deserialize = false)
private Long id;
@NotNull
private String owner;
private Date raffleDate;
private String thumbnailUrl;
@ToMany(referencedJoinProperty = "giveawayId")
private List<Influencer> mustFollowList;
@NotNull
@Convert(converter = GiveawayCommentTypeConverter.class, columnType = Integer.class)
private GiveawayCommentType tipo;
private String specifWordValue;
private Integer amountFriendsToIndicate;
@NotNull
@Unique
private String mediaId;
//to reflect the relationships
@ToMany(referencedJoinProperty = "raffle")
@Expose(deserialize = false, serialize = false)
private List<UserOnGiveaway> attendantsTickets;
}
As you can see I've 2 fields that i DONT WANT to be serialized so I annotated them with expose = false
, but even with this Gson is trying to serialize them and crashing due OutOfMemory
. (UserOnGiveaway has a circular reference with Giveaway and this explains why it crashes.)
The Gson code is:
Gson parser = new GsonBuilder().setPrettyPrinting().excludeFieldsWithModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.TRANSIENT).create();
StringBuilder sb = new StringBuilder(200);
try {
for (Giveaway g : this.dao.getGiveawayDao().loadAll())
sb.append(parser.toJson(g) + "\n");
} catch (Exception e) {
e.printStackTrace();
}
I didn't want to use .excludeFieldsWithoutExposeAnnotation()
since it forces me to write way more than necessary and anotate everything just to exclude 1 field...
What am I doing wrong?