File tree Expand file tree Collapse file tree 3 files changed +26
-0
lines changed
main/scala/com/baeldung/scala/zio/json
test/scala/com/baeldung/scala/zio/json Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,13 @@ import zio.json.*
5
5
sealed trait Command
6
6
case class Start (timeout : Long ) extends Command
7
7
case object Stop extends Command
8
+ case class Kill (reason : String , force : Boolean ) extends Command
8
9
9
10
@ jsonDiscriminator(" type" )
10
11
sealed trait Command2
11
12
case class Start2 (timeout : Long ) extends Command2
12
13
case object Stop2 extends Command2
14
+ case class Kill2 (reason : String , force : Boolean ) extends Command2
13
15
14
16
object Start {
15
17
implicit val encoder : JsonEncoder [Start ] = DeriveJsonEncoder .gen[Start ]
Original file line number Diff line number Diff line change @@ -25,6 +25,10 @@ class DecodingSpec extends AnyWordSpec with Matchers {
25
25
Start (100 )
26
26
)
27
27
""" {"Stop":{}}""" .fromJson[Command ] shouldBe Right (Stop )
28
+ """ {"Kill":{"reason":"Random reason","force":false}}"""
29
+ .fromJson[Command ] shouldBe Right (
30
+ Kill (" Random reason" , false )
31
+ )
28
32
}
29
33
30
34
" decode Stop to JSON" in {
@@ -35,13 +39,21 @@ class DecodingSpec extends AnyWordSpec with Matchers {
35
39
Start (100 )
36
40
)
37
41
""" "Stop"""" .fromJson[Stop .type ] shouldBe Right (Stop )
42
+ """ {"Kill":{"reason":"Random reason","force":false}}"""
43
+ .fromJson[Command ] shouldBe Right (
44
+ Kill (" Random reason" , false )
45
+ )
38
46
}
39
47
40
48
" use a discriminator" in {
41
49
""" {"type":"Start2","timeout":100}""" .fromJson[Command2 ] shouldBe Right (
42
50
Start2 (100 )
43
51
)
44
52
""" {"type":"Stop2"}""" .fromJson[Command2 ] shouldBe Right (Stop2 )
53
+ """ {"type":"Kill2","reason":"Random reason","force":false}"""
54
+ .fromJson[Command2 ] shouldBe Right (
55
+ Kill2 (" Random reason" , false )
56
+ )
45
57
}
46
58
47
59
" fail if there's no discriminator" in {
Original file line number Diff line number Diff line change @@ -13,6 +13,10 @@ class EncodingSpec extends AnyWordSpec with Matchers {
13
13
" encode an ADT to JSON" in {
14
14
(Start (100 ): Command ).toJson shouldBe """ {"Start":{"timeout":100}}"""
15
15
(Stop : Command ).toJson shouldBe """ {"Stop":{}}"""
16
+ (Kill (
17
+ reason = " Random reason" ,
18
+ force = false
19
+ ): Command ).toJson shouldBe """ {"Kill":{"reason":"Random reason","force":false}}"""
16
20
}
17
21
18
22
" encode Stop to JSON" in {
@@ -21,13 +25,21 @@ class EncodingSpec extends AnyWordSpec with Matchers {
21
25
22
26
(Start (100 ): Command ).toJson shouldBe """ {"Start":{"timeout":100}}"""
23
27
Stop .toJson shouldBe """ "Stop""""
28
+ (Kill (
29
+ reason = " Random reason" ,
30
+ force = false
31
+ ): Command ).toJson shouldBe """ {"Kill":{"reason":"Random reason","force":false}}"""
24
32
}
25
33
26
34
" use a discriminator" in {
27
35
(Start2 (
28
36
100
29
37
): Command2 ).toJson shouldBe """ {"type":"Start2","timeout":100}"""
30
38
(Stop2 : Command2 ).toJson shouldBe """ {"type":"Stop2"}"""
39
+ (Kill2 (
40
+ reason = " Random reason" ,
41
+ force = false
42
+ ): Command2 ).toJson shouldBe """ {"type":"Kill2","reason":"Random reason","force":false}"""
31
43
}
32
44
}
33
45
}
You can’t perform that action at this time.
0 commit comments