在Scala中使用CIRCE时,如何忽略字段从序列化中

发布于 2025-01-29 03:25:44 字数 290 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩余の解释 2025-02-05 03:25:44

您可以制作一个自定义编码器,以编辑某些字段:

implicit val encodeEmployee: Encoder[Employee] = new Encoder[Employee] {
  final def apply(a: Employee): Json = Json.obj(
    ("name", Json.fromString(a.name)),
    ("password", Json.fromString("[REDACTED]")),
  )
}

以后更新

,以避免从Semiauto/Auto解码器中浏览所有字段:

import io.circe.generic.semiauto._

implicit val encodeEmployee: Encoder[Employee] =
  deriveEncoder[Employee]
    .contramap[Employee](unredacted => unredacted.copy(password = "[REDACTED]"))

You could make a custom encoder that redacts some fields:

implicit val encodeEmployee: Encoder[Employee] = new Encoder[Employee] {
  final def apply(a: Employee): Json = Json.obj(
    ("name", Json.fromString(a.name)),
    ("password", Json.fromString("[REDACTED]")),
  )
}

LATER UPDATE

In order to avoid going through all fields contramap from a semiauto/auto decoder:

import io.circe.generic.semiauto._

implicit val encodeEmployee: Encoder[Employee] =
  deriveEncoder[Employee]
    .contramap[Employee](unredacted => unredacted.copy(password = "[REDACTED]"))
二手情话 2025-02-05 03:25:44

尽管 @gatear的答案很有用,但实际上并没有回答这个问题。

不幸的是,CIRCE(至少直到版本0.14.2)没有注释可以忽略字段。到目前为止,只有一个注释(@jsonkey),这用于重命名字段名称。

为了在序列化(CIRCE调用编码)时忽略字段,您可以在encoder实现中避免该字段。

因此,而不是包括密码字段:

implicit val employeeEncoder: Encoder[Employee] =
  Encoder.forProduct2("name", "password")(employee => (employee.name, employee.password))

您要使用它:

implicit val employeeEncoder: Encoder[Employee] =
  Encoder.forProduct1("name")(employee => (u.name))

我一直使用的是创建一个较小的case类,其中仅包括我感兴趣的字段。然后,我让CIRCE的自动派生使用io.circe.generic.auto ._

import io.circe.generic.auto._
import io.circe.syntax._

case class EmployeeToEncode(name: String)

// Then given an employee object:
EmployeeToEncode(employee.name).asJson

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:

implicit val employeeEncoder: Encoder[Employee] =
  Encoder.forProduct2("name", "password")(employee => (employee.name, employee.password))

you ommit it:

implicit val employeeEncoder: Encoder[Employee] =
  Encoder.forProduct1("name")(employee => (u.name))

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 with io.circe.generic.auto._:

import io.circe.generic.auto._
import io.circe.syntax._

case class EmployeeToEncode(name: String)

// Then given an employee object:
EmployeeToEncode(employee.name).asJson
少年亿悲伤 2025-02-05 03:25:44
deriveEncoder.mapJsonObject(_.remove("password"))
deriveEncoder.mapJsonObject(_.remove("password"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文