Skip to content

Commit

Permalink
Tidy up Json Extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
IainHull committed Jun 19, 2014
1 parent 47fe58b commit 307dbd1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/main/scala/org/iainhull/resttest/JsonExtractors.scala
Expand Up @@ -34,29 +34,31 @@ trait JsonExtractors {
/**
* Extract the response body as an object.
*/
def jsonBodyAs[T: Reads](implicit tag : reflect.ClassTag[T] ): Extractor[T] = jsonBodyAs(JsPath)
def jsonBodyAs[T: Reads : reflect.ClassTag]: Extractor[T] = jsonBodyAs(JsPath)

/**
* Extract a portion of the response body as an object.
*
* @param path the path for the portion of the response to use
*/
def jsonBodyAs[T: Reads](path: JsPath = JsPath)(implicit tag : reflect.ClassTag[T] ) = {
JsonBody andThen (jsonToValue(_, path)) as (s"JsonBodyAs[${tag.runtimeClass.getName}]")
def jsonBodyAs[T: Reads : reflect.ClassTag](path: JsPath) = {
val tag = implicitly[reflect.ClassTag[T]]
JsonBody andThen (jsonToValue(_, path)) as (s"jsonBodyAs[${tag.runtimeClass.getSimpleName}]")
}

/**
* Extract the response body as a List of objects.
*/
def jsonBodyAsList[T: Reads](implicit tag : reflect.ClassTag[T] ): Extractor[Seq[T]] = jsonBodyAsList(JsPath)
def jsonBodyAsList[T: Reads](implicit tag : reflect.ClassTag[T]): Extractor[Seq[T]] = jsonBodyAsList(JsPath)

/**
* Extract a portion of the response body as a List of objects.
*
* @param path the path for the portion of the response to use
*/
def jsonBodyAsList[T: Reads](path: JsPath = JsPath)(implicit tag : reflect.ClassTag[T] ) = {
JsonBody andThen (jsonToList(_, path)) as (s"JsonBodyAsList[${tag.runtimeClass.getName}]")
def jsonBodyAsList[T: Reads : reflect.ClassTag](path: JsPath) = {
val tag = implicitly[reflect.ClassTag[T]]
JsonBody andThen (jsonToList(_, path)) as (s"jsonBodyAsList[${tag.runtimeClass.getSimpleName}]")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/iainhull/resttest/TestDriver.scala
Expand Up @@ -44,4 +44,4 @@ trait TestDriver {
* The default RequestBuilder, common to all tests
*/
implicit def defBuilder: RequestBuilder
}
}
9 changes: 9 additions & 0 deletions src/test/scala/org/iainhull/resttest/JsonExtractorsSpec.scala
Expand Up @@ -43,8 +43,17 @@ class JsonExtractorsSpec extends FlatSpec with Matchers {
evaluate(jsonBodyAsList[Int](__ \\ "age"), jsonList) should be(List(25, 20))
}

it should "include the type in its name" in {
jsonBodyAsList[Person].name should be ("jsonBodyAsList[Person]")
}


"jsonBodyAs" should "deserialise to scala types" in {
evaluate(jsonBodyAs[Person], Json parse personJson) should be(Jason)
evaluate(jsonBodyAs[String](__ \ "user" \ "name"), jsonDoc) should be("toto")
}

it should "include the type in its name" in {
jsonBodyAs[Person].name should be ("jsonBodyAs[Person]")
}
}

0 comments on commit 307dbd1

Please sign in to comment.