-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Car.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.fasterxml.jackson.module.scala.`enum` | ||
|
||
case class Car(make: String, color: ColorEnum) |
5 changes: 5 additions & 0 deletions
5
src/test/scala-3/com/fasterxml/jackson/module/scala/enum/ColorEnum.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.fasterxml.jackson.module.scala.`enum` | ||
|
||
enum ColorEnum { case Red, Green, Blue } | ||
|
||
case class Colors(set: Set[ColorEnum]) |
7 changes: 7 additions & 0 deletions
7
src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Ctx.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.fasterxml.jackson.module.scala.`enum` | ||
|
||
object Ctx { | ||
enum ColorEnum { case Red, Green, Blue } | ||
} | ||
|
||
case class CtxCar(make: String, color: Ctx.ColorEnum) |
53 changes: 53 additions & 0 deletions
53
src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumDeserializerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.fasterxml.jackson.module.scala.`enum` | ||
|
||
import com.fasterxml.jackson.core.`type`.TypeReference | ||
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException | ||
import com.fasterxml.jackson.databind.json.JsonMapper | ||
import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class EnumDeserializerSpec extends AnyWordSpec with Matchers { | ||
val mapper = JsonMapper.builder().addModule(DefaultScalaModule).build() | ||
|
||
"EnumModule" should { | ||
"deserialize ColorEnum" in { | ||
val red = s""""${ColorEnum.Red}"""" | ||
mapper.readValue(red, classOf[ColorEnum]) shouldEqual ColorEnum.Red | ||
} | ||
"fail deserialization of invalid ColorEnum" in { | ||
val json = s""""xyz"""" | ||
intercept[IllegalArgumentException] { | ||
mapper.readValue(json, classOf[ColorEnum]) | ||
} | ||
} | ||
"deserialize Colors" in { | ||
val colors = Colors(Set(ColorEnum.Red, ColorEnum.Green)) | ||
val json = mapper.writeValueAsString(colors) | ||
mapper.readValue(json, classOf[Colors]) shouldEqual colors | ||
} | ||
"deserialize ColorEnum with non-singleton EnumModule" in { | ||
val red = s""""${ColorEnum.Red}"""" | ||
mapper.readValue(red, classOf[ColorEnum]) shouldEqual ColorEnum.Red | ||
} | ||
"deserialize JavaCompatibleColorEnum" in { | ||
mapper.writeValueAsString(JavaCompatibleColorEnum.Red) shouldEqual s""""${JavaCompatibleColorEnum.Red}"""" | ||
} | ||
"deserialize Car with ColorEnum" in { | ||
val red = s"""{"make":"Perodua","color":"${ColorEnum.Green}"}""" | ||
mapper.readValue(red, classOf[Car]) shouldEqual Car("Perodua", ColorEnum.Green) | ||
} | ||
"deserialize CtxCar with Ctx.ColorEnum" in { | ||
val red = s"""{"make":"Perodua","color":"${Ctx.ColorEnum.Green}"}""" | ||
mapper.readValue(red, classOf[CtxCar]) shouldEqual CtxCar("Perodua", Ctx.ColorEnum.Green) | ||
} | ||
"deserialize Enum as Map Key" in { | ||
val json = s"""{"Green":"green","Red":"red"}""" | ||
val map = mapper.readValue(json, new TypeReference[Map[ColorEnum, String]] {}) | ||
map should have size 2 | ||
map(ColorEnum.Green) shouldEqual "green" | ||
map(ColorEnum.Red) shouldEqual "red" | ||
} | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumSerializerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.fasterxml.jackson.module.scala.`enum` | ||
|
||
import com.fasterxml.jackson.databind.json.JsonMapper | ||
import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class EnumSerializerSpec extends AnyWordSpec with Matchers { | ||
val mapper = JsonMapper.builder().addModule(DefaultScalaModule).build() | ||
|
||
"EnumModule" should { | ||
"not serialize None" in { | ||
mapper.writeValueAsString(None) should not equal s""""$None"""" | ||
} | ||
"serialize ColorEnum" in { | ||
mapper.writeValueAsString(ColorEnum.Red) shouldEqual s""""${ColorEnum.Red}"""" | ||
} | ||
"serialize Colors" in { | ||
val json = mapper.writeValueAsString(Colors(Set(ColorEnum.Red, ColorEnum.Green))) | ||
json should startWith("""{"set":[""") | ||
json should include(""""Red"""") | ||
json should include(""""Green"""") | ||
} | ||
"serialize ColorEnum with non-singleton EnumModule" in { | ||
mapper.writeValueAsString(ColorEnum.Red) shouldEqual s""""${ColorEnum.Red}"""" | ||
} | ||
"serialize JavaCompatibleColorEnum" in { | ||
mapper.writeValueAsString(ColorEnum.Red) shouldEqual s""""${ColorEnum.Red}"""" | ||
} | ||
"serialize Car with ColorEnum" in { | ||
mapper.writeValueAsString(Car("Perodua", ColorEnum.Green)) shouldEqual s"""{"make":"Perodua","color":"${ColorEnum.Green}"}""" | ||
} | ||
"serialize CtxCar with Ctx.ColorEnum" in { | ||
mapper.writeValueAsString(CtxCar("Perodua", Ctx.ColorEnum.Green)) shouldEqual s"""{"make":"Perodua","color":"${Ctx.ColorEnum.Green}"}""" | ||
} | ||
"serialize Enum as Map Key" in { | ||
mapper.writeValueAsString(Map(ColorEnum.Green -> "green")) shouldEqual s"""{"Green":"green"}""" | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/test/scala-3/com/fasterxml/jackson/module/scala/enum/JavaCompatibleColorEnum.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.fasterxml.jackson.module.scala.`enum` | ||
|
||
enum JavaCompatibleColorEnum extends java.lang.Enum[JavaCompatibleColorEnum] { case Red, Green, Blue } |