-
Notifications
You must be signed in to change notification settings - Fork 329
/
LottoExample.scala
77 lines (61 loc) · 2.89 KB
/
LottoExample.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright 2009-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.json4s
import org.specs2.mutable.Specification
import org.json4s.native.Document
object NativeLottoExample extends LottoExample[Document]("Native") with native.JsonMethods {
import LottoExample._
implicit val formats = DefaultFormats
def extractWinner(jv: JValue): Winner = jv.extract[Winner]
def extractLotto(jv: JValue): Lotto = jv.extract[Lotto]
}
object JacksonLottoExample extends LottoExample[JValue]("Jackson") with jackson.JsonMethods {
import LottoExample._
implicit val formats = DefaultFormats
def extractWinner(jv: JValue): Winner = jv.extract[Winner]
def extractLotto(jv: _root_.org.json4s.JValue): Lotto = jv.extract[Lotto]
}
abstract class LottoExample[T](mod: String) extends Specification with JsonMethods[T] {
import LottoExample._
("The " + mod + " Lotto Examples") should {
"pass" in {
compact(render(json)) must_== """{"lotto":{"id":5,"winning-numbers":[2,45,34,23,7,5,3],"winners":[{"winner-id":23,"numbers":[2,45,34,23,3,5]},{"winner-id":54,"numbers":[52,3,12,11,18,22]}]}}"""
val exp: Winner = Winner(23, List(2, 45, 34, 23, 3, 5))
val winn: Winner = extractWinner((json \ "lotto" \ "winners")(0))
winn.`winner-id` must_== exp.`winner-id`
extractLotto(json \ "lotto") must_== lotto
json.values must_== Map("lotto" -> Map("id" -> 5, "winning-numbers" -> List(2, 45, 34, 23, 7, 5, 3), "draw-date" -> None, "winners" -> List(Map("winner-id" -> 23, "numbers" -> List(2, 45, 34, 23, 3, 5)), Map("winner-id" -> 54, "numbers" -> List(52, 3, 12, 11, 18, 22)))))
}
}
def extractWinner(jv: JValue): Winner
def extractLotto(jv: JValue): Lotto
}
object LottoExample {
import JsonDSL._
case class Winner(`winner-id`: Long, numbers: List[Int])
case class Lotto(id: Long, `winning-numbers`: List[Int], winners: List[Winner], `draw-date`: Option[java.util.Date])
val winners = List(Winner(23, List(2, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22)))
val lotto = Lotto(5, List(2, 45, 34, 23, 7, 5, 3), winners, None)
val json: JObject =
("lotto" ->
("id" -> lotto.id) ~
("winning-numbers" -> lotto.`winning-numbers`) ~
("draw-date" -> lotto.`draw-date`.map(_.toString)) ~
("winners" ->
lotto.winners.map { w =>
(("winner-id" -> w.`winner-id`) ~
("numbers" -> w.numbers))}))
}