0
votes

i want ro make a response

val res: List[Categories] = CategoryObj.getCategories
complete(OK, res)

i wrote an implicit json formats

implicit val jsCat = jsonFormat3(Category)
implicit val jsCats = jsonFormat1(Seq[Category])

but for a second line i got three errors

Error:(8, 25) inferred type arguments [Seq[DAO.Category],Seq[DAO.Category]] do not conform to method jsonFormat1's type parameter bounds [P1,T <: Product] implicit val jsCats = jsonFormat1(Seq[Category])

Error:(8, 40) type mismatch; found : Seq[DAO.Category] => Seq[DAO.Category] required: P1 => T Note: implicit value jsCats is not applicable here because it comes after the application point and it lacks an explicit result type implicit val jsCats = jsonFormat1(Seq[Category])

Error:(8, 36) could not find implicit value for evidence parameter of type spray.json.DefaultJsonProtocol.JF[P1] (Cannot find JsonWriter or JsonFormat type class for P1) implicit val jsCats = jsonFormat1(Seq[Category])

And i can't get how to deal with them

2
Is your Category class a case class?Emiliano Martinez
@EmiCareOfCell44, case class, as i mentioned in titleDmitry Reutov
if you comment the Seq[Category] it should work. Does it give errors of missing serializer if you comment that line?Emiliano Martinez
@EmiCareOfCell44, yes, it threw an error but i already got a right code and gave an answerDmitry Reutov

2 Answers

2
votes

Seq[Category] is obviously not a subtype of Product

inferred type arguments [Seq[DAO.Category],Seq[DAO.Category]] do not conform to method jsonFormat1's type parameter bounds [P1,T <: Product]

while case classes are subtypes of Product.

At https://developer.lightbend.com/guides/akka-http-quickstart-scala/json.html it's written

implicit val usersJsonFormat = jsonFormat1(Users) 

final case class Users(users: immutable.Seq[User])

i.e. jsonFormat1 is applied to a case class wrapping a Seq, not to Seq itself.

1
votes

The problem was i had just simple object for application.

object myApp {
  // my code
}

These changes helped

trait appJSONProtocol extends DefaultJsonProtocol {
  implicit val jsCat = jsonFormat3(Category)
}

object myApp extends App with appJSONProtocol with SprayJsonSupport {
  // my code ...
}