spray-json - Generic Enum Format

1,731 views
Skip to first unread message

daniel...@gmail.com

unread,
Feb 5, 2014, 8:47:57 AM2/5/14
to spray...@googlegroups.com
Hi,

i'm currently trying to implement a generic formatter for enums

```scala
import spray.json._

object QuestionTypes extends {
  type Status = Status.Value

  object Status extends Enumeration {
    type Status = Value
    val Draft = Value("draft")
    val Open = Value("open")
    val Closed = Value("closed")
  }
}

case class Question(id: Int, status: QuestionTypes.Status)

object EnumProtocol extends DefaultJsonProtocol {

  abstract class EnumJsonFormat[T  <: Enumeration](val enum: T) extends JsonFormat[T] {
    def write(enum: T): JsString = JsString(enum.toString)

    def read(json: JsValue): enum.Value = json match {
      case JsString(value) => enum.withName(value)
      case _ => deserializationError("Enumeration expected")
    }
  }

  implicit object QuestionTypesStatusJsonFormat extends EnumJsonFormat(QuestionTypes.Status)
}

object QuestionProtocol extends DefaultJsonProtocol {
  import EnumProtocol._

  implicit val questionFormat = jsonFormat2(Question)
}
````

But when compiling it fails with 

```could not find implicit value for evidence parameter of type com.starmind.commons.model.json.QuestionProtocol.JF[com.starmind.commons.model.json.QuestionTypes.Status]```

Implementing it as described here https://groups.google.com/d/msg/spray-user/RkIwRIXzDDc/vbXMoZhkY6QJ works without any problems.

Why does my approach not work?

Best regards

Mathias Doenitz

unread,
Feb 5, 2014, 11:04:26 AM2/5/14
to spray...@googlegroups.com
Daniel,

we generally suggest not to use Enumerations in Scala at all.
Implementing a simple abstract data type with a sealed trait and a few case objects is usually the simpler and better alternative with no drawbacks.
Why do you want to work with Enumerations?

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/54591e3d-b450-490b-aba0-4d85a1494d2d%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

daniel...@gmail.com

unread,
Feb 5, 2014, 12:58:49 PM2/5/14
to spray...@googlegroups.com
Hi Mathias,

Enumerations just seemed to be a nice way at places where we have to map existing values from the DB to Scala code and vice-versa.
But as they introduce more trouble than they do help we'll probably follow your suggestion and replace them with 
corresponding case objects or even simple strings in cases where type safety is not critical

Thanks for your reply.

Adam Shannon

unread,
Feb 5, 2014, 2:35:52 PM2/5/14
to spray...@googlegroups.com
There is a related json library I've worked on that has generic scala/java enum formats. Perhaps it'd give you some insight if you wanted/needed to use enums.




For more options, visit https://groups.google.com/groups/opt_out.



--
Adam Shannon
Software Engineer
University of Northern Iowa
Senior -- Computer Science & Mathematics
http://ashannon.us

daniel...@gmail.com

unread,
Feb 6, 2014, 2:02:29 AM2/6/14
to spray...@googlegroups.com

Thanks Adam,

this is really helpful!

There is a related json library I've worked on that has generic scala/java enum formats. Perhaps it'd give you some insight if you wanted/needed to use enums.

Reply all
Reply to author
Forward
0 new messages