spray-json

Cannot find JsonWriter or JsonFormat type class for a case class

為{幸葍}努か 提交于 2019-12-22 05:51:35
问题 Following the tutorial from http://www.smartjava.org/content/first-steps-rest-spray-and-scala, there are some unexpected error messages. What is going on? Have I defined implicit JsonWriter by the implicit val personFormat = jsonFormat3(Person) call? scala> import spray.json.DefaultJsonProtocol import spray.json.DefaultJsonProtocol scala> object MyJsonProtocol extends DefaultJsonProtocol { implicit val personFormat = jsonFormat3(Person) } | | defined object MyJsonProtocol scala> case class

How to represent optional fields in spray-json?

杀马特。学长 韩版系。学妹 提交于 2019-12-20 17:37:38
问题 I have an optional field on my requests: case class SearchRequest(url: String, nextAt: Option[Date]) My protocol is: object SearchRequestJsonProtocol extends DefaultJsonProtocol { implicit val searchRequestFormat = jsonFormat(SearchRequest, "url", "nextAt") } How do I mark the nextAt field optional, such that the following JSON objects will be correctly read and accepted: {"url":"..."} {"url":"...", "nextAt":null} {"url":"...", "nextAt":"2012-05-30T15:23Z"} I actually don't really care about

could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller

谁说胖子不能爱 提交于 2019-12-19 09:50:07
问题 I'm using val akkaV = "2.2.3" val sprayV = "1.2.0" Seq( "io.spray" % "spray-can" % sprayV, "io.spray" % "spray-routing" % sprayV, "io.spray" %% "spray-json" % "1.2.5", "io.spray" % "spray-testkit" % sprayV, "com.typesafe.akka" %% "akka-actor" % akkaV, "com.typesafe.akka" %% "akka-testkit" % akkaV, And getting this error: could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller[List[org.bwi.models.Cluster]] with this code: object JsonImplicits

Spray-json deserializing nested object

ε祈祈猫儿з 提交于 2019-12-18 11:33:13
问题 How to deserialize nested objects correctly in spray-json? import spray.json._ case class Person(name: String) case class Color(n: String, r: Int, g: Int, b: Int, p: Person) object MyJsonProtocol extends DefaultJsonProtocol { implicit object ColorJsonFormat extends RootJsonFormat[Color] { def write(c: Color) = JsObject( "color-name" -> JsString(c.n), "Green" -> JsNumber(c.g), "Red" -> JsNumber(c.r), "Blue" -> JsNumber(c.b), "person-field" -> JsObject("p-name" -> JsString(c.p.name)) ) def read

akka http (un)marshall traits

元气小坏坏 提交于 2019-12-13 17:55:13
问题 Let's assume the following Code: sealed trait Action { def run(): Boolean } case class SimpleAction(parameter: String) extends Actions { // some impl } case class ExtendedAction(parameter1: String, parameter2: String) extends Actions { // some impl } Now I want to define a webservice where one can retrieve the Actions. How can I Marshall the Action as it's just the trait and no specific Type? I have found this https://github.com/spray/spray-json#providing-jsonformats-for-other-types in the

How to convert a JsArray to sequence of case classes with spray-json?

旧城冷巷雨未停 提交于 2019-12-13 16:42:06
问题 I have a a variable var movieArray = movieText.parseJson which is of class println(movieArray.getClass) class spray.json.JsArray How do I convert it to a Sequence of case classes e.g case class Movie(id: Int, title: String) I tried 1. movieArray.convertTo[Seq[Movie]] 2. movieArray.map(_.convertTo[Movie]) 3. for (i <- movieArray) println(i) gives errors... 1. Cannot find JsonReader or JsonFormat type class for Seq[Movie] 2. value map is not a member of spray.json.JsValue 3. value foreach is

Generic Spray-Client

删除回忆录丶 提交于 2019-12-13 00:43:35
问题 I'm trying to create a generic HTTP client in Scala using spray. Here is the class definition: object HttpClient extends HttpClient class HttpClient { implicit val system = ActorSystem("api-spray-client") import system.dispatcher val log = Logging(system, getClass) def httpSaveGeneric[T1:Marshaller,T2:Unmarshaller](uri: String, model: T1, username: String, password: String): Future[T2] = { val pipeline: HttpRequest => Future[T2] = logRequest(log) ~> sendReceive ~> logResponse(log) ~>

akka-http : could not find implicit value for parameter unmarshalling

房东的猫 提交于 2019-12-12 15:25:57
问题 My spray json support looks like this object MarshallingSupport extends SprayJsonSupport { implicit def json4sFormats: Formats = DefaultFormats } And in my route I want to map the request to a dto object Main extends App with AppConfig with BaseService with MainActorSystem { val processor = system.actorOf(Props(), "processorActor") val view = system.actorOf(Props(), "processorActor") override protected implicit val executor: ExecutionContext = system.dispatcher override protected val log:

Howto test Custom Json Objects with Spray Routing

时光怂恿深爱的人放手 提交于 2019-12-12 14:22:59
问题 I'm creating a Rest API with spray-routing on top of mongodb for some CRUD operations, this all works fine, expect whenever I try to test it with specs2 the following specification class RestServiceSpec extends Specification with Specs2RouteTest with RoutingRestService // database initialization removed for clarity "The rest service" should "have a player called 'Theo TestPlayer' in the db" in { Get("/api/1.0/player/" + player1._id) ~> restRoute ~> check { entityAs[Player] must be equalTo

How do I write/read to/from disk Spray json object?

霸气de小男生 提交于 2019-12-12 04:49:17
问题 I want to be able to read/write Json object from/to disk. I admit, in Java it would have been taking me about 10 minutes. Scala is a bit more challenging. I think that the main reason is not enough info on the net. Anyway, this is what I have done so far: package com.example import java.io.{BufferedWriter, FileWriter} import spray.json._ import spray.json.DefaultJsonProtocol import java.nio.file.{Paths, Files} import java.nio.charset.StandardCharsets object Test { object Foo extends