1
- /* __ __ *\
1
+ /* ____ *\
2
2
* / /____ ___ ____ ___ ___ _/ / generic-id *
3
3
* / __/ -_) _ `/ _ \/ _ \/ _ `/ / contributed by tegonal *
4
4
* \__/\__/\_, /\___/_//_/\_,_/_/ http://tegonal.com/ *
@@ -31,8 +31,8 @@ object TypedId {
31
31
implicit def fromJson (json : Json .type ) = TypedId
32
32
33
33
// 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 ]
36
36
37
37
trait BaseId [V ] {
38
38
val value : V
@@ -46,24 +46,21 @@ object TypedId {
46
46
case class StringId [T ](value : String ) extends BaseId [String ]
47
47
case class NumberId [T ](value : BigDecimal ) extends BaseId [BigDecimal ]
48
48
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 ] {
50
50
def reads (json : JsValue ): JsResult [I ] = json match {
51
+
51
52
case JsString (value) => JsSuccess (fact(value))
52
53
case _ => JsError (s " Unexpected JSON value $json" )
53
54
}
54
55
55
56
def writes (id : I ): JsValue = JsString (id.value)
56
57
}
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 ] {
58
59
def reads (json : JsValue ): JsResult [I ] = json match {
59
60
case JsNumber (value) => JsSuccess (fact(value))
60
61
case _ => JsError (s " Unexpected JSON value $json" )
61
62
}
62
63
63
64
def writes (id : I ): JsValue = JsNumber (id.value)
64
65
}
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 _)
69
66
}
0 commit comments