aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala
diff options
context:
space:
mode:
authorKseniya Tomskikh <ktomskih@datamonsters.co>2017-08-04 17:57:12 +0600
committerKseniya Tomskikh <ktomskih@datamonsters.co>2017-08-11 14:41:17 +0600
commitef9517e1b8f599fbdd15c474cf7dfea61e803c2f (patch)
treec31a139d35dab0c10c70bb7afef2bca8e48f0fee /src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala
parent5519c219e2404cb19b6116dee90b40b5e5e2a720 (diff)
downloadrest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.tar.gz
rest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.tar.bz2
rest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.zip
PDSUI-2188 Created and fixed spray json formats for tric and rep
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala')
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala
new file mode 100644
index 0000000..fbb0258
--- /dev/null
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/common.scala
@@ -0,0 +1,66 @@
+package xyz.driver.pdsuidomain.formats.json.sprayformats
+
+import java.time.{LocalDate, LocalDateTime}
+
+import spray.json._
+import xyz.driver.pdsuicommon.domain.{FuzzyValue, LongId, StringId, UuidId}
+
+object common {
+
+ implicit def longIdFormat[T] = new RootJsonFormat[LongId[T]] {
+ override def write(id: LongId[T]): JsNumber = JsNumber(id.id)
+ override def read(json: JsValue): LongId[T] = json match {
+ case JsNumber(value) => LongId(value.toLong)
+ case _ => deserializationError(s"Expected number as LongId, but got $json")
+ }
+ }
+
+ implicit def stringIdFormat[T] = new RootJsonFormat[StringId[T]] {
+ override def write(id: StringId[T]): JsString = JsString(id.toString)
+ override def read(json: JsValue): StringId[T] = json match {
+ case JsString(value) => StringId(value)
+ case _ => deserializationError(s"Expected string as StringId, but got $json")
+ }
+ }
+
+ implicit def uuidIdFormat[T] = new RootJsonFormat[UuidId[T]] {
+ override def write(id: UuidId[T]): JsString = JsString(id.toString)
+ override def read(json: JsValue): UuidId[T] = json match {
+ case JsString(value) => UuidId(value)
+ case _ => deserializationError(s"Expected string as UuidId, but got $json")
+ }
+ }
+
+ implicit def dateTimeFormat = new RootJsonFormat[LocalDateTime] {
+ override def write(date: LocalDateTime): JsString = JsString(date.toString)
+ override def read(json: JsValue): LocalDateTime = json match {
+ case JsString(value) => LocalDateTime.parse(value)
+ case _ => deserializationError(s"Expected date as LocalDateTime, but got $json")
+ }
+ }
+
+ implicit def dateFormat = new RootJsonFormat[LocalDate] {
+ override def write(date: LocalDate): JsString = JsString(date.toString)
+ override def read(json: JsValue): LocalDate = json match {
+ case JsString(value) => LocalDate.parse(value)
+ case _ => deserializationError(s"Expected date as LocalDate, but got $json")
+ }
+ }
+
+ implicit def fuzzyValueFormat: RootJsonFormat[FuzzyValue] = new RootJsonFormat[FuzzyValue] {
+ override def write(value: FuzzyValue): JsString = JsString(FuzzyValue.valueToString(value))
+ override def read(json: JsValue): FuzzyValue = json match {
+ case JsString(value) => FuzzyValue.fromString(value)
+ case _ => deserializationError(s"Expected value as FuzzyValue, but got $json")
+ }
+ }
+
+ implicit val integerFormat: RootJsonFormat[Integer] = new RootJsonFormat[Integer] {
+ override def write(obj: Integer): JsNumber = JsNumber(obj.intValue())
+ override def read(json: JsValue): Integer = json match {
+ case JsNumber(value) => value.toInt
+ case _ => deserializationError(s"Expected number as Integer, but got $json")
+ }
+ }
+
+}