Play Framework: How to serialize/deserialize an enumeration to/from JSON Play Framework: How to serialize/deserialize an enumeration to/from JSON json json

Play Framework: How to serialize/deserialize an enumeration to/from JSON


implicit val genderReads = Reads.enumNameReads(Gender) is working fine for me. Play Scala 2.4.2


Try changing it to

def reads(json: JsValue) = JsSuccess(MyEnum.withName(json.as[String].value))


Expanding on @surenyonjan's response, the following works nicely with Play Json 2.6:

object MyEnum extends Enumeration {  type MyEnum = Value  val e1, e2 = Value  implicit val myEnumReads = Reads.enumNameReads(MyEnum)  implicit val myEnumWrites = Writes.enumNameWrites}