java gson在序列化时替换密码值 [英] java gson replace password value while serialization
问题描述
在使用Gson反序列化对象时,如何用 XXX
替换密码字段的值?我发现这篇文章: Gson:如何排除特定字段从序列化没有注释,基本上跳过这个领域。这将是一个选择,但我仍然希望用 XXX
来替换值。我也试过这个:
GsonBuilder builder = new GsonBuilder()。setPrettyPrinting();
builder.registerTypeAdapter(String.class,new JsonSerializer< String>(){
@Override public JsonElement serialize(String value,Type arg1,JsonSerializationContext arg2){
//找不到确定字段名称的方法
return new JsonPrimitive(value);
}
});
不幸的是,我无法确定该字段的名称。那么还有其他选择吗?
我使用Gson以漂亮的方式记录一些对象,所以在阅读日志时我不需要格式化。
发布此答案时,我感到非常跛脚。但是,这是你可以做到的,它基本上是在序列化之前复制和更改Java对象。
pre $
public class User {
private static final Gson gson = new Gson();
公共字符串名称;
公共字符串密码;
$ b $ public public User(String name,String pwd){
this.name = name;
this.password = pwd;
$ b @Override
保护对象clone()抛出CloneNotSupportedException {
返回新用户(this.name,this.password);
}
public static void main(String [] aa){
JsonSerializer< User> ser = new JsonSerializer< User>(){
@Override
public JsonElement serialize(User u,Type t,JsonSerializationContext ctx){
try {
User clone = u.clone();
clone.password = clone.password.replaceAll(。,x);
return(gson.toJsonTree(clone,User.class));
} catch(CloneNotSupportedException e){
//如果你不克隆克隆,请做些什么。
}
返回gson.toJsonTree(u,User.class);
}
};
Gson g = new GsonBuilder()。registerTypeAdapter(User.class,ser).create();
System.out.println(g.toJson(new User(naishe,S3cr37)));
获得序列化为:
{name:naishe,password:xxxxxx}
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.
解决方案 I feel pretty lame while posting this answer. But, it's what you can, it essentially copies and changes the Java object, before serializing.
public class User {
private static final Gson gson = new Gson();
public String name;
public String password;
public User(String name, String pwd){
this.name = name;
this.password = pwd;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return new User(this.name, this.password);
}
public static void main(String[] aa){
JsonSerializer<User> ser = new JsonSerializer<User>() {
@Override
public JsonElement serialize(User u, Type t, JsonSerializationContext ctx) {
try {
User clone = (User)u.clone();
clone.password = clone.password.replaceAll(".","x");
return (gson.toJsonTree(clone, User.class));
} catch (CloneNotSupportedException e) {
//do something if you dont liek clone.
}
return gson.toJsonTree(u, User.class);
}
};
Gson g = new GsonBuilder().registerTypeAdapter(User.class, ser).create();
System.out.println(g.toJson(new User("naishe", "S3cr37")));
}
}
Gets serialized to:
{"name":"naishe","password":"xxxxxx"}
这篇关于java gson在序列化时替换密码值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!