How can I replace the value of a password field with XXX
while de-serializing an object with Gson? I found this post: Gson: How to exclude specific fields from Serialization without annotations that basically skips the field. This would be an option, but I still would prefer to replace the value with XXX
I also tried this:
GsonBuilder builder = new GsonBuilder().setPrettyPrinting();
builder.registerTypeAdapter(String.class, new JsonSerializer<String>(){
@Override public JsonElement serialize(String value, Type arg1, JsonSerializationContext arg2){
// could not find a way to determine the field name
return new JsonPrimitive(value);
}
});
Unfortunately, I wasn't able to determine the name of the field. So is there any other option?
I use Gson to log some objects the "pretty" way, so I don't need to bother with the formatting while reading the logs.