Skip to content

Instantly share code, notes, and snippets.

@tomer-ben-david
Last active April 10, 2018 12:14
Show Gist options
  • Save tomer-ben-david/566b252f6c1d5b1064c842c3ebf24f86 to your computer and use it in GitHub Desktop.
Save tomer-ben-david/566b252f6c1d5b1064c842c3ebf24f86 to your computer and use it in GitHub Desktop.
#scala

scala

mockito

import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._

with MockitoSugar

val mockClient = mock[Client]
when(mockClient.status).thenReturn(200)

play

json

implicit val writesMutableListBuffer: Writes[ListBuffer[(String, mutable.ListBuffer[T])]] = new Writes[ListBuffer[(String, mutable.ListBuffer[T])]] {
 
    def writes(q: ListBuffer[(String, mutable.ListBuffer[T])]): JsValue = {
      Json.obj("myobj" -> q.map(
        item => Json.obj(
          "listbuffer-key" -> Json.toJson(item._1),
          "listbuffer-values" -> Json.toJson(item._2)
        )
      )
      )
    }
  }
 
  val someReader = new Reads[MyObj] {
    override def reads(json: JsValue): JsResult[MyObj] = {
      val fieldValue = (json \ "someField").as[String]
    }
  }
 
  // for simple case classes just define the default writes/reads
 
  case class Customer(name: String)
  object Customer {
    implicit val customerJsonWriter = Json.writes[Customer]
    implicit val customerJsonReader = Json.reads[Customer]
  }
 
  // for inheritance define case object for the train with pattern matching
 
  object RequestData {
    implicit val requestDataWriter = new Writes[RequestData] {
      override def writes(o: RequestData): JsValue = {
        o match {
          case stringRequestData:StringRequestData => StringRequestData.stringRequestDataWriter.writes(stringRequestData)
          case _ => throw new IllegalArgumentException(s"requestDataWriter: No writer for $o")
        }
      }
    }
 
    implicit val requestDataReader = new Reads[RequestData] {
      override def reads(json: JsValue): JsResult[RequestData] = {
        val requestType = (json \ "requestType").as[String]
        requestType match {
          case "stringRequestData" => StringRequestData.stringRequestDataReader.reads(json)
          case _ => throw new RuntimeException(s"requestDataReader: does not support json: $json with type $requestType")
        }
      }
    }
  }
 
  // Map to json and json to map, array
  implicit val moreDetailsJsonWriter = new Writes[Map[String, SomeValue]] {
    override def writes(o: Map[String, SomeValue]): JsValue = {
      Json.arr(o.map( {
        case (key, value) => key -> SomeValue.writes(value)
      }))
    }
  }
 
  implicit val moreDetailsJsonReader = new Reads[Map[String, SomeValue]] {
    override def reads(json: JsValue): JsResult[Map[String, SomeValue]] = {
      val mapAsJson = json.as[JsArray]
      val kvSeq = mapAsJson.value.flatMap { entry =>
        val keyValSeq = entry.asInstanceOf[JsObject].fields
 
        keyValSeq.map(keyValEntry => keyValEntry._1.asInstanceOf[String] ->
          SomeValue.reads(keyValEntry._2).get)
 
      }
      JsSuccess(kvSeq.toMap)
    }
  }
 
  // Take first key of a json { "somekey": "somevalue" } will return "somekey"
  json.asInstanceOf[JsObject].fields(0)._1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment