Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jsrikrishna/c3632723742eaefd8c31 to your computer and use it in GitHub Desktop.
Save jsrikrishna/c3632723742eaefd8c31 to your computer and use it in GitHub Desktop.
package rest
/**
* @author sjalipar
*/
import spray.json._
import scala.collection.immutable.ListMap
case class Response(time: String, a: Int, b: Int, c: Int, d: Int)
object restJsonProtocol extends DefaultJsonProtocol {
implicit object customResponseJsonProtocol extends RootJsonFormat[Response] {
def write(resp: Response): JsValue = JsObject(ListMap("time" -> JsString(resp.time),
"a" -> JsNumber(resp.a),
"b" -> JsNumber(resp.b),
"c" -> JsNumber(resp.c),
"d" -> JsNumber(resp.d)))
def read(json: JsValue): Response = {
json match {
case obj: JsObject => obj.convertTo[Response]
case _ => deserializationError("error occured in deserialization")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment