json - Akka-Spray object marshall -


i experimenting aka , spray, want achieve simple object marshalling service.

when try compile code following error :

error:(33, 18) not find implicit value parameter marshaller:   spray.httpx.marshalling.marshaller[exampleapplication.password]   marshal(password(randomstring(8),i,0))                    ^ 

here code:

import akka.actor.actorsystem import spray.http.httpentity import spray.json.defaultjsonprotocol import spray.routing.simpleroutingapp import spray.httpx.marshalling._ import spray.json._  object exampleapplication extends app simpleroutingapp {   implicit val actorsystem = actorsystem()   implicit var = 0;     object myjsonprotocol extends defaultjsonprotocol {     implicit val passwordformat = jsonformat3(password)   }    case class password(pass: string, count: int, complexity: int)    def newpass(cplx: int):password = {return password(randomstring(cplx),i,0)}    startserver(interface = "localhost", port = 8080) {     {       path("passgen") {         i+=1         complete {           marshal(newpass(8))         }       }     }   }    def randomstring(n: int): string = {     n match {       case 1 => util.random.nextprintablechar().tostring       case _ => util.random.nextprintablechar.tostring ++ randomstring(n - 1).tostring     }   } } 

i'm still failing understand what's going wrong.

two changes fix it:

import spray.httpx.sprayjsonsupport._ 

then though define jsonprotocol object right in app must still import it's members explicitly:

object myjsonprotocol extends defaultjsonprotocol {   implicit val passwordformat = jsonformat3(password) } import myjsonprotocol._ 

that looks little repetitive in case, in use cases you'll have defined somewhere else.

optional

you can complete call without explicitly calling marshal:

complete {   newpass(8) } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -