Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy api to create custom en-/decoders #689

Open
987Nabil opened this issue Jul 26, 2022 · 2 comments
Open

Easy api to create custom en-/decoders #689

987Nabil opened this issue Jul 26, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@987Nabil
Copy link
Contributor

987Nabil commented Jul 26, 2022

I do understand, that the main way to create en-/decoders are macros.
But in my experience, you always have some cases, where you'd like to have some custom ones.

I thought of something like the play json api to create reads/writes.

Or in the lights of Scala 3 context functions maybe something like

case class User(name: String, email: EmailAddress)
object User:
  implicit val decoder: JsonDecoder[User] =
    decoder {
      obj {
        field[String]("name"),
        field[EmailAddress]("email")
      }.apply { (name, email) => User(name, email) }
    }

This is more a basic idea and needs discussion, I guess.

Edit
JsonCursor could maybe be used to describe the structure of the data.
And to clarify this: I am talking about having a static description and generate out of it en-/decoders, that do not convert to the json ast first.

@plokhotnyuk
Copy link
Contributor

@987Nabil You can use macrolizer.show to print code of generated codecs.

@fsvehla fsvehla added the enhancement New feature or request label Aug 8, 2022
@Kalin-Rudnicki
Copy link
Contributor

I imagine you mean something like circe's ProductDecoders?

  final case class Person(firstName: String, lastName: String, age: Int)

  io.circe.Decoder.forProduct3[Person, String, String, Option[Int]](
    "firstName",
    "lastName",
    "age",
  ) { (firstName, lastName, age) =>
    Person(firstName, lastName, age.getOrElse(0))
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants