question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Case class with Enumeration is not converted

See original GitHub issue

I’m trying to convert a case class that has a Enumeration as a argument

import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
object WeekDay extends Enumeration {
  val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
case class test1(day: WeekDay.Value)
case class test2(day: String)

While test2(“Mon”).asJson works as expected:

{
  "day" : "Mon"
}

But: test1(WeekDay.Mon).asJson results in:

<console>:27: error: could not find implicit value for parameter encoder: io.circe.Encoder[test] test(WeekDay.Mon).asJson

Is there a way to work around this or is this a bug?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dhenschecommented, Jun 12, 2016

I’m not seeing anywhere in the project where basic scala Enumerations have a provided Encoder/Decoder instance. However, following how the json4s handled this I came up with the following

class EnumTranscoder[E <: Enumeration](enum: E) extends Decoder[E#Value] with Encoder[E#Value] {
  override def apply(c: HCursor): Result[E#Value] = Decoder.decodeString.map(enum.withName).apply(c)

  override def apply(a: E#Value): Json = Encoder.encodeString.apply(a.toString)
}
0reactions
travisbrowncommented, Jul 8, 2016

Yes, we’d want them to be in the companion objects so that they get made available without additional imports. Thanks, @mariussoutier!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Case objects vs Enumerations in Scala - Stack Overflow
Case classes naturally supports more fields than a Value based Enumeration which supports a name and ID. The advantages of using Enumerations ......
Read more >
Case Objects vs Enumerations in Scala - Baeldung
An enumerated type is a data type that contains only a finite set of named values, and they are supported in most modern...
Read more >
Enumerations
Enum case declarations are similar to secondary constructors: they are scoped outside of the enum template, despite being declared within it. This means...
Read more >
enum — Support for enumerations — Python 3.11.1 ...
The class Color is an enumeration (or enum) · The attributes Color.RED , Color.GREEN , etc., are enumeration members (or members) and are...
Read more >
PHP 8.1: Enums
The UnitEnum::cases method returns an array of all cases of a given Enum. PHP classes are not allowed to implement this interface, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found