-
Notifications
You must be signed in to change notification settings - Fork 190
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
Standard format for Enumeration #200
Comments
I'd like to see said implementation, even if it's not part of the project, I could use it :) |
Since I ended up finding this after digging around on the internet for an example, I'll paste the example I found import spray.json.{DeserializationException, JsString, JsValue, RootJsonFormat}
/**
* Based on the code found: https://groups.google.com/forum/#!topic/spray-user/RkIwRIXzDDc
*/
class EnumJsonConverter[T <: scala.Enumeration](enu: T) extends RootJsonFormat[T#Value] {
override def write(obj: T#Value): JsValue = JsString(obj.toString)
override def read(json: JsValue): T#Value = {
json match {
case JsString(txt) => enu.withName(txt)
case somethingElse => throw DeserializationException(s"Expected a value from enum $enu instead of $somethingElse")
}
}
} Use like: implicit val enumConverter = new EnumJsonConverter(YourEnum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would it be useful to provide a standard format for values of an arbitrary
Enumeration
type?I have a working implementation, though it relies on an implicit type evidence of the specific enum type provided by the user. I could use reflection instead, but that seems less desirable.
The text was updated successfully, but these errors were encountered: