scala - Convert Future result into json -


i have 2 model communication:

case class post(id: int, name: string, text: string) case class tag(id: int, name: string) 

and create json format models:

import play.api.libs.json._ object myformats {   implicit val postformat = json.format[post]   implicit val tagformat = json.format[tag] }         

then create service (actor) can return okresponse or badresponse

sealed trait response case class okresponse[t](model: t) extends response case class badresponse(msg: string) extends response   // easy example   case class message(id: int)  class myactorservice extends actor {       def receive = {         case message(id) =>            if (id == 0) {              sender ! okresponse(post(1, "foo", "bar"))           }  else if (id == 1) {              sender ! okresponse(tag(1, "tag"))           } else {              sender ! badresponse("id overflow")           }      } } 

then want convert model okresponse json value:

(myactorservice ? message(1)).mapto[response].map {   case badresponse(msg) => println(msg)    case okresponse(model) =>      println(json.tojson(model))  }        

but not compiled because no json serializer found type any. try implement implicit writes or format type.

how talk scala type of model? best way save type work future in scala?

the type of model value in okresponse not known, bottom type any.

you pattern match on model of okresponse.

(myactorservice ? message(1)).mapto[response].map {   case badresponse(msg) => println(msg)    case okresponse(post: post) =>      println(json.tojson(post))    case okresponse(tag: tag) =>      println(json.tojson(tag))  } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -