Anyone know spray-json well? Having a problem.

425 views
Skip to first unread message

kraythe

unread,
Feb 12, 2013, 12:31:35 AM2/12/13
to scala...@googlegroups.com
I tried to seek help on the Spray-ser list but it doesnt get much traffic so I was hoping perhaps someone here could help.

I am trying to create my first simple spray actor that all it does is send back the headers the user sent as JSON.
The project can be found here: https://bitbucket.org/kraythensoft/ecplat
And the problematic source file here: https://bitbucket.org/kraythensoft/ecplat/src/ff09200fa791/src/main/scala/com/kraythensoft/ecplat/Actors.scala?at=master

The actor is quite simple and pasted below:

class HTTPRoutingActor extends Actor with SprayActorLogging {
  val jsonContentType = new ContentType(MediaTypes.`application/json`, Option(HttpCharsets.`UTF-8`))

  def receive = {
    case HttpRequest(HttpMethods.GET, "/ecplat", headers, _, _) =>
      val headerValues = Map[String, String](headers map { h => (h.name, h.value) }: _*)
      val hdrJSON = headerValues.toJson.prettyPrint.stripMargin
      //val hdrJSON = headers.toJson.prettyPrint.stripMargin
      val response = HttpResponse(
        entity = HttpBody(jsonContentType, hdrJSON)
      )
      sender ! response
    case _: HttpRequest => sender ! HttpResponse(status = StatusCodes.NotFound, entity = "Unknown resource!")
  }
}

I have tried to serialize the map of headers itself as well as loading it into a map[string, string] to no avail. Compiling this code yields:

- gradle clean build run
Starting Ecplat on Akka Microkernel, Exit with CTRL-C.
:clean
:compileJava UP-TO-DATE
:compileScala
..../KraythenSoft/ecplat/src/main/scala/com/kraythensoft/ecplat/Actors.scala:44: value toJson is not a member of scala.collection.immutable.Map[String,String]
      val hdrJSON = headerValues.toJson.prettyPrint.stripMargin
                                 ^
one error found
:compileScala FAILED


As you can see there is something wrong with the toJson method even though it should be found on the map it isnt. I have googled this til google bled and to no avail. Any help would be appreciated.

-- Robert

Naftoli Gugenheim

unread,
Feb 12, 2013, 12:33:37 AM2/12/13
to kraythe, scala-user
You must be missing an import. toJson must be "implicited" on to Map via some import.




--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Naftoli Gugenheim

unread,
Feb 12, 2013, 12:36:44 AM2/12/13
to kraythe, scala-user
The first result for "spray tojson" is https://github.com/spray/spray-json which says to import spray.json._ (see https://github.com/spray/spray-json#usage)

kraythe

unread,
Feb 12, 2013, 12:52:35 AM2/12/13
to scala...@googlegroups.com, kraythe
Not Missing an import, its there. Just click on the file you will see it.

-- Robert

kraythe

unread,
Feb 12, 2013, 12:53:51 AM2/12/13
to scala...@googlegroups.com, kraythe

and line 12 of the file, if you had clicked on it, says:

import spray.json._

It still doesnt work.

-- Robert

Naftoli Gugenheim

unread,
Feb 12, 2013, 2:29:31 AM2/12/13
to kraythe, scala-user
Okay sorry about that. I dug a bit more. Apparently you also have to import DefaultJsonProtocol._ .

kraythe

unread,
Feb 12, 2013, 6:02:31 AM2/12/13
to scala...@googlegroups.com, kraythe
That one is imported already as well. On line 7.

-- RObert

Johannes Rudolph

unread,
Feb 12, 2013, 6:16:49 AM2/12/13
to kraythe, scala-user
The problem is an unfortunate conflict between `import spray.json._` and `import spray.util._`. See

Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

kraythe

unread,
Feb 12, 2013, 4:17:05 PM2/12/13
to scala...@googlegroups.com, kraythe, johannes...@googlemail.com
Great Catch. Pull request merged.
Reply all
Reply to author
Forward
0 new messages