Last active
October 26, 2015 22:09
-
-
Save jsrikrishna/c3632723742eaefd8c31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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