Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
Renamed value type reads and writes creation methods to prevent from …
Browse files Browse the repository at this point in the history
…clashing with the play.api.Json
  • Loading branch information
Jakub Chrobasik committed Nov 25, 2016
1 parent 046ed87 commit 440b1b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ trait ValueTypeFormat {
implicit val bigDecimalToJson = (value: BigDecimal) => JsNumber.apply(value)
implicit val booleanToJson = JsBoolean.apply _

def reads[T, V <: ValueType[T]](instantiateFromSimpleType: T => V)
(implicit parse: PartialFunction[JsValue, T]) =
def valueTypeReadsFor[T, V <: ValueType[T]](instantiateFromSimpleType: T => V)
(implicit parse: PartialFunction[JsValue, T]) =
new Reads[V] {

def reads(json: JsValue): JsResult[V] = Try(parse(json)).flatMap(t => Try(instantiateFromSimpleType(t))) match {
Expand All @@ -62,13 +62,13 @@ trait ValueTypeFormat {
}
}

def writes[T, V <: ValueType[T]](implicit toJson: T => JsValue) = new Writes[V] {
def valueTypeWritesFor[T, V <: ValueType[T]](implicit toJson: T => JsValue) = new Writes[V] {

def writes(value: V): JsValue = toJson(value.value)

}

def writes[V <: StringValue](implicit classTag: ClassTag[V]) = new Writes[V] {
def valueTypeWritesFor[V <: StringValue](implicit classTag: ClassTag[V]) = new Writes[V] {

def writes(value: V): JsValue = JsString(value.value)

Expand All @@ -77,5 +77,5 @@ trait ValueTypeFormat {
def format[T, V <: ValueType[T]](instantiateFromSimpleType: T => V)
(implicit parse: PartialFunction[JsValue, T],
toJson: T => JsValue) =
Format[V](reads(instantiateFromSimpleType), writes[T, V])
Format[V](valueTypeReadsFor(instantiateFromSimpleType), valueTypeWritesFor[T, V])
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,29 @@ class RoundedBigDecimalValueTypeFormatSpec extends UnitSpec {

class ValueTypeFormatSpec extends UnitSpec {

"reads" should {
"valueTypeReadsFor" should {

"allow to deserialize given json into an object" in {

implicit val stringValueReads = reads(TestStringValue.apply)
implicit val stringValueReads = valueTypeReadsFor(TestStringValue.apply)

fromJson(JsString("value")).get shouldBe TestStringValue("value")
}

}

"writes" should {
"valueTypeWritesFor" should {

"allow to serialize given object into a json" in {

implicit val stringValueReads = writes[Int, TestIntValue]
implicit val stringValueReads = valueTypeWritesFor[Int, TestIntValue]

toJson(TestIntValue(1)) shouldBe JsNumber(1)
}

"allow to serialize given StringValue object into a json" in {

implicit val stringValueReads = writes[TestStringValue]
implicit val stringValueReads = valueTypeWritesFor[TestStringValue]

toJson(TestStringValue("value")) shouldBe JsString("value")
}
Expand Down

0 comments on commit 440b1b7

Please sign in to comment.