Skip to content

Documents and fixes unmarshalling part. #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Currently library supports Scala version `2.11` and `2.10`.

In order to use current version _scala-jsonapi_ you have to add library dependency assuming that you have [sonatype resolvers](http://www.scala-sbt.org/0.13/docs/Resolvers.html#Maven) set up.

libraryDependencies += "org.zalando" %% "scala-jsonapi" % "0.3.3"
libraryDependencies += "org.zalando" %% "scala-jsonapi" % "0.3.4"

# Usage

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/zalando/jsonapi/json/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ package object json {
Marshaller.delegate[T, RootObject](`application/vnd.api+json`)(Jsonapi.asRootObject(_))

implicit def jsonapiJsonConvertableUnmarshaller[T: JsonapiRootObjectReader](implicit u: Unmarshaller[RootObject]): Unmarshaller[T] =
Unmarshaller.delegate[RootObject, T](`application/vnd.api+json`)(Jsonapi.asJsonapi(_))
Unmarshaller.delegate[RootObject, T](`application/vnd.api+json`)(Jsonapi.fromRootObject(_))
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ trait SprayJsonJsonapiSupport extends SprayJsonJsonapiFormat with DefaultJsonPro
}

implicit def sprayJsonJsonapiUnmarshaller =
Unmarshaller.delegate[String, RootObject](`application/vnd.api+json`) { json
json.parseJson.convertTo[RootObject]
Unmarshaller.delegate[String, RootObject](`application/vnd.api+json`) { string
string.parseJson.convertTo[RootObject]
}
}

Expand Down
29 changes: 23 additions & 6 deletions src/main/scala/org/zalando/jsonapi/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ package object jsonapi {
def toJsonapi(a: A): RootObject
}

/**
* Returns a provided object [[A]] from Jsonapi [[model.RootObject]] object.
* @tparam A the type that is an instance of this type class
*/
trait JsonapiRootObjectReader[A] {
def fromJsonapi[A](rootObject: RootObject): A
def fromJsonapi(rootObject: RootObject): A
}

trait JsonRootObjectFormat[A] extends JsonapiRootObjectWriter[A] with JsonapiRootObjectReader[A]
/**
* A combination of reader and writer in one since place.
* @tparam A the type that is an instance of this type class
*/
trait JsonapiRootObjectFormat[A] extends JsonapiRootObjectWriter[A] with JsonapiRootObjectReader[A]

/**
* Implicit class that provides operations on types that are instances of the [[JsonapiRootObjectWriter]] type class.
Expand All @@ -34,9 +42,15 @@ package object jsonapi {
}
}

implicit class FromJsonapiRootObjectReaderOps[A](rootObject: RootObject) {
def jsonapi(implicit reader: JsonapiRootObjectReader[A]): A = {
Jsonapi.asJsonapi(rootObject)
/**
* Implicit class that provides operations on types that are instances of the provided type class.
*/
implicit class FromJsonapiRootObjectReaderOps(rootObject: RootObject) {
/**
* Returns a provided type object from Jsonapi [[model.RootObject]].
*/
def jsonapi[A](implicit reader: JsonapiRootObjectReader[A]): A = {
Jsonapi.fromRootObject(rootObject)
}
}

Expand All @@ -50,7 +64,10 @@ package object jsonapi {
def asRootObject[A](a: A)(implicit writer: JsonapiRootObjectWriter[A]): RootObject =
writer toJsonapi a

def asJsonapi[A](rootObject: RootObject)(implicit reader: JsonapiRootObjectReader[A]): A =
/**
* Returns a provided value from the Jsonapi [[model.RootObject]]
*/
def fromRootObject[A](rootObject: RootObject)(implicit reader: JsonapiRootObjectReader[A]): A =
reader fromJsonapi rootObject
}
}
18 changes: 13 additions & 5 deletions src/test/scala/org/zalando/jsonapi/json/JsonapiSupportSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.zalando.jsonapi.json

import org.scalactic.TypeCheckedTripleEquals
import org.scalatest.{ EitherValues, WordSpec }
import org.zalando.jsonapi.JsonapiRootObjectWriter
import org.zalando.jsonapi.{ JsonapiRootObjectReader, JsonapiRootObjectWriter }
import org.zalando.jsonapi.model.{ JsonApiObject, JsonApiProperty, RootObject }
import spray.http.HttpEntity
import spray.httpx.marshalling._
Expand All @@ -15,8 +15,9 @@ trait JsonapiSupportSpec extends WordSpec with TypeCheckedTripleEquals with Eith

s"$jsonapiSupportClassName" must {
trait Context {
val rootObject = RootObject(jsonApi = Some(List(JsonApiProperty("foo", JsonApiObject.StringValue("bar")))))
case class Foo(bar: String)
val rootObject = RootObject(jsonApi = Some(List(JsonApiProperty("foo", JsonApiObject.StringValue("bar")))))
val foo = Foo("bar")
}

"allow marshalling a Jsonapi root object with the correct content type" in new Context {
Expand All @@ -28,15 +29,22 @@ trait JsonapiSupportSpec extends WordSpec with TypeCheckedTripleEquals with Eith
implicit val fooWriter = new JsonapiRootObjectWriter[Foo] {
override def toJsonapi(a: Foo) = rootObject
}
assert(marshal(Foo("bar")).right.value.toString ===
assert(marshal(foo).right.value.toString ===
"""HttpEntity(application/vnd.api+json; charset=UTF-8,{"jsonapi":{"foo":"bar"}})""")
}

// TODO Fix Play framework unmarshalling problem (issue #25 on github)
// "allow unmarshalling a Json to a root object" in {
// val rootObject = RootObject(jsonApi = Some(List(JsonApiProperty("foo", JsonApiObject.StringValue("bar")))))
// "allow unmarshalling a Json to a root object with the correct content type" in new Context {
// val httpEntity = HttpEntity(`application/vnd.api+json`, """{"jsonapi":{"foo":"bar"}}""")
// assert(httpEntity.as[RootObject] === Right(rootObject))
// }
//
// "allow unmarshalling Jsonapi root object to a value type" in new Context {
// implicit val fooReader = new JsonapiRootObjectReader[Foo] {
// override def fromJsonapi(ro: RootObject) = foo
// }
// val httpEntity = HttpEntity(`application/vnd.api+json`, """{"jsonapi":{"foo":"bar"}}""")
// assert(httpEntity.as[Foo] === Right(foo))
// }
}
}
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.3.3"
version in ThisBuild := "0.3.4"