在Scala中使用CIRCE时,如何忽略字段从序列化中
我在Scala中使用CIRCE,并有以下要求:
假设我有一些类似于下面的类,我想避免密码字段序列化,那么是否有任何方法让Circe知道它不应序列化密码字段?
在其他库中,我们有 @transient这样的注释,可以防止字段序列化,CIRCE中是否有这样的注释?
case class Employee(
name: String,
password: String)
I am using circe in scala and have a following requirement :
Let's say I have some class like below and I want to avoid password field from being serialised then is there any way to let circe know that it should not serialise password field?
In other libraries we have annotations like @transient which prevent field from being serialised ,is there any such annotation in circe?
case class Employee(
name: String,
password: String)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以制作一个自定义编码器,以编辑某些字段:
以后更新
,以避免从Semiauto/Auto解码器中浏览所有字段:
You could make a custom encoder that redacts some fields:
LATER UPDATE
In order to avoid going through all fields contramap from a semiauto/auto decoder:
尽管 @gatear的答案很有用,但实际上并没有回答这个问题。
不幸的是,CIRCE(至少直到版本0.14.2)没有注释可以忽略字段。到目前为止,只有一个注释(
@jsonkey
),这用于重命名字段名称。为了在序列化(CIRCE调用编码)时忽略字段,您可以在
encoder
实现中避免该字段。因此,而不是包括
密码
字段:您要使用它:
我一直使用的是创建一个较小的
case类
,其中仅包括我感兴趣的字段。然后,我让CIRCE的自动派生使用io.circe.generic.auto ._
:Although @gatear's answer is useful, it doesn't actually answer the question.
Unfortunately Circe (at least until version 0.14.2) does not have annotations to ignore fields. So far there is only a single annotation (
@JsonKey
) and this is used to rename field names.In order to ignore a field when serialising (which Circe calls encoding) you can avoid that field in the
Encoder
implementation.So instead of including the
password
field:you ommit it:
Alternatively what I've been using is creating a smaller
case class
which only includes the fields I'm interested in. Then I let Circe's automatic derivation kick in withio.circe.generic.auto._
: