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

前端 未结 5 1197
有刺的猬
有刺的猬 2020-12-29 20:43

Given the following enumeration...

object MyEnum extends Enumeration {

  type MyEnum = Value

  val Val1 = Value(\"val1\")
  val Val2 = Value(\"val2\")
  va         


        
5条回答
  •  梦毁少年i
    2020-12-29 21:05

    Play 2.7

    Since play-json 2.7 there is Json.formatEnum method. Added in scope of #140

    Example:

    object MyEnum extends Enumeration {
      type MyEnum = Value
    
      val Val1 = Value("val1")
      val Val2 = Value("val2")
      val ValN = Value("valN")
    
      implicit val format = Json.formatEnum(this)
    }
    

提交回复
热议问题