-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 in BACIBBD/spline from feature/SL-62-rest-laye…
…r to feature/SL-43-new-data-model * commit '2b75005016c6c9c0d5f059a93b2b5935dd3cbdf7': SL-62 + scala doc SL-62 Fix REST serialization by switching from Salat JSON to Json4s impl
- Loading branch information
Showing
14 changed files
with
246 additions
and
128 deletions.
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
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
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
86 changes: 86 additions & 0 deletions
86
web/src/main/scala/za/co/absa/spline/web/json/StringJSONConverters.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,86 @@ | ||
/* | ||
* Copyright 2017 Barclays Africa Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.spline.web.json | ||
|
||
import java.net.URI | ||
|
||
import org.json4s.ext.UUIDSerializer | ||
import org.json4s.native.Serialization | ||
import org.json4s.{CustomSerializer, DateFormat, DefaultFormats, Formats, FullTypeHints, JNull, JString, Serializer} | ||
import za.co.absa.spline.common.ReflectionUtils.subClassesOf | ||
import za.co.absa.spline.common.TypeFreaks._ | ||
import za.co.absa.spline.model._ | ||
|
||
/** | ||
* Implicit JSON serializer/deserializer | ||
* <p> | ||
* Usage examples: | ||
* <code><pre> | ||
* import StringJSONConverters._ | ||
* | ||
* // Serialize object to JSON | ||
* val myObject: FooBar = ??? | ||
* val json:String == myObject.toJson | ||
* | ||
* // Deserialize from JSON | ||
* val jsonString: String = """{ "foo":42, "bar":777 }""" | ||
* val myObject: FooBar = jsonString.fromJson[FooBar] | ||
* </pre></code> | ||
*/ | ||
object StringJSONConverters { | ||
|
||
object URISerializer extends CustomSerializer[URI](_ => | ||
( { | ||
case JString(s) => new URI(s) | ||
case JNull => null | ||
}, { | ||
case uri: URI => JString(uri.toString) | ||
})) | ||
|
||
object SplineFormats extends Formats { | ||
val dateFormat: DateFormat = DefaultFormats.lossless.dateFormat | ||
|
||
override def typeHintFieldName: String = "_typeHint" | ||
|
||
override val typeHints = FullTypeHints( | ||
subClassesOf[op.Operation] | ||
++ subClassesOf[expr.Expression] | ||
++ subClassesOf[dt.DataType]) | ||
|
||
override val customSerializers: List[Serializer[_]] = | ||
UUIDSerializer :: URISerializer :: super.customSerializers | ||
} | ||
|
||
implicit val formats: Formats = SplineFormats | ||
|
||
implicit class JsonToModel(json: String) { | ||
|
||
def fromJson[T <: AnyRef : Manifest]: T = Serialization.read(json) | ||
|
||
def fromJsonArray[T <: AnyRef : Manifest]: Seq[T] = Serialization.read[Seq[T]](json) | ||
} | ||
|
||
implicit class CollectionToJson[T <: AnyRef : Manifest](xs: Traversable[T]) { | ||
def toJsonArray: String = Serialization.write(xs) | ||
} | ||
|
||
implicit class EntityToJson[T <: AnyRef : `not a subtype of`[Traversable[_]]#λ : Manifest](entity: T) { | ||
def toJson: String = Serialization.write(entity) | ||
} | ||
|
||
|
||
} |
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
40 changes: 0 additions & 40 deletions
40
web/src/main/scala/za/co/absa/spline/web/salat/StringJSONConverters.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.