Skip to content

Commit 59c2fe3

Browse files
committed
adjusted version, enhanced testcase using companion objects
1 parent cb8f765 commit 59c2fe3

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Add the following to `build.sbt`:
3333

3434
resolvers += "Tegonal releases" at "https://github.com/tegonal/tegonal-mvn/raw/master/releases/"
3535

36-
libraryDependencies += "com.tegonal" %% "play-json-typedid" % "1.0.0"
36+
libraryDependencies += "com.tegonal" %% "play-json-typedid" % "1.0.1"
3737

3838
## Usage
3939

app/com/tegonal/play/json/TypedIdFormat.scala

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* __ __ *\
1+
/* ____ *\
22
* / /____ ___ ____ ___ ___ _/ / generic-id *
33
* / __/ -_) _ `/ _ \/ _ \/ _ `/ / contributed by tegonal *
44
* \__/\__/\_, /\___/_//_/\_,_/_/ http://tegonal.com/ *
@@ -31,8 +31,8 @@ object TypedId {
3131
implicit def fromJson(json: Json.type) = TypedId
3232

3333
//extended format function
34-
def idformat[I <: StringBaseId](fact: Factory[String, I]) = new StringTypedIdFormat[I](fact)
35-
def idformat[I <: NumberBaseId](fact: Factory[BigDecimal, I]) = new NumberTypedIdFormat[I](fact)
34+
def idformat[I <: StringBaseId](implicit fact: Factory[String, I]) = new StringTypedIdFormat[I]
35+
def idformat[I <: NumberBaseId](implicit fact: Factory[BigDecimal, I]) = new NumberTypedIdFormat[I]
3636

3737
trait BaseId[V] {
3838
val value: V
@@ -46,24 +46,21 @@ object TypedId {
4646
case class StringId[T](value: String) extends BaseId[String]
4747
case class NumberId[T](value: BigDecimal) extends BaseId[BigDecimal]
4848

49-
implicit class StringTypedIdFormat[I <: BaseId[String]](fact: Factory[String, I]) extends Format[I] {
49+
class StringTypedIdFormat[I <: BaseId[String]](implicit fact: Factory[String, I]) extends Format[I] {
5050
def reads(json: JsValue): JsResult[I] = json match {
51+
5152
case JsString(value) => JsSuccess(fact(value))
5253
case _ => JsError(s"Unexpected JSON value $json")
5354
}
5455

5556
def writes(id: I): JsValue = JsString(id.value)
5657
}
57-
implicit class NumberTypedIdFormat[I <: BaseId[BigDecimal]](fact: Factory[BigDecimal, I]) extends Format[I] {
58+
class NumberTypedIdFormat[I <: BaseId[BigDecimal]](implicit fact: Factory[BigDecimal, I]) extends Format[I] {
5859
def reads(json: JsValue): JsResult[I] = json match {
5960
case JsNumber(value) => JsSuccess(fact(value))
6061
case _ => JsError(s"Unexpected JSON value $json")
6162
}
6263

6364
def writes(id: I): JsValue = JsNumber(id.value)
6465
}
65-
66-
//provide formats for id classes
67-
implicit def stringIdFormat[T]: Format[StringId[T]] = new StringTypedIdFormat[StringId[T]](StringId.apply _)
68-
implicit def numberIdFormat[T]: Format[NumberId[T]] = new NumberTypedIdFormat[NumberId[T]](NumberId.apply _)
6966
}

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name := "play-json-typedid"
22

33
organization := "com.tegonal"
44

5-
version := "1.0.0"
5+
version := "1.0.1"
66

77
scalaVersion := "2.11.1"
88

test/com/tegonal/play/json/TypedIdFormatSpec.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,28 @@ object TypedIdFormatSpec extends PlaySpecification {
3535
case class TestId2(value: BigDecimal) extends NumberBaseId
3636

3737
//provide formats for id classes
38-
implicit val testId1Format: Format[TestId1] = Json.idformat[TestId1](TestId1.apply _)
39-
implicit val testId2Format: Format[TestId2] = Json.idformat[TestId2](TestId2.apply _)
38+
object TestId1 {
39+
implicit val testId1Format: Format[TestId1] = Json.idformat[TestId1](TestId1.apply _)
40+
}
41+
object TestId2 {
42+
implicit val testId2Format: Format[TestId2] = Json.idformat[TestId2](TestId2.apply _)
43+
}
4044

4145
case class Test1(id: TestId1)
4246
case class Test2(id: TestId2)
4347
case class Test3(id: TestId1, foreigId: TestId2)
4448

45-
case class Test4(id: StringId[Test4])
46-
case class Test5(id: NumberId[Test5])
47-
case class Test6(id: StringId[Test6], foreigKey: NumberId[Test5])
49+
//provide formats for container classes in companion objects
50+
object Test1 {
51+
implicit val test1Format: Format[Test1] = Json.format[Test1]
52+
}
4853

49-
//provide formats for container classes
50-
implicit val test1Format: Format[Test1] = Json.format[Test1]
51-
implicit val test2Format: Format[Test2] = Json.format[Test2]
52-
implicit val test3Format: Format[Test3] = Json.format[Test3]
54+
object Test2 {
55+
implicit val test2Format: Format[Test2] = Json.format[Test2]
56+
}
57+
object Test3 {
58+
implicit val test3Format: Format[Test3] = Json.format[Test3]
59+
}
5360

5461
//test data
5562
val test1: JsValue = Json.parse("""

0 commit comments

Comments
 (0)