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

Example for value classes in docs #1113

Open
mtsokol opened this issue Apr 19, 2019 · 2 comments
Open

Example for value classes in docs #1113

mtsokol opened this issue Apr 19, 2019 · 2 comments

Comments

@mtsokol
Copy link

mtsokol commented Apr 19, 2019

Hi,
I've recently tried to handle value classes in circe without nested structure.
Only found issue #469 which lacks in full example.
I've figured it out, but as circe beginner I wish I found it in docs (e.g. in semi-automatic-derivation).

Would you mind accepting such docs pull request?

import io.circe.Encoder
import io.circe.syntax._
import io.circe.generic.extras.semiauto._
import io.circe.generic.extras.defaults._

implicit val UserEncoder: Encoder[User]         = deriveEncoder
implicit val UserIdEncoder: Encoder[UserId]     = deriveUnwrappedEncoder
implicit val UserNameEncoder: Encoder[UserName] = deriveUnwrappedEncoder

case class UserId(id: Int) extends AnyVal
case class UserName(name: String) extends AnyVal
case class User(userId: UserId, userName: UserName)

val usr = User(UserId(1446), UserName("User Name"))

usr.asJson
//  res0: io.circe.Json =
//  {
//    "userId" : 1446,
//    "userName" : "User Name"
//  }
@captainju
Copy link

Same for the decoder counterpart.

@GMadorell
Copy link

Thanks for putting this issue out there! For me it was the only way to find it in google 👍 .
Also, personally I would like for being able to automatically use this as the default option (maybe by using a different import?) for automatically derivating the codecs. In the project I'm currently working on 100% of the encoders and decoders are like this.

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

No branches or pull requests

3 participants