Sujit Kamthe
Sujit Kamthe

Reputation: 961

No instance of play.api.libs.json.Format is available for scala.Predef.Map[java.lang.String, scala.Option[scala.Double]]

Trying to write a json format for an entity which contains a Map of Option. It throws following error

Error:(8, 68) No instance of play.api.libs.json.Format is available for scala.Predef.Map[java.lang.String, scala.Option[scala.Double]] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)

Code snippet:

import play.api.libs.json.{Json, OFormat}

val a: Map[String, Option[Double]] = Map("a" -> None)

case class Person(x: Map[String, Option[Double]])

object Person {
  implicit val personFormat: OFormat[Person] = Json.format[Person]
}

Json.toJson(Person(a))

Upvotes: 4

Views: 5162

Answers (2)

Dmytro Mitin
Dmytro Mitin

Reputation: 51703

You can define implicits:

  import scala.collection.Map

  object Person {
    implicit object MapReads extends Reads[Map[String, Option[Double]]] {
      def reads(jsValue: JsValue): JsResult[Map[String, Option[Double]]] = jsValue match {
        case JsObject(map) => JsSuccess(
          map.mapValues {
            case JsNumber(d) => Some(d.toDouble)
            case _ => None
          }
        )
        case _ => JsError()
      }
    }

    implicit object MapWrites extends Writes[Map[String, Option[Double]]] {
      def writes(map: Map[String, Option[Double]]): JsValue =
        JsObject(map.mapValues(optd => JsNumber(optd.getOrElse[Double](0.0))))
    }

    implicit val personFormat: OFormat[Person] = Json.format[Person]
  }

  println(Json.toJson(Person(a)))//{"x":{"a":0}}

Based on answer.

Upvotes: 3

Marcin Bablok
Marcin Bablok

Reputation: 46

The problem seems to be the inability of play-json macros to work with nested type variables:

Map[String, Option[Double]]

You could use an intermediate type wrapping Option

import play.api.libs.json.{Json, OFormat}

case class OptionDouble(value: Option[Double])
case class Person(x: Map[String, OptionDouble])

implicit val optionDoubleFormat = Json.format[OptionDouble]
implicit val personFormat = Json.format[Person]

val a: Map[String, OptionDouble] = Map("a" -> OptionDouble(None))
Json.toJson(Person(a))

Another option could be to write the formatter by hand.

Upvotes: 1

Related Questions