Java Gson序列化期间排除字段 [英] Java Gson Exclude fields during serialization

查看:97
本文介绍了Java Gson序列化期间排除字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ConfigInstance 类,它包含一个密码和一个 password_hash
现在我想使用gson序列化对象,但不包括密码字段。

  public class ConfigInstance {
public String database_address;
public int database_port;
public String database_user;

@Expose(serialize = false)
private String database_pass;
public String database_pass_hash;

public String GetPass(){return database_pass; }
$ b $ public void Encrypt(){/ *在序列化之前创建哈希* /}
$ b $ public void Decrypt(){/ *在反序列化之后创建密码* /}





$ b

正如你所看到的,我已经尝试过使用 @Expose (serialize = false)但它似乎没有做任何事情。此外,我已将该字段设置为私有,因为我认为这会覆盖 @Expose



,但正在运行以下代码:

  private void toFile(File file,ConfigInstance map){
map.Encrypt();
Gson gson = new GsonBuilder()。setPrettyPrinting()。create();
String jsonConfig = gson.toJson(map);
FileWriter作家;
尝试{
writer = new FileWriter(file);
writer.write(jsonConfig);
writer.flush();
writer.close();
} catch(IOException e){
System.out.println(Error exporting config:+ e.toString());


$ / code>

仍然会导致以下文件内容没有错误:

  {
database_address:127.0.0.1,
database_port:1521,
database_user:test,
database_pass:test1234,
database_pass_hash:B9FE2C011B59F0D0D383D70073E48A19
}

那么我做错了什么?我现在很无助,并希望得到任何帮助,因为似乎并不工作。



预先感谢。

解决方案

这个结果,你需要使用 @Expose 注释所有的字段:

  public class ConfigInstance {

@Expose
public String database_address;
@Expose
public int database_port;
@Expose
public String database_user;

@Expose(serialize = false)
private String database_pass;
@Expose
public String database_pass_hash;

并且配置Gson只暴露注释的字段并忽略其余部分,如下所示: / p>

  Gson gson = new GsonBuilder()。excludeFieldsWithoutExposeAnnotation()。setPrettyPrinting()。create(); 

然后,您会得到:

 {
database_address:127.0.0.1,
database_port:1521,
database_user:test,
database_pass_hash:B9FE2C011B59F0D0D383D70073E48A19
}

另外,当对字符串进行反序列化时,






不过,您可以配置 Gson Serializer 来完成这项工作。


I have a ConfigInstance class which contains a password and a password_hash. Now I want to serialize the object using gson but exclude the password field.

public class ConfigInstance {
    public String database_address;
    public int database_port;
    public String database_user;

    @Expose(serialize = false)
    private String database_pass;
    public String database_pass_hash;

    public String GetPass() { return database_pass; }

    public void Encrypt() { /* Creates the hash before serializing*/ }

    public void Decrypt() { /* Creates the password after deserializing */}
}

As you can see, I have tried using @Expose(serialize = false) but it doesn't seem to do anything. Also I already set the field to private since I figured that this would "override" the @Expose

but running following code:

private void toFile(File file, ConfigInstance map) {
    map.Encrypt();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonConfig = gson.toJson(map);
    FileWriter writer;
    try {
        writer = new FileWriter(file);
        writer.write(jsonConfig);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        System.out.println("Error exporting config: " + e.toString());
    }
}

still results in the following file content without Errors:

{
  "database_address": "127.0.0.1",
  "database_port": 1521,
  "database_user": "test",
  "database_pass": "test1234",
  "database_pass_hash": "B9FE2C011B59F0D0D383D70073E48A19"
}

So what am I doing wrong? I am pretty clueless right now and would appreciate any help since THIS doesn't seem to work.

Thanks in advance.

解决方案

In order to get this result, you need to annotate all the fields with the @Expose:

public class ConfigInstance {

    @Expose
    public String database_address;
    @Expose
    public int database_port;
    @Expose
    public String database_user;

    @Expose(serialize = false)
    private String database_pass;
    @Expose
    public String database_pass_hash;

And configure Gson to only expose fields that are annotated and ignore the rest as shown in the following:

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();

Then, you'll get:

{
  "database_address": "127.0.0.1",
  "database_port": 1521,
  "database_user": "test",
  "database_pass_hash": "B9FE2C011B59F0D0D383D70073E48A19"
}

Also, when deserialising the string you'll still have the password attribute as well.


Still, you have the possibility to configure a Gson Serializer to accomplish this.

这篇关于Java Gson序列化期间排除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆