ticofab
ticofab

Reputation: 7717

Scala Play Json Format for Map[Locale, String]

I have objects of type

Map[java.util.Locale, String] 

How can I make Json Writes / Reads for this? I have looked at a couple other questions, but couldn't come up with a solution myself. I got (but not tested yet) something for Locale

implicit val localeReads: Reads[Locale] = new Reads[Locale] {
  def reads(json: JsValue): JsResult[Locale] =
    json match {
      case JsString(langString) => JsSuccess(new Locale(langString))
      case _ => JsError("Locale Language String Expected")
    }
}

implicit val localeWrites: Writes[Locale] = new Writes[Locale] {
  def writes(locale: Locale) = JsString(locale.toString)
}

How can I then use this in

implicit val myMapReads: Reads[Map[Locale, String]] = ???
implicit val myMapWrites: Writes[Map[Locale, String]] = ???

?

Upvotes: 4

Views: 1622

Answers (2)

Joost de Vries
Joost de Vries

Reputation: 640

Here's a function that will create a Format[K,V] for you if K serialises to a JsString:

/** Play Json only supports Map[String,V]. This function creates a format for Map[K,V]. The type K should produce a JsString.
 * Otherwise the serialisation will fail. Which should be good enough since in valid json keys can only be strings. 
*/
def mapFormat[K, V](implicit fk: Format[K], fv: Format[V]): Format[Map[K, V]] = 
  new OFormat[Map[K, V]] {
    override def writes(o: Map[K, V]): JsObject = {
      val stringMap = o.map { case (k, v) => (Json.toJson[K](k).as[JsString].value, v) }
      Json.toJson(stringMap).as[JsObject]
    }

    override def reads(json: JsValue): JsResult[Map[K, V]] = {
      for {
        stringMap <- Json.fromJson[Map[String, V]](json)
        _         <- Json.fromJson[Set[K]](Json.toJson(stringMap.keySet))
      } yield stringMap.map { case (k, v) => (Json.fromJson[K](JsString(k)).get, v) }
    }
  }

Upvotes: 0

Ende Neu
Ende Neu

Reputation: 15783

This should work:

implicit val localeReads: Reads[Locale] = new Reads[Locale] {
  def reads(json: JsValue): JsResult[Locale] =
    json match {
      case JsString(langString) => JsSuccess(new Locale(langString))
      case _ => JsError("Locale Language String Expected")
    }
}

implicit val localeWrites: Writes[Locale] = new Writes[Locale] {
  def writes(locale: Locale) = JsString(locale.toString)
}

implicit val myMapWrites: Writes[Map[Locale, String]] = new Writes[Map[Locale, String]] {
  override def writes(o: Map[Locale, String]): JsValue = Json.toJson(o)
}

implicit val myMapRead: Reads[Map[Locale, String]] = new Reads[Map[Locale, String]] {
  override def reads(json: JsValue): JsResult[Map[Locale, String]] = JsSuccess {
    json.as[JsObject].value.map {
      case (k, v) => (new Locale(k), v.as[String])
    }.toMap
  }
}

Basically play already knows how to convert a Locale to json because of the Writes you provided so simply calling toJson will work.

For the Reads it's a bit more complex and you have to do a mapping, .value returns a Map[String, JsValue] where the first represents the Locale object and the second a simple string so calling as[String] will already give you what you want.

Note that I've wrapped everything in a JsSuccess but you may assume that the json you got cannot be converted to JsObject, apply a try/catch, and decide wether you want to return success or failure.

Upvotes: 2

Related Questions