case class Foo(x: String, y: List[Bar])
case class Bar(z: Long)
then I have two formats like this:
object FooJsonProtocol extends DefaultJsonProtocol {
implicit val fooFormat = jsonFormat(Foo, "x", "y")
}
object BarJsonProtocol extends DefaultJsonProtocol {
implicit val barFormat = jsonFormat(Bar, "z")
}
I import them into scope like:
import FooJsonProtocol.fooFormat
import BarJsonProtocol.barFormat
But I get a compile error:
could not find implicit value for evidence parameter of type
FooJsonProtocol.JF[List[Bar]]
What am I missing? Thanks!
Gregg
as Saku already pointed out you need to bring the whole protocol into scope, not only the JsonFormat(s) for your class(es).
Also, you don't need to create separate protocol objects for each case class.
It's enough if you create one custom object and import all of its members:
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val fooFormat = jsonFormat(Foo, "x", "y")
implicit val barFormat = jsonFormat(Bar, "z")
}
import MyJsonProtocol._
Cheers,
Mathias
---
mat...@spray.cc
http://www.spray.cc
import MyJsonProtocol._
I am still getting the error I mentioned above, but I am also getting
an ambiguous implicit error - that's actually why I explicitly
imported my formatters in the first place.
ambiguous implicit values:
[error] both value StringMarshaller in trait DefaultMarshallers of
type => cc.spray.typeconversion.MarshallerBase[String]
[error] and method sprayJsonMarshaller in trait SprayJsonSupport of
type [A](implicit evidence$2:
cc.spray.json.JsonWriter[A])cc.spray.typeconversion.MarshallerBase[A]{lazy
val printer: cc.spray.json.JsonPrinter}
[error] match expected type cc.spray.http.ContentType =>
Option[cc.spray.http.ContentType] =>
cc.spray.typeconversion.Marshalling[String]
[error] ctx.fail(400, "user %s already
exists".format(u.get("_id")))
Is there a good way to deal with that one? Thanks for the help.
ok, two more things you need to change:
1. To fix the "could not find implicit value" error you need to switch the declaration order in your MyJsonProtocol:
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val barFormat = jsonFormat(Bar, "z")
implicit val fooFormat = jsonFormat(Foo, "x", "y")
}
2. To fix the "ambiguous implicit values" error you could switch to the latest snapshots of spray-server and spray-json or manually remove the ambiguity, e.g. by using an "import SprayJsonSupport._" (rather then mixing in the SprayJsonSupport trait) at a scope that doesn't include the `ctx.fail` line or by explicitly specifying the marshaller to be used:
ctx.fail(400, "user %s already exists".format(u.get("_id")))(StringMarshaller)
HTH and cheers,
Mathias
---
mat...@spray.cc
http://www.spray.cc
case class User (
.....
friendsLists : Set[String])
object UserJsonSupport extends DefaultJsonProtocol with SprayJsonSupport {
implicit val UserFormat = jsonFormat12(UserService.User)
}
Error:(39, 43) could not find implicit value for evidence parameter of type UserJsonSupport.JF[scala.collection.mutable.Set[String]]
implicit val UserFormat = jsonFormat12(UserService.User)
^
How to deal with such cases?
--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/bf343d7c-2da2-4cc3-8e23-947e53f04012%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.