2

I am trying to use play.api.lib.json to convert a json to my object. But then this happend...

  case class Foo(foo:Option[Map[String,String]])
  case class Bar(bar:String,foo:Foo)

  def barJsonToModel(foobarJson:JsValue):Bar = {
     implicit val fooReads: Reads[Foo] = (
        ( JsPath \ "foo" ).readNullable[Map[String,String]]
     )(Foo.apply _)
  }

Expression of type Reads[Option[Map[String,String]]] doesn't comfort to expect type Reads[Foo]

cchantep
  • 8,797
  • 3
  • 25
  • 39
  • 1
    I think that your problem is similar to that in [this question](https://stackoverflow.com/questions/40786742/play-framework-json-reads-for-a-single-attribute-case-class). Also, since your class `Foo` is only a wrapper around a single value, you can remove it and include your `foo: Option[Map[String, String]])` directly in your class `Bar`. – Jack Bourne Jun 21 '19 at 07:46
  • 1
    Possible duplicate of [Play framework: JSON Reads for a single-attribute case class](https://stackoverflow.com/questions/40786742/play-framework-json-reads-for-a-single-attribute-case-class) – Jack Bourne Jun 21 '19 at 07:47

1 Answers1

0

You are using the functional syntax of play-json, but so an import is missing before:

import play.api.libs.functional.syntax._
Tomer Shetah
  • 7,646
  • 6
  • 20
  • 32
cchantep
  • 8,797
  • 3
  • 25
  • 39