com.fasterxml.jackson.databind.SerializationFeature Scala Examples
The following examples show how to use com.fasterxml.jackson.databind.SerializationFeature.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: TsStreamingTest.scala From spark-riak-connector with Apache License 2.0 | 7 votes |
package com.basho.riak.spark.streaming import java.nio.ByteBuffer import java.util.concurrent.{Callable, Executors, TimeUnit} import com.basho.riak.spark._ import com.basho.riak.spark.rdd.RiakTSTests import com.basho.riak.spark.rdd.timeseries.{AbstractTimeSeriesTest, TimeSeriesData} import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper, SerializationFeature} import com.fasterxml.jackson.module.scala.DefaultScalaModule import org.apache.spark.sql.Row import org.junit.Assert._ import org.junit.experimental.categories.Category import org.junit.{After, Before, Test} @Category(Array(classOf[RiakTSTests])) class TsStreamingTest extends AbstractTimeSeriesTest(false) with SparkStreamingFixture { protected final val executorService = Executors.newCachedThreadPool() private val dataSource = new SocketStreamingDataSource private var port = -1 @Before def setUp(): Unit = { port = dataSource.start(client => { testData .map(tolerantMapper.writeValueAsString) .foreach(x => client.write(ByteBuffer.wrap(s"$x\n".getBytes))) logInfo(s"${testData.length} values were send to client") }) } @After def tearDown(): Unit = { dataSource.stop() } @Test(timeout = 10 * 1000) // 10 seconds timeout def saveToRiak(): Unit = { executorService.submit(new Runnable { override def run(): Unit = { ssc.socketTextStream("localhost", port) .map(string => { val tsdata = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true) .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true) .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .registerModule(DefaultScalaModule) .readValue(string, classOf[TimeSeriesData]) Row(1, "f", tsdata.time, tsdata.user_id, tsdata.temperature_k) }) .saveToRiakTS(bucketName) ssc.start() ssc.awaitTerminationOrTimeout(5 * 1000) } }) val result = executorService.submit(new Callable[Array[Seq[Any]]] { override def call(): Array[Seq[Any]] = { var rdd = sc.riakTSTable[Row](bucketName) .sql(s"SELECT user_id, temperature_k FROM $bucketName $sqlWhereClause") var count = rdd.count() while (count < testData.length) { TimeUnit.SECONDS.sleep(2) rdd = sc.riakTSTable[Row](bucketName) .sql(s"SELECT user_id, temperature_k FROM $bucketName $sqlWhereClause") count = rdd.count() } rdd.collect().map(_.toSeq) } }).get() assertEquals(testData.length, result.length) assertEqualsUsingJSONIgnoreOrder( """ |[ | ['bryce',305.37], | ['bryce',300.12], | ['bryce',295.95], | ['ratman',362.121], | ['ratman',3502.212] |] """.stripMargin, result) } }
Example 2
Source File: JacksonMessageWriter.scala From drizzle-spark with Apache License 2.0 | 6 votes |
package org.apache.spark.status.api.v1 import java.io.OutputStream import java.lang.annotation.Annotation import java.lang.reflect.Type import java.nio.charset.StandardCharsets import java.text.SimpleDateFormat import java.util.{Calendar, SimpleTimeZone} import javax.ws.rs.Produces import javax.ws.rs.core.{MediaType, MultivaluedMap} import javax.ws.rs.ext.{MessageBodyWriter, Provider} import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} @Provider @Produces(Array(MediaType.APPLICATION_JSON)) private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{ val mapper = new ObjectMapper() { override def writeValueAsString(t: Any): String = { super.writeValueAsString(t) } } mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat) override def isWriteable( aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Boolean = { true } override def writeTo( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType, multivaluedMap: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { t match { case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8)) case _ => mapper.writeValue(outputStream, t) } } override def getSize( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Long = { -1L } } private[spark] object JacksonMessageWriter { def makeISODateFormat: SimpleDateFormat = { val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'") val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")) iso8601.setCalendar(cal) iso8601 } }
Example 3
Source File: JacksonJsonSerializerTest.scala From akka-tools with MIT License | 5 votes |
package no.nextgentel.oss.akkatools.serializing import akka.actor.ActorSystem import akka.serialization.SerializationExtension import com.fasterxml.jackson.annotation.JsonTypeInfo import com.fasterxml.jackson.databind.{SerializationFeature, ObjectMapper} import com.fasterxml.jackson.module.scala.DefaultScalaModule import com.typesafe.config.{ConfigFactory, Config} import org.scalatest.{Matchers, FunSuite} class JacksonJsonSerializerTest extends FunSuite with Matchers { val objectMapper = new ObjectMapper() objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) objectMapper.registerModule(new DefaultScalaModule) test("serializer") { JacksonJsonSerializer.setObjectMapper(objectMapper) val serializer = new JacksonJsonSerializer() val a = Animal("our cat", 12, Cat("black", true)) val bytes = serializer.toBinary(a) val ar = serializer.fromBinary(bytes, classOf[Animal]).asInstanceOf[Animal] assert( a == ar) } test("Registering the serializer works") { JacksonJsonSerializer.setObjectMapper(objectMapper) val system = ActorSystem("JacksonJsonSerializerTest", ConfigFactory.load("akka-tools-json-serializing.conf")) val serialization = SerializationExtension.get(system) assert( classOf[JacksonJsonSerializer] == serialization.serializerFor(classOf[Animal]).getClass) system.terminate() } test("DepricatedTypeWithMigrationInfo") { JacksonJsonSerializer.setObjectMapper(objectMapper) val serializer = new JacksonJsonSerializer() val bytes = serializer.toBinary(OldType("12")) assert(NewType(12) == serializer.fromBinary(bytes, classOf[OldType])) } test("verifySerialization - no error") { JacksonJsonSerializer.setObjectMapper(objectMapper) JacksonJsonSerializer.setVerifySerialization(true) val serializer = new JacksonJsonSerializer() val a = Animal("our cat", 12, Cat("black", true)) val ow = ObjectWrapperWithTypeInfo(a) serializer.toBinary(ow) } test("verifySerialization - with error") { JacksonJsonSerializer.setObjectMapper(objectMapper) JacksonJsonSerializer.setVerifySerialization(true) val serializer = new JacksonJsonSerializer() val a = Animal("our cat", 12, Cat("black", true)) val ow = ObjectWrapperWithoutTypeInfo(a) intercept[JacksonJsonSerializerVerificationFailed] { serializer.toBinary(ow) } } test("verifySerialization - disabled") { JacksonJsonSerializer.setObjectMapper(objectMapper) JacksonJsonSerializer.setVerifySerialization(true) val serializer = new JacksonJsonSerializer() val a = Animal("our cat", 12, Cat("black", true)) val ow = ObjectWrapperWithoutTypeInfoOverrided(a) serializer.toBinary(ow) } } case class Animal(name:String, age:Int, t:Cat) extends JacksonJsonSerializable case class Cat(color:String, tail:Boolean) case class OldType(s:String) extends DepricatedTypeWithMigrationInfo { override def convertToMigratedType(): AnyRef = NewType(s.toInt) } case class NewType(i:Int) case class ObjectWrapperWithTypeInfo(@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@any_class") any:AnyRef) case class ObjectWrapperWithoutTypeInfo(any:AnyRef) case class ObjectWrapperWithoutTypeInfoOverrided(any:AnyRef) extends JacksonJsonSerializableButNotDeserializable
Example 4
Source File: JacksonScalaProvider.scala From daf-semantics with Apache License 2.0 | 5 votes |
package it.almawave.kb.http.providers import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider import com.fasterxml.jackson.databind.ObjectMapper import javax.ws.rs.ext.Provider import javax.ws.rs.Produces import com.fasterxml.jackson.module.scala.DefaultScalaModule import javax.ws.rs.core.MediaType import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.annotation.JsonAnyGetter import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.databind.DeserializationFeature import javax.ws.rs.ext.ContextResolver import com.fasterxml.jackson.databind.JsonSerializer import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.databind.SerializerProvider import java.lang.Double import java.lang.Boolean @Provider @Produces(Array(MediaType.APPLICATION_JSON)) class JacksonScalaProvider extends JacksonJaxbJsonProvider with ContextResolver[ObjectMapper] { println("\n\nregistered " + this.getClass) val mapper = new ObjectMapper() mapper .registerModule(DefaultScalaModule) .setSerializationInclusion(JsonInclude.Include.ALWAYS) .configure(SerializationFeature.INDENT_OUTPUT, true) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true) .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true) .configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true) .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true) .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) // .setVisibility(JsonMethod.FIELD, Visibility.ANY); .getSerializerProvider.setNullValueSerializer(new JsonSerializer[Object] { def serialize(obj: Object, gen: JsonGenerator, provider: SerializerProvider) { obj match { case bool: Boolean => gen.writeBoolean(false) case number: Integer => gen.writeNumber(0) case number: Double => gen.writeNumber(0.0D) case text: String => gen.writeString("") case _ => gen.writeString("") } } }) super.setMapper(mapper) override def getContext(klasses: Class[_]): ObjectMapper = mapper }
Example 5
Source File: RpcMainchainNodeApi.scala From Sidechains-SDK with MIT License | 5 votes |
package com.horizen.mainchain.api import java.io.{BufferedReader, InputStreamReader} import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} import com.horizen.SidechainSettings import com.horizen.serialization.ApplicationJsonSerializer import com.horizen.utils.BytesUtils class RpcMainchainNodeApi(val sidechainSettings: SidechainSettings) extends MainchainNodeApi { private lazy val isOsWindows = { val osname = System.getProperty("os.name", "generic").toLowerCase() osname.contains("win") } private val clientPath = sidechainSettings.websocket.zencliCommandLine + " " + (sidechainSettings.genesisData.mcNetwork match { case "regtest" => "-regtest " case "testnet" => "-testnet " case _ => "" }) private def callRpc(params: String) : String = { System.out.println(clientPath + " " + params) val process = Runtime.getRuntime.exec(clientPath + " " + params) val stdInput: BufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream)) val stdError: BufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream)) val error = stdError.readLine() if(error != null) throw new IllegalStateException("Error: " + error) stdInput.readLine } private def encloseJsonParameter(parameter: String): String = { if (isOsWindows) "\"" + parameter.replace("\"", "\\\"") + "\"" else "'" + parameter + "'" } private def encloseStringParameter(parameter: String): String = { "\"" + parameter + "\"" } override def getSidechainInfo: SidechainInfoResponse = { val objectMapper = new ObjectMapper() val response = callRpc("getscinfo") objectMapper.readValue(response, classOf[SidechainInfoResponse]) } override def sendCertificate(certificateRequest: SendCertificateRequest): SendCertificateResponse = { val serializer = ApplicationJsonSerializer.getInstance() // TODO: maybe it's better to construct object mapper from scratch serializer.setDefaultConfiguration() val objectMapper = serializer.getObjectMapper objectMapper.disable(SerializationFeature.INDENT_OUTPUT) val response = callRpc("send_certificate " + encloseStringParameter(BytesUtils.toHexString(certificateRequest.sidechainId)) + " " + certificateRequest.epochNumber + " " + certificateRequest.quality + " " + encloseStringParameter(BytesUtils.toHexString(certificateRequest.endEpochBlockHash)) + " " + encloseStringParameter(BytesUtils.toHexString(certificateRequest.proofBytes)) + " " + encloseJsonParameter(objectMapper.writeValueAsString(certificateRequest.backwardTransfers)) + " " + certificateRequest.fee ) SendCertificateResponse(BytesUtils.fromHexString(response)) } }
Example 6
Source File: JacksonMessageWriter.scala From sparkoscope with Apache License 2.0 | 5 votes |
package org.apache.spark.status.api.v1 import java.io.OutputStream import java.lang.annotation.Annotation import java.lang.reflect.Type import java.nio.charset.StandardCharsets import java.text.SimpleDateFormat import java.util.{Calendar, Locale, SimpleTimeZone} import javax.ws.rs.Produces import javax.ws.rs.core.{MediaType, MultivaluedMap} import javax.ws.rs.ext.{MessageBodyWriter, Provider} import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} @Provider @Produces(Array(MediaType.APPLICATION_JSON)) private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{ val mapper = new ObjectMapper() { override def writeValueAsString(t: Any): String = { super.writeValueAsString(t) } } mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat) override def isWriteable( aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Boolean = { true } override def writeTo( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType, multivaluedMap: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { t match { case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8)) case _ => mapper.writeValue(outputStream, t) } } override def getSize( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Long = { -1L } } private[spark] object JacksonMessageWriter { def makeISODateFormat: SimpleDateFormat = { val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'", Locale.US) val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")) iso8601.setCalendar(cal) iso8601 } }
Example 7
Source File: BigQueryTypeSpec.scala From shapeless-datatype with Apache License 2.0 | 5 votes |
package shapeless.datatype.bigquery import java.net.URI import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} import com.google.api.services.bigquery.model.TableRow import com.google.common.io.BaseEncoding import com.google.protobuf.ByteString import org.joda.time.{Instant, LocalDate, LocalDateTime, LocalTime} import org.scalacheck.Prop.forAll import org.scalacheck.ScalacheckShapeless._ import org.scalacheck._ import shapeless._ import shapeless.datatype.record._ import scala.reflect.runtime.universe._ object BigQueryTypeSpec extends Properties("BigQueryType") { import shapeless.datatype.test.Records._ import shapeless.datatype.test.SerializableUtils._ val mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) implicit def compareByteArrays(x: Array[Byte], y: Array[Byte]) = java.util.Arrays.equals(x, y) implicit def compareIntArrays(x: Array[Int], y: Array[Int]) = java.util.Arrays.equals(x, y) def roundTrip[A: TypeTag, L <: HList](m: A)(implicit gen: LabelledGeneric.Aux[A, L], fromL: FromTableRow[L], toL: ToTableRow[L], mr: MatchRecord[L] ): Boolean = { BigQuerySchema[A] // FIXME: verify the generated schema val t = ensureSerializable(BigQueryType[A]) val f1: SerializableFunction[A, TableRow] = new SerializableFunction[A, TableRow] { override def apply(m: A): TableRow = t.toTableRow(m) } val f2: SerializableFunction[TableRow, Option[A]] = new SerializableFunction[TableRow, Option[A]] { override def apply(m: TableRow): Option[A] = t.fromTableRow(m) } val toFn = ensureSerializable(f1) val fromFn = ensureSerializable(f2) val copy = fromFn(mapper.readValue(mapper.writeValueAsString(toFn(m)), classOf[TableRow])) val rm = RecordMatcher[A] copy.exists(rm(_, m)) } implicit val byteStringBigQueryMappableType = BigQueryType.at[ByteString]("BYTES")( x => ByteString.copyFrom(BaseEncoding.base64().decode(x.toString)), x => BaseEncoding.base64().encode(x.toByteArray) ) property("required") = forAll { m: Required => roundTrip(m) } property("optional") = forAll { m: Optional => roundTrip(m) } property("repeated") = forAll { m: Repeated => roundTrip(m) } property("mixed") = forAll { m: Mixed => roundTrip(m) } property("nested") = forAll { m: Nested => roundTrip(m) } property("seqs") = forAll { m: Seqs => roundTrip(m) } implicit val arbDate = Arbitrary(arbInstant.arbitrary.map(i => new LocalDate(i.getMillis))) implicit val arbTime = Arbitrary(arbInstant.arbitrary.map(i => new LocalTime(i.getMillis))) implicit val arbDateTime = Arbitrary( arbInstant.arbitrary.map(i => new LocalDateTime(i.getMillis)) ) case class DateTimeTypes( instant: Instant, date: LocalDate, time: LocalTime, dateTime: LocalDateTime ) property("date time types") = forAll { m: DateTimeTypes => roundTrip(m) } implicit val uriBigQueryType = BigQueryType.at[URI]("STRING")(v => URI.create(v.toString), _.toASCIIString) property("custom") = forAll { m: Custom => roundTrip(m) } }
Example 8
Source File: JacksonMessageWriter.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
package org.apache.spark.status.api.v1 import java.io.OutputStream import java.lang.annotation.Annotation import java.lang.reflect.Type import java.nio.charset.StandardCharsets import java.text.SimpleDateFormat import java.util.{Calendar, Locale, SimpleTimeZone} import javax.ws.rs.Produces import javax.ws.rs.core.{MediaType, MultivaluedMap} import javax.ws.rs.ext.{MessageBodyWriter, Provider} import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} @Provider @Produces(Array(MediaType.APPLICATION_JSON)) private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{ val mapper = new ObjectMapper() { override def writeValueAsString(t: Any): String = { super.writeValueAsString(t) } } mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat) override def isWriteable( aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Boolean = { true } override def writeTo( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType, multivaluedMap: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { t match { case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8)) case _ => mapper.writeValue(outputStream, t) } } override def getSize( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Long = { -1L } } private[spark] object JacksonMessageWriter { def makeISODateFormat: SimpleDateFormat = { val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'", Locale.US) val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")) iso8601.setCalendar(cal) iso8601 } }
Example 9
Source File: JacksonMessageWriter.scala From iolap with Apache License 2.0 | 5 votes |
package org.apache.spark.status.api.v1 import java.io.OutputStream import java.lang.annotation.Annotation import java.lang.reflect.Type import java.text.SimpleDateFormat import java.util.{Calendar, SimpleTimeZone} import javax.ws.rs.Produces import javax.ws.rs.core.{MediaType, MultivaluedMap} import javax.ws.rs.ext.{MessageBodyWriter, Provider} import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} @Provider @Produces(Array(MediaType.APPLICATION_JSON)) private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{ val mapper = new ObjectMapper() { override def writeValueAsString(t: Any): String = { super.writeValueAsString(t) } } mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat) override def isWriteable( aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Boolean = { true } override def writeTo( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType, multivaluedMap: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { t match { case ErrorWrapper(err) => outputStream.write(err.getBytes("utf-8")) case _ => mapper.writeValue(outputStream, t) } } override def getSize( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Long = { -1L } } private[spark] object JacksonMessageWriter { def makeISODateFormat: SimpleDateFormat = { val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'") val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")) iso8601.setCalendar(cal) iso8601 } }
Example 10
Source File: JacksonMessageWriter.scala From spark1.52 with Apache License 2.0 | 5 votes |
package org.apache.spark.status.api.v1 import java.io.OutputStream import java.lang.annotation.Annotation import java.lang.reflect.Type import java.text.SimpleDateFormat import java.util.{Calendar, SimpleTimeZone} import javax.ws.rs.Produces import javax.ws.rs.core.{MediaType, MultivaluedMap} import javax.ws.rs.ext.{MessageBodyWriter, Provider} import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} @Provider @Produces(Array(MediaType.APPLICATION_JSON)) private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{ val mapper = new ObjectMapper() { override def writeValueAsString(t: Any): String = { super.writeValueAsString(t) } } mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat) override def isWriteable( aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Boolean = { true } override def writeTo( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType, multivaluedMap: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { t match { case ErrorWrapper(err) => outputStream.write(err.getBytes("utf-8")) case _ => mapper.writeValue(outputStream, t) } } override def getSize( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Long = { -1L } } private[spark] object JacksonMessageWriter { def makeISODateFormat: SimpleDateFormat = { val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'") val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")) iso8601.setCalendar(cal) iso8601 } }
Example 11
Source File: TableRowDiffy.scala From ratatool with Apache License 2.0 | 5 votes |
package com.spotify.ratatool.diffy import java.io.StringReader import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} import com.google.api.client.json.JsonObjectParser import com.google.api.client.json.jackson2.JacksonFactory import com.google.api.services.bigquery.model.{TableFieldSchema, TableRow, TableSchema} import scala.jdk.CollectionConverters._ import scala.util.Try class TableRowDiffy(tableSchema: TableSchema, ignore: Set[String] = Set.empty, unordered: Set[String] = Set.empty, unorderedFieldKeys: Map[String, String] = Map()) extends Diffy[TableRow](ignore, unordered, unorderedFieldKeys) { override def apply(x: TableRow, y: TableRow): Seq[Delta] = diff(Option(x), Option(y), schema.getFields.asScala.toList, "") private type Record = java.util.Map[String, AnyRef] // TableSchema is not serializable private val schemaString: String = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) .writeValueAsString(tableSchema) private lazy val schema: TableSchema = new JsonObjectParser(new JacksonFactory) .parseAndClose(new StringReader(schemaString), classOf[TableSchema]) // scalastyle:off cyclomatic.complexity private def diff(x: Option[Record], y: Option[Record], fields: Seq[TableFieldSchema], root: String): Seq[Delta] = { def getField(f: String)(x: Record): Option[AnyRef] = { Option(x.get(f)) } fields.flatMap { f => val name = f.getName val fullName = if (root.isEmpty) name else root + "." + name if (f.getType == "RECORD" && f.getMode != "REPEATED") { val a = x.flatMap(r => getField(name)(r).map(_.asInstanceOf[Record])) val b = y.flatMap(r => getField(name)(r).map(_.asInstanceOf[Record])) if (a.isEmpty && b.isEmpty) { Nil } else if (a.isEmpty || b.isEmpty) { Seq(Delta(fullName, a, b, UnknownDelta)) } else { diff(a, b, f.getFields.asScala.toList, fullName) } } else if (f.getMode == "REPEATED" && unordered.contains(fullName)) { if (f.getType == "RECORD" && unorderedFieldKeys.contains(fullName)) { val l = x .flatMap(outer => getField(name)(outer).map(_.asInstanceOf[java.util.List[Record]].asScala.toList)) .getOrElse(List()) .flatMap(inner => Try(inner.get(unorderedFieldKeys(fullName))).toOption.map(k => (k, inner))).toMap val r = y .flatMap(outer => getField(name)(outer).map(_.asInstanceOf[java.util.List[Record]].asScala.toList)) .getOrElse(List()) .flatMap(inner => Try(inner.get(unorderedFieldKeys(fullName))).toOption.map(k => (k, inner))).toMap (l.keySet ++ r.keySet).flatMap(k => diff(l.get(k), r.get(k), f.getFields.asScala.toList, fullName)) } else { val a = x.flatMap(r => Option(r.get(name).asInstanceOf[java.util.List[AnyRef]])) .map(sortList) val b = y.flatMap(r => Option(r.get(name).asInstanceOf[java.util.List[AnyRef]])) .map(sortList) if (a == b) Nil else Seq(Delta(fullName, a, b, delta(a.orNull, b.orNull))) } } else { val a = x.flatMap(r => getField(name)(r)) val b = y.flatMap(r => getField(name)(r)) if (a == b) Nil else Seq(Delta(fullName, a, b, delta(a.orNull, b.orNull))) } }.filter(d => !ignore.contains(d.field)) } // scalastyle:on cyclomatic.complexity }
Example 12
Source File: JacksonMessageWriter.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.status.api.v1 import java.io.OutputStream import java.lang.annotation.Annotation import java.lang.reflect.Type import java.nio.charset.StandardCharsets import java.text.SimpleDateFormat import java.util.{Calendar, Locale, SimpleTimeZone} import javax.ws.rs.Produces import javax.ws.rs.core.{MediaType, MultivaluedMap} import javax.ws.rs.ext.{MessageBodyWriter, Provider} import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} @Provider @Produces(Array(MediaType.APPLICATION_JSON)) private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{ val mapper = new ObjectMapper() { override def writeValueAsString(t: Any): String = { super.writeValueAsString(t) } } mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat) override def isWriteable( aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Boolean = { true } override def writeTo( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType, multivaluedMap: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { t match { case ErrorWrapper(err) => outputStream.write(err.getBytes(StandardCharsets.UTF_8)) case _ => mapper.writeValue(outputStream, t) } } override def getSize( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Long = { -1L } } private[spark] object JacksonMessageWriter { def makeISODateFormat: SimpleDateFormat = { val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'", Locale.US) val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")) iso8601.setCalendar(cal) iso8601 } }
Example 13
Source File: JacksonMessageWriter.scala From BigDatalog with Apache License 2.0 | 5 votes |
package org.apache.spark.status.api.v1 import java.io.OutputStream import java.lang.annotation.Annotation import java.lang.reflect.Type import java.text.SimpleDateFormat import java.util.{Calendar, SimpleTimeZone} import javax.ws.rs.Produces import javax.ws.rs.core.{MediaType, MultivaluedMap} import javax.ws.rs.ext.{MessageBodyWriter, Provider} import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature} @Provider @Produces(Array(MediaType.APPLICATION_JSON)) private[v1] class JacksonMessageWriter extends MessageBodyWriter[Object]{ val mapper = new ObjectMapper() { override def writeValueAsString(t: Any): String = { super.writeValueAsString(t) } } mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) mapper.setDateFormat(JacksonMessageWriter.makeISODateFormat) override def isWriteable( aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Boolean = { true } override def writeTo( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType, multivaluedMap: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { t match { case ErrorWrapper(err) => outputStream.write(err.getBytes("utf-8")) case _ => mapper.writeValue(outputStream, t) } } override def getSize( t: Object, aClass: Class[_], `type`: Type, annotations: Array[Annotation], mediaType: MediaType): Long = { -1L } } private[spark] object JacksonMessageWriter { def makeISODateFormat: SimpleDateFormat = { val iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'GMT'") val cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")) iso8601.setCalendar(cal) iso8601 } }
Example 14
Source File: ProjectDefaultJacksonMapper.scala From orders-aws with Apache License 2.0 | 5 votes |
package works.weave.socks.aws.orders import com.fasterxml.jackson.annotation.JsonAutoDetect import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.annotation.PropertyAccessor import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule import com.fasterxml.jackson.module.scala.DefaultScalaModule object ProjectDefaultJacksonMapper { def build() : ObjectMapper = { val mapper = new ObjectMapper() mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS) mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) mapper.enable(SerializationFeature.INDENT_OUTPUT) mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) val javaTime : JavaTimeModule = new JavaTimeModule mapper.registerModule(javaTime) val scalaModule = new DefaultScalaModule() mapper.registerModule(scalaModule) mapper } }