Noise free JSON format for sealed traits with Play 2.2 library

前端 未结 4 2009
醉梦人生
醉梦人生 2020-12-24 12:16

I need to get a simple JSON serialization solution with minimum ceremony. So I was quite happy finding this forthcoming Play 2.2 library. This works perfectly with plain cas

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 12:49

    Play 2.7

    sealed traits are supported in play-json.

    object Foo{
      implicit val format = Json.format[Foo]
    }
    

    Play 2.6

    This can be done now elegantly with play-json-derived-codecs

    Just add this:

    object Foo{
        implicit val jsonFormat: OFormat[Foo] = derived.oformat[Foo]()
    }
    

    See here for the whole example: ScalaFiddle

提交回复
热议问题