Spray marshalling adding backslashes

452 views
Skip to first unread message

Bharath Kumar Reddy

unread,
Sep 14, 2016, 10:10:57 PM9/14/16
to spray.io User List
Hello
Am using actor-per-request model to process some requests. Until before spray completes the request, messages are fine. After spray serves the request, backslashes are added into the resulting output.

implicit val errorFormat = jsonFormat1(Error)

implicit object StringFormat extends RootJsonFormat[String] {
  override def read(json: JsValue): String = json.compactPrint

  override def write(obj: String): JsValue = JsString(obj)
}

def receive = {
  case Response(payload) => {
    println("payload: " + new String(payload, Charset.forName("UTF-8")))
    complete(OK, new String(payload, Charset.forName("UTF-8")))
  }
  case ReceiveTimeout   => complete(GatewayTimeout, Error("Request timeout"))
}

`println` shows correct value, but after completion backslashes are present in the output:

"{\"metadata\":{\"sourceId\":\"FTP\",\"client\":\"test\",\"timeStamp\":\"2016-03-25T14:47:18.563\"

Need help!

Thanks in advance.

Bharath Kumar Reddy

unread,
Sep 14, 2016, 10:15:48 PM9/14/16
to spray.io User List
Missed complete block above:

def complete[T <: AnyRef : RootJsonFormat](status: StatusCode, obj: T) = {
  r.complete((status, obj))
  stop(self)
}

Age Mooij

unread,
Sep 18, 2016, 8:47:39 AM9/18/16
to spray...@googlegroups.com
Hi

Your special String format is circular. It turns a raw input into a rendered String of that input (i.e. the String representation of the JSON, which requires escaped quotes)  and then, when writing back to JSON, that rendered String is just wrapped in a JString again.

I'm also very confused by your use raw byte arrays in creating Strings. What is this Response class for?

Age

--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at https://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/8644eb0e-3b88-4397-8e57-e6c40a4f6361%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bharath Kumar Reddy

unread,
Sep 21, 2016, 1:11:29 AM9/21/16
to spray.io User List
Hello
I ran into the issue described here. 

I removed `SprayJsonSupport` and it was giving the right result.

Thanks
Reply all
Reply to author
Forward
0 new messages