Skip to content

Instantly share code, notes, and snippets.

@MishaelRosenthal
Created January 21, 2014 13:17
Show Gist options
  • Save MishaelRosenthal/8539841 to your computer and use it in GitHub Desktop.
Save MishaelRosenthal/8539841 to your computer and use it in GitHub Desktop.
Usage example for Scala Json writing and reading using Spray. The example includes: Polymorphism, nested case classes, case objects, external libraries classes (FiniteDuration), lists.
package com.liveperson.predictivedialer.common.sessions
import scala.concurrent.duration.FiniteDuration
/**
* User: michaelna
* Date: 1/15/14
* Time: 2:58 PM
*/
object InviteResponse {
sealed trait InviteResponse
case object Ignore extends InviteResponse
case class Accept(timeToAccept: FiniteDuration) extends InviteResponse
case class Decline(timeToDecline: FiniteDuration) extends InviteResponse
}
package com.liveperson.predictivedialer.common.sessions
import scala.concurrent.duration._
import com.liveperson.predictivedialer.common.sessions.InviteResponse.InviteResponse
/**
* User: michaelna
* Date: 1/6/14
* Time: 3:25 PM
*/
case class SessionBehaviour(sessionId: SessionBehaviourId,
sessionStart: FiniteDuration,
sessionDuration: FiniteDuration,
timeOnPage: FiniteDuration,
inviteResponse: InviteResponse,
patience: FiniteDuration,
chatDuration: FiniteDuration)
case class SessionBehaviourId(configId: Int, index: Int)
package com.liveperson.predictivedialer.common.sessions
import com.liveperson.predictivedialer.common.sessions.InviteResponse._
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{Matchers, FlatSpec}
import scala.concurrent.duration._
import spray.json._
import DefaultJsonProtocol._
/**
* Created with IntelliJ IDEA.
* User: mishaelr
* Date: 1/20/14
* Time: 1:32 PM
*/
@RunWith(classOf[JUnitRunner])
class SessionSprayJsonTest extends FlatSpec with Matchers{
implicit val sessionBehaviourIdFormat = jsonFormat2(SessionBehaviourId.apply)
implicit object IgnoreJsonFormat extends RootJsonFormat[Ignore.type] {
def write(a: Ignore.type) = JsString("Ignore")
def read(value: JsValue) = Ignore
}
implicit object FiniteDurationJsonFormat extends RootJsonFormat[FiniteDuration]{
def write(dur: FiniteDuration) = JsObject(
"length" -> JsNumber(dur.length),
"unit" -> JsString(dur.unit.toString)
)
def read(value: JsValue) = {
value.asJsObject.getFields("length", "unit") match {
case Seq(JsNumber(length), JsString(unit)) => FiniteDuration(length.toLong, unit.toLowerCase)
case _ => deserializationError("FiniteDuration expected")
}
}
}
implicit object InviteResponseJsonFormat extends RootJsonFormat[InviteResponse] {
implicit val acceptFormat = jsonFormat1(Accept.apply)
implicit val declineFormat = jsonFormat1(Decline.apply)
def write(response: InviteResponse) = response match {
case Ignore => JsObject("type" -> JsString("Ignore"))
case acc: Accept => JsObject(
"type" -> JsString("Accept"),
"value" -> acc.toJson
)
case dec: Decline => JsObject(
"type" -> JsString("Decline"),
"value" -> dec.toJson
)
}
def read(value: JsValue) = value.asJsObject.fields("type") match {
case JsString("Ignore") => Ignore
case JsString("Accept") => value.asJsObject.fields("value").asJsObject.convertTo[Accept]
case JsString("Decline") => value.asJsObject.fields("value").asJsObject.convertTo[Decline]
case _ => deserializationError("InviteResponse expected")
}
}
implicit val sessionBehaviourFormat = jsonFormat7(SessionBehaviour.apply)
/**
* Tests
*/
"SessionBehaviourId" should "written and read to Json" in {
val idBefore = SessionBehaviourId(1,1)
val idJson = idBefore.toJson
val idAfter = idJson.convertTo[SessionBehaviourId]
println("idBefore " + idBefore)
println("idJson " + idJson.prettyPrint)
println("idAfter " + idAfter)
idBefore should be (idAfter)
}
"Duration" should "written and read to Json" in {
val durationBefore = 5.seconds
val durationJson = durationBefore.toJson
val durationAfter = durationJson.convertTo[FiniteDuration]
println("durationBefore " + durationBefore)
println("durationJson " + durationJson.prettyPrint)
println("durationAfter " + durationAfter)
durationBefore should be (durationAfter)
}
"Ignore" should "written and read to Json" in {
val ignoreBefore = Ignore
val ignoreJson = ignoreBefore.toJson
val ignoreAfter = ignoreJson.convertTo[Ignore.type]
println("ignoreBefore " + ignoreBefore)
println("ignoreJson " + ignoreJson.prettyPrint)
println("ignoreAfter " + ignoreAfter)
ignoreBefore should be (ignoreAfter)
}
"Accept" should "written and read to Json" in {
val acceptBefore: InviteResponse = Accept(7.seconds)
val acceptJson = acceptBefore.toJson
val acceptAfter = acceptJson.convertTo[InviteResponse]
println("acceptBefore " + acceptBefore)
println("acceptJson " + acceptJson.prettyPrint)
println("acceptAfter " + acceptAfter)
acceptBefore should be (acceptAfter)
}
"Response" should "written and read to Json" in {
val responseBefore: InviteResponse = Accept(7 seconds)
val responseJson = responseBefore.toJson
val responseAfter = responseJson.convertTo[InviteResponse]
println("responseBefore " + responseBefore)
println("responseJson " + responseJson.prettyPrint)
println("responseAfter " + responseAfter)
responseBefore should be (responseAfter)
}
"Responses List" should "written and read to Json" in {
val responsesBefore = List[InviteResponse](Ignore, Accept(7 seconds), Decline(1 minute))
val responsesJson = responsesBefore.toJson
val responsesAfter = responsesJson.convertTo[List[InviteResponse]]
println("responsesBefore " + responsesBefore)
println("responsesJson " + responsesJson)
println("responsesAfter " + responsesAfter)
responsesBefore should be (responsesAfter)
}
"SessionBehaviour" should "written and read to Json" in {
val sessionBefore = SessionBehaviour(SessionBehaviourId(1, 1), 1 second, 1 second, 1 second, Accept(1 second), 1 second ,1 second)
val sessionJson = sessionBefore.toJson
val sessionAfter = sessionJson.convertTo[SessionBehaviour]
println("sessionBefore " + sessionBefore)
println("sessionJson " + sessionJson.prettyPrint)
println("sessionAfter " + sessionAfter)
sessionBefore should be (sessionBefore)
}
"SessionBehaviours list" should "written and read to Json" in {
val invitationResponses = List(Ignore, Accept(1 second), Decline(1 second))
val sessionsBefore = for{
i <- 1 until 10
response = invitationResponses(i % 3)
} yield SessionBehaviour(SessionBehaviourId(i, i), i seconds, i seconds, i seconds, response, i seconds, i seconds)
val sessionsJson = sessionsBefore.toJson
val sessionsAfter = sessionsJson.convertTo[List[SessionBehaviour]]
println("sessionsBefore " + sessionsBefore)
println("sessionsJson " + sessionsJson)
println("sessionsAfter " + sessionsAfter)
invitationResponses should be (invitationResponses)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment