Java Gson序列化期间排除字段

编程入门 行业动态 更新时间:2024-10-15 18:25:24
本文介绍了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();

然后,您会得到:

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

不过,您可以配置 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序列化期间排除字段

本文发布于:2023-11-17 14:58:48,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   序列化   Java   Gson

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!