diff --git a/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Car.scala b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Car.scala new file mode 100644 index 000000000..26f2b4545 --- /dev/null +++ b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Car.scala @@ -0,0 +1,3 @@ +package com.fasterxml.jackson.module.scala.`enum` + +case class Car(make: String, color: ColorEnum) diff --git a/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/ColorEnum.scala b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/ColorEnum.scala new file mode 100644 index 000000000..09b0fb001 --- /dev/null +++ b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/ColorEnum.scala @@ -0,0 +1,5 @@ +package com.fasterxml.jackson.module.scala.`enum` + +enum ColorEnum { case Red, Green, Blue } + +case class Colors(set: Set[ColorEnum]) diff --git a/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Ctx.scala b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Ctx.scala new file mode 100644 index 000000000..bd8c46ec2 --- /dev/null +++ b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/Ctx.scala @@ -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) diff --git a/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumDeserializerSpec.scala b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumDeserializerSpec.scala new file mode 100644 index 000000000..b161f2a4b --- /dev/null +++ b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumDeserializerSpec.scala @@ -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" + } + + } +} diff --git a/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumSerializerSpec.scala b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumSerializerSpec.scala new file mode 100644 index 000000000..b2ef27eb9 --- /dev/null +++ b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/EnumSerializerSpec.scala @@ -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"}""" + } + } +} diff --git a/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/JavaCompatibleColorEnum.scala b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/JavaCompatibleColorEnum.scala new file mode 100644 index 000000000..e4f38cffd --- /dev/null +++ b/src/test/scala-3/com/fasterxml/jackson/module/scala/enum/JavaCompatibleColorEnum.scala @@ -0,0 +1,3 @@ +package com.fasterxml.jackson.module.scala.`enum` + +enum JavaCompatibleColorEnum extends java.lang.Enum[JavaCompatibleColorEnum] { case Red, Green, Blue }