Spray-json cannot marshal map [String, String]

I have the following route setup, but when my map returns to the first full block, I get an error message:

could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[scala.collection.immutable.Map[String,String]]

 import spray.routing.HttpService import akka.actor.Actor import spray.http.HttpRequest import spray.routing.RequestContext import spray.json.DefaultJsonProtocol._ class UserServiceActor extends Actor with RestUserService { def actorRefFactory = context def receive = runRoute(linkRoute) } trait RestUserService extends HttpService { val userService = new LinkUserService def linkRoute = pathPrefix("user" / Segment) { userId => path("link") { parameters('service ! "YT") { complete { Map("status"-> "OK", "auth_url" -> "http://mydomain.com/auth") } } } } } 

According to this test , I would have to convert the map to json when DefaultJsonProtocol._ is imported, but even this failed:

 val map:Map[String, String] = Map("hi"->"bye") map.toJson 

Cannot find JsonWriter or JsonFormat type class for scala.collection.mutable.Map[String,String]

Not sure what is wrong :(

+7
scala spray-json
source share
1 answer

Someone from the list of loose mail indicated that the created Map was volatile, spray json would not marshal it. I changed it to scala.collection.immutable.Map and also added the following imports:

 import spray.httpx.SprayJsonSupport._ import spray.json.DefaultJsonProtocol._ 

And now everything works fine.

+19
source share

All Articles