Reputation: 3007
Seems to a bug filed here: https://github.com/google/gson/issues/513
The trouble is I don't know any workarounds, I am fairly new to Gson. I have a boolean field in my POJO that I set myself, it doesn't come in the JSON, I set to true
, and when the object is parsed by Gson, its' set back to false
. Behaviour that should not be happening.
This is what I have tried so far:
Gson gson = new GsonBuilder()
.serializeNulls()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getName().equals("reviewed");
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();
As you can see I have tried skipped the field reviewed
, however it's still being set to false
. I've also tried adding breakpoints inside the shouldSkipField
method and it does indeed return true
when it tries to parse reviewed
. So I really don't understand, all I can chalk it up to is the bug I posted above.
Does anyone have a better solution? It seems the bug has been around for a while.
Appreciate the help.
Edit:
Here is how it looks in my POJO
private boolean reviewed;
//Getters and setters
public boolean isReviewed() {
return reviewed;
}
public void setReviewed(boolean reviewed) {
this.reviewed = reviewed;
}
Upvotes: 0
Views: 211
Reputation: 2249
Gson does not really "reset" the field to its default value. It actually creates a new instance from the class you provide for it. and then sets all the fields that come from json to your object.
Here's what you should do. You have to set the reviewed
field in the same instance that is returned from the json.
Upvotes: 1