-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
de3c252
commit 260ffe7
Showing
8 changed files
with
374 additions
and
1 deletion.
There are no files selected for viewing
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
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
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,18 @@ | ||
package enumeratum | ||
|
||
import org.json4s.CustomSerializer | ||
import org.json4s.JsonAST.JString | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.Any")) | ||
object Json4s { | ||
|
||
def serializer[A <: EnumEntry: Manifest](enum: Enum[A]): CustomSerializer[A] = new CustomSerializer[A](_ => ( | ||
{ | ||
case JString(s) if enum.withNameOption(s).isDefined => enum.withName(s) | ||
}, | ||
{ | ||
case x: A => JString(x.entryName) | ||
} | ||
)) | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
enumeratum-json4s/src/main/scala/enumeratum/values/Json4s.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,67 @@ | ||
package enumeratum.values | ||
|
||
import org.json4s.CustomSerializer | ||
import org.json4s.JsonAST.{JInt, JLong, JString} | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.Any")) | ||
object Json4s { | ||
|
||
def serializer[A <: IntEnumEntry: Manifest](enum: IntEnum[A]): CustomSerializer[A] = new CustomSerializer[A](_ => ( | ||
{ | ||
case JInt(i) if i <= Int.MaxValue && enum.withValueOpt(i.toInt).isDefined => enum.withValue(i.toInt) | ||
case JLong(i) if i <= Int.MaxValue && enum.withValueOpt(i.toInt).isDefined => enum.withValue(i.toInt) | ||
}, | ||
{ | ||
case x: A => JLong(x.value.toLong) | ||
} | ||
)) | ||
|
||
def serializer[A <: LongEnumEntry: Manifest](enum: LongEnum[A]): CustomSerializer[A] = new CustomSerializer[A](_ => ( | ||
{ | ||
case JInt(i) if enum.withValueOpt(i.toLong).isDefined => enum.withValue(i.toLong) | ||
case JLong(i) if enum.withValueOpt(i).isDefined => enum.withValue(i) | ||
}, | ||
{ | ||
case x: A => JLong(x.value) | ||
} | ||
)) | ||
|
||
def serializer[A <: ShortEnumEntry: Manifest](enum: ShortEnum[A]): CustomSerializer[A] = new CustomSerializer[A](_ => ( | ||
{ | ||
case JInt(i) if i <= Short.MaxValue.toInt && enum.withValueOpt(i.toShort).isDefined => enum.withValue(i.toShort) | ||
case JLong(i) if i <= Short.MaxValue && enum.withValueOpt(i.toShort).isDefined => enum.withValue(i.toShort) | ||
}, | ||
{ | ||
case x: A => JLong(x.value.toLong) | ||
} | ||
)) | ||
|
||
def serializer[A <: StringEnumEntry: Manifest](enum: StringEnum[A]): CustomSerializer[A] = new CustomSerializer[A](_ => ( | ||
{ | ||
case JString(s) if enum.withValueOpt(s).isDefined => enum.withValue(s) | ||
}, | ||
{ | ||
case x: A => JString(x.value) | ||
} | ||
)) | ||
|
||
def serializer[A <: ByteEnumEntry: Manifest](enum: ByteEnum[A]): CustomSerializer[A] = new CustomSerializer[A](_ => ( | ||
{ | ||
case JInt(i) if i <= Byte.MaxValue.toInt && enum.withValueOpt(i.toByte).isDefined => enum.withValue(i.toByte) | ||
case JLong(i) if i <= Byte.MaxValue && enum.withValueOpt(i.toByte).isDefined => enum.withValue(i.toByte) | ||
}, | ||
{ | ||
case x: A => JLong(x.value.toLong) | ||
} | ||
)) | ||
|
||
def serializer[A <: CharEnumEntry: Manifest](enum: CharEnum[A]): CustomSerializer[A] = new CustomSerializer[A](_ => ( | ||
{ | ||
case JString(s) if s.length == 1 && enum.withValueOpt(s.head).isDefined => enum.withValue(s.head) | ||
}, | ||
{ | ||
case x: A => JString(x.value.toString) | ||
} | ||
)) | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
enumeratum-json4s/src/test/scala/enumeratum/Json4sSpec.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,65 @@ | ||
package enumeratum | ||
|
||
import org.json4s.{DefaultFormats, MappingException} | ||
import org.json4s.native.Serialization | ||
import org.scalatest.{FunSpec, Matchers} | ||
|
||
class Json4sSpec extends FunSpec with Matchers { | ||
|
||
implicit val formats = DefaultFormats + Json4s.serializer(TrafficLight) | ||
|
||
case class Data(tr: TrafficLight) | ||
case class DataOpt(tr: Option[TrafficLight]) | ||
|
||
describe("to JSON") { | ||
it("should serialize plain value to entryName") { | ||
TrafficLight.values.foreach { value => | ||
Serialization.write(Data(tr = value)) shouldBe ("""{"tr":"""" + value.entryName + """"}""") | ||
} | ||
} | ||
|
||
it("should serialize Some(value) to entryName") { | ||
TrafficLight.values.foreach { value => | ||
Serialization.write(DataOpt(tr = Some(value))) shouldBe ("""{"tr":"""" + value.entryName + """"}""") | ||
} | ||
} | ||
|
||
it("should serialize None to nothing") { | ||
Serialization.write(DataOpt(tr = None)) shouldBe """{}""" | ||
} | ||
} | ||
|
||
describe("from JSON") { | ||
it("should parse enum members when given proper encoding") { | ||
TrafficLight.values.foreach { value => | ||
Serialization.read[Data]("""{"tr":"""" + value.entryName + """"}""").tr shouldBe value | ||
} | ||
} | ||
|
||
it("should parse enum members into optional values") { | ||
TrafficLight.values.foreach { value => | ||
Serialization.read[DataOpt]("""{"tr":"""" + value.entryName + """"}""").tr shouldBe Some(value) | ||
} | ||
} | ||
|
||
it("should parse missing value into None") { | ||
Serialization.read[DataOpt]("""{}""").tr shouldBe None | ||
} | ||
|
||
it("should parse invalid value into None") { | ||
Serialization.read[DataOpt]("""{"tr":"bogus"}""").tr shouldBe None | ||
Serialization.read[DataOpt]("""{"tr":17}""").tr shouldBe None | ||
Serialization.read[DataOpt]("""{"tr":true}""").tr shouldBe None | ||
Serialization.read[DataOpt]("""{"tr":null}""").tr shouldBe None | ||
} | ||
|
||
it("should fail to parse random JSON values to members") { | ||
a [MappingException] shouldBe thrownBy (Serialization.read[Data]("""{"tr":"bogus"}""")) | ||
a [MappingException] shouldBe thrownBy (Serialization.read[Data]("""{"tr":17}""")) | ||
a [MappingException] shouldBe thrownBy (Serialization.read[Data]("""{"tr":true}""")) | ||
a [MappingException] shouldBe thrownBy (Serialization.read[Data]("""{"tr":null}""")) | ||
a [MappingException] shouldBe thrownBy (Serialization.read[Data]("""{}""")) | ||
} | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
enumeratum-json4s/src/test/scala/enumeratum/TrafficLight.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,10 @@ | ||
package enumeratum | ||
|
||
sealed trait TrafficLight extends EnumEntry | ||
object TrafficLight extends Enum[TrafficLight] { | ||
case object Red extends TrafficLight | ||
case object Yellow extends TrafficLight | ||
case object Green extends TrafficLight | ||
|
||
val values = findValues | ||
} |
Oops, something went wrong.