This is a solution with a custom reader for the MetaTag
class. The read just convert the JsValue
to a JsObject
, which have the useful fieldSet
method.
For MetaTags
, the macro inception works perfectly
object Application extends Controller {
case class MetaTags(description: String, tags: List[MetaTag])
case class MetaTag(key: String, data: List[String])
implicit val readMetaTag = Reads(js =>
JsSuccess(js.as[JsObject].fieldSet.map(tag =>
MetaTag(tag._1, tag._2.as[List[String]])).toList))
implicit val readMetaTags = Json.reads[MetaTags]
def index = Action {
val json = Json.obj(
"description" -> "Hello world",
"tags" -> Map(
"key1" -> Seq("key1a", "key1b"),
"key2" -> Seq("key2a"),
"key3" -> Seq("Key3a", "key3b", "key3c")))
val meta = json.as[MetaTags]
Ok(meta.tags.map(_.data.mkString(",")).mkString("/"))
// key1a,key1b/key2a/Key3a,key3b,key3c
}
}