com.fasterxml.jackson.databind.DeserializationContext Scala Examples

The following examples show how to use com.fasterxml.jackson.databind.DeserializationContext. 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: WriteMode.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.ebiznext.comet.schema.model.WriteMode.{APPEND, ERROR_IF_EXISTS, IGNORE, OVERWRITE}
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}
import org.apache.spark.sql.SaveMode


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[WriteDeserializer])
sealed case class WriteMode(value: String) {
  override def toString: String = value

  def toSaveMode: SaveMode = {
    this match {
      case OVERWRITE       => SaveMode.Overwrite
      case APPEND          => SaveMode.Append
      case ERROR_IF_EXISTS => SaveMode.ErrorIfExists
      case IGNORE          => SaveMode.Ignore
      case _ =>
        throw new Exception("Should never happen")
    }
  }
}

object WriteMode {

  def fromString(value: String): WriteMode = {
    value.toUpperCase() match {
      case "OVERWRITE"       => WriteMode.OVERWRITE
      case "APPEND"          => WriteMode.APPEND
      case "ERROR_IF_EXISTS" => WriteMode.ERROR_IF_EXISTS
      case "IGNORE"          => WriteMode.IGNORE
      case _ =>
        throw new Exception(s"Invalid Write Mode try one of ${writes}")
    }
  }

  object OVERWRITE extends WriteMode("OVERWRITE")

  object APPEND extends WriteMode("APPEND")

  object ERROR_IF_EXISTS extends WriteMode("ERROR_IF_EXISTS")

  object IGNORE extends WriteMode("IGNORE")

  val writes: Set[WriteMode] = Set(OVERWRITE, APPEND, ERROR_IF_EXISTS, IGNORE)
}

class WriteDeserializer extends JsonDeserializer[WriteMode] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): WriteMode = {
    val value = jp.readValueAs[String](classOf[String])
    WriteMode.fromString(value)
  }
} 
Example 2
Source File: MListDeserializer.scala    From sope   with Apache License 2.0 5 votes vote down vote up
package com.sope.etl.transform.model

import com.fasterxml.jackson.core.{JsonParser, JsonToken}
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.sope.etl.annotations.SqlExpr
import com.sope.etl.transform.model.action.TransformActionRoot
import com.sope.etl.transform.model.io.input.SourceTypeRoot
import com.sope.etl.transform.model.io.output.TargetTypeRoot
import com.sope.etl.utils.SQLChecker.checkSQL
import com.sope.utils.Logging

import scala.collection.mutable
import scala.reflect.ClassTag
import scala.reflect.runtime.universe._


    if (p.getCurrentToken != JsonToken.START_ARRAY) {
      val location = p.getCurrentLocation
      log.error(s"Invalid list definition for ${p.getCurrentName} tag")
      failures += Failed("Invalid yaml list definition", location.getLineNr, location.getColumnNr)
      return MList(data, failures)
    }

    while (p.nextToken() != JsonToken.END_ARRAY) {
      if (p.getCurrentToken == JsonToken.START_OBJECT && Option(p.getCurrentName).isEmpty) {
        val location = p.getCurrentLocation
        try {
          val validElem = p.readValueAs[T](clz)
          // Check if the element has any SQL expression/ SQL to be validated
          val clazz = mirror.staticClass(validElem.getClass.getCanonicalName)
          val objMirror = mirror.reflect(validElem)
          clazz.selfType.members.collect {
            case m: MethodSymbol if m.isCaseAccessor && m.annotations.exists(_.tree.tpe =:= typeOf[SqlExpr]) =>
              val expr = objMirror.reflectField(m).get
              if (m.name.toString.trim == "sql") (expr, true) else (expr, false)
          }.foreach { case (expr, isSql) => checkSQL(expr, isSql) }
          log.trace(s"Successfully Parsed element of type $clz :- $validElem")
          data += validElem
        }
        catch {
          case e: Exception =>
            log.error(s"Parsing failed with message ${e.getMessage} at ${location.getLineNr}:${location.getColumnNr}")
            failures += Failed(e.getMessage, location.getLineNr, location.getColumnNr)
        }
      } else {
        // Cases where the next token might be an internal object/array as result of failure on the root object.
        // These are skipped and token is moved to next object at root.
        if ((p.getCurrentToken == JsonToken.START_OBJECT || p.getCurrentToken == JsonToken.START_ARRAY)
          && Option(p.getCurrentName).isDefined) {
          log.debug("Skipping Current Token: " + p.getCurrentToken + " with Name: " + p.getCurrentName)
          p.skipChildren()
        }
      }
    }
    MList(data, failures)
  }

  override def getNullValue: MList[T] = MList[T](Nil)
}

object MListDeserializer {

  class TransformationDeserializer extends MListDeserializer(classOf[Transformation])

  class ActionDeserializer extends MListDeserializer(classOf[TransformActionRoot])

  class InputDeserializer extends MListDeserializer(classOf[SourceTypeRoot])

  class TargetDeserializer extends MListDeserializer(classOf[TargetTypeRoot])

} 
Example 3
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    context.handleUnexpectedToken(klass, parser)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll[JsonNode](fields)
} 
Example 4
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    throw context.mappingException(klass)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll(fields)
} 
Example 5
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    context.handleUnexpectedToken(klass, parser)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll(fields)
} 
Example 6
Source File: JacksonCompat.scala    From circe-jackson   with Apache License 2.0 5 votes vote down vote up
package io.circe.jackson

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{ DeserializationContext, JsonNode, ObjectMapper, ObjectWriter }
import com.fasterxml.jackson.databind.node.ObjectNode

private[jackson] trait JacksonCompat {
  protected def makeWriter(mapper: ObjectMapper): ObjectWriter = mapper.writerWithDefaultPrettyPrinter[ObjectWriter]()

  protected def handleUnexpectedToken(context: DeserializationContext)(
    klass: Class[_],
    parser: JsonParser
  ): Unit =
    throw context.mappingException(klass)

  protected def objectNodeSetAll(node: ObjectNode, fields: java.util.Map[String, JsonNode]): JsonNode =
    node.setAll(fields)
} 
Example 7
Source File: Program.scala    From fusion-data   with Apache License 2.0 5 votes vote down vote up
package mass.model.job

import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.core.{ JsonGenerator, JsonParser }
import com.fasterxml.jackson.databind.{ DeserializationContext, SerializerProvider }
import com.fasterxml.jackson.databind.annotation.{ JsonDeserialize, JsonSerialize }
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import helloscala.common.data.StringValueName

@JsonSerialize(using = classOf[Program.EnumSer])
@JsonDeserialize(using = classOf[Program.EnumDeser])
sealed abstract class Program(@JsonValue val value: String, val name: String) {
  def toValueName: StringValueName = StringValueName(value, name)
}

object Program {
  case object SCALA extends Program("scala", "Scala")
  case object JAVA extends Program("java", "Java")
  case object PYTHON extends Program("python", "Python")
  case object SH extends Program("sh", "SH")
  case object SQL extends Program("sql", "SQL")
  case object JS extends Program("js", "Javascript")

  val values = Vector(SCALA, JAVA, PYTHON, SH, SQL, JS)

  def fromValue(value: String): Program =
    optionFromValue(value).getOrElse(
      throw new NoSuchElementException(s"Program.values by name not found, it is $value."))

  def optionFromValue(value: String): Option[Program] = {
    val v = value.toLowerCase()
    values.find(_.value == v)
  }

  def fromName(name: String): Program =
    optionFromName(name).getOrElse(throw new NoSuchElementException(s"Program.values by name not found, it is $name."))

  def optionFromName(name: String): Option[Program] = {
    val n = name.toLowerCase()
    values.find(_.name == n)
  }

  class EnumSer extends StdSerializer[Program](classOf[Program]) {
    override def serialize(value: Program, gen: JsonGenerator, provider: SerializerProvider): Unit =
      gen.writeString(value.value)
  }
  class EnumDeser extends StdDeserializer[Program](classOf[Program]) {
    override def deserialize(p: JsonParser, ctxt: DeserializationContext): Program =
      Program.fromValue(p.getValueAsString)
  }
} 
Example 8
Source File: TriggerType.scala    From fusion-data   with Apache License 2.0 5 votes vote down vote up
package mass.model.job

import com.fasterxml.jackson.core.{ JsonGenerator, JsonParser }
import com.fasterxml.jackson.databind.annotation.{ JsonDeserialize, JsonSerialize }
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.databind.{ DeserializationContext, SerializerProvider }
import helloscala.common.util.{ IEnumTrait, IEnumTraitCompanion }

@JsonSerialize(using = classOf[TriggerType.EnumSer])
@JsonDeserialize(using = classOf[TriggerType.EnumDeser])
sealed abstract class TriggerType extends IEnumTrait[String] {
  override val value: String = name
}

object TriggerType extends IEnumTraitCompanion[String] {
  self =>
  override type Value = TriggerType

  case object SIMPLE extends TriggerType
  case object CRON extends TriggerType
  case object EVENT extends TriggerType

  override val values = Vector(CRON, EVENT, SIMPLE)

  override def optionFromValue(value: String): Option[TriggerType] = super.optionFromValue(value.toUpperCase())

  class EnumSer extends StdSerializer[TriggerType](classOf[TriggerType]) {
    override def serialize(value: TriggerType, gen: JsonGenerator, provider: SerializerProvider): Unit =
      gen.writeString(value.value)
  }
  class EnumDeser extends StdDeserializer[TriggerType](classOf[TriggerType]) {
    override def deserialize(p: JsonParser, ctxt: DeserializationContext): TriggerType =
      TriggerType.fromValue(p.getValueAsString.toUpperCase())
  }
} 
Example 9
Source File: SqlStatementSerialization.scala    From NSDb   with Apache License 2.0 5 votes vote down vote up
package io.radicalbit.nsdb.common.statement

import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.{DeserializationContext, SerializerProvider}
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer

object SqlStatementSerialization {

  object ComparisonOperatorSerialization {

    class ComparisonOperatorJsonSerializer extends StdSerializer[ComparisonOperator](classOf[ComparisonOperator]) {

      override def serialize(value: ComparisonOperator, gen: JsonGenerator, provider: SerializerProvider): Unit =
        gen.writeString(value.toString)
    }

    class ComparisonOperatorJsonDeserializer extends StdDeserializer[ComparisonOperator](classOf[ComparisonOperator]) {

      override def deserialize(p: JsonParser, ctxt: DeserializationContext): ComparisonOperator = {
        p.getText match {
          case "GreaterThanOperator"      => GreaterThanOperator
          case "GreaterOrEqualToOperator" => GreaterOrEqualToOperator
          case "LessThanOperator"         => LessThanOperator
          case "LessOrEqualToOperator"    => LessOrEqualToOperator
        }
      }
    }

  }

  object AggregationSerialization {

    class AggregationJsonSerializer extends StdSerializer[Aggregation](classOf[Aggregation]) {

      override def serialize(value: Aggregation, gen: JsonGenerator, provider: SerializerProvider): Unit =
        gen.writeString(value.toString)
    }

    class AggregationJsonDeserializer extends StdDeserializer[Aggregation](classOf[Aggregation]) {

      override def deserialize(p: JsonParser, ctxt: DeserializationContext): Aggregation = {
        p.getText match {
          case "CountAggregation" => CountAggregation
          case "MaxAggregation"   => MaxAggregation
          case "MinAggregation"   => MinAggregation
          case "SumAggregation"   => SumAggregation
          case "FirstAggregation" => FirstAggregation
          case "LastAggregation"  => LastAggregation
          case "AvgAggregation"   => AvgAggregation
        }
      }
    }

  }

  object LogicalOperatorSerialization {

    class LogicalOperatorJsonSerializer extends StdSerializer[LogicalOperator](classOf[LogicalOperator]) {

      override def serialize(value: LogicalOperator, gen: JsonGenerator, provider: SerializerProvider): Unit =
        gen.writeString(value.toString)
    }

    class LogicalOperatorJsonDeserializer extends StdDeserializer[LogicalOperator](classOf[LogicalOperator]) {

      override def deserialize(p: JsonParser, ctxt: DeserializationContext): LogicalOperator = {
        p.getText match {
          case "NotOperator" => NotOperator
          case "AndOperator" => AndOperator
          case "OrOperator"  => OrOperator
        }
      }
    }

  }
} 
Example 10
Source File: FieldClassType.scala    From NSDb   with Apache License 2.0 5 votes vote down vote up
package io.radicalbit.nsdb.common.protocol

import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.{DeserializationContext, SerializerProvider}
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer



@JsonSerialize(using = classOf[FieldClassTypeJsonSerializer])
@JsonDeserialize(using = classOf[FieldClassTypeJsonDeserializer])
sealed trait FieldClassType

case object TimestampFieldType extends FieldClassType
case object ValueFieldType     extends FieldClassType
case object DimensionFieldType extends FieldClassType
case object TagFieldType       extends FieldClassType

class FieldClassTypeJsonSerializer extends StdSerializer[FieldClassType](classOf[FieldClassType]) {

  override def serialize(value: FieldClassType, gen: JsonGenerator, provider: SerializerProvider): Unit =
    gen.writeString(value.toString)

}

class FieldClassTypeJsonDeserializer extends StdDeserializer[FieldClassType](classOf[FieldClassType]) {

  override def deserialize(p: JsonParser, ctxt: DeserializationContext): FieldClassType = {
    p.getText match {
      case "TimestampFieldType" => TimestampFieldType
      case "ValueFieldType"     => ValueFieldType
      case "DimensionFieldType" => DimensionFieldType
      case "TagFieldType"       => TagFieldType
    }
  }
} 
Example 11
Source File: EnumEntrySerializer.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.utils.json

import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, SerializerProvider}
import enumeratum.{Enum, EnumEntry}
import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString

import scala.reflect.ClassTag


  def jackson[A <: EnumEntry: ClassTag](enum: Enum[A]): SerDes[A] = {
    val klazz = implicitly[ClassTag[A]].runtimeClass.asInstanceOf[Class[A]]
    val ser = new StdSerializer[A](klazz) {
      override def serialize(value: A, gen: JsonGenerator, provider: SerializerProvider): Unit = {
        gen.writeString(value.entryName)
      }
    }
    val des = new StdDeserializer[A](klazz) {
      override def deserialize(p: JsonParser, ctxt: DeserializationContext): A = {
        enum.withNameInsensitive(p.getValueAsString)
      }
    }
    new SerDes[A](klazz, ser, des)
  }

} 
Example 12
Source File: HttpOrderBook.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.api.http.entities

import com.fasterxml.jackson.core.{JsonGenerator, JsonParser}
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonNode, ObjectMapper, SerializerProvider}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.wavesplatform.dex.domain.asset.Asset.{IssuedAsset, Waves}
import com.wavesplatform.dex.domain.asset.{Asset, AssetPair}
import com.wavesplatform.dex.domain.bytes.ByteStr
import com.wavesplatform.dex.domain.model.Denormalization
import com.wavesplatform.dex.model.LevelAgg

@JsonSerialize(using = classOf[HttpOrderBook.Serializer])
case class HttpOrderBook(timestamp: Long, pair: AssetPair, bids: Seq[LevelAgg], asks: Seq[LevelAgg], assetPairDecimals: Option[(Int, Int)] = None)

object HttpOrderBook {

  private val coreTypeSerializers = new SimpleModule()
  coreTypeSerializers.addDeserializer(classOf[AssetPair], new AssetPairDeserializer)

  private val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)
  mapper.registerModule(coreTypeSerializers)

  private def serialize(value: Any): String = mapper.writeValueAsString(value)

  private class AssetPairDeserializer extends StdDeserializer[AssetPair](classOf[AssetPair]) {

    override def deserialize(p: JsonParser, ctxt: DeserializationContext): AssetPair = {
      val node = p.getCodec.readTree[JsonNode](p)

      def readAssetId(fieldName: String): Asset = {
        val x = node.get(fieldName).asText(Asset.WavesName)
        if (x == Asset.WavesName) Waves else IssuedAsset(ByteStr.decodeBase58(x).get)
      }

      AssetPair(readAssetId("amountAsset"), readAssetId("priceAsset"))
    }
  }

  private def formatValue(value: BigDecimal, decimals: Int): String = new java.text.DecimalFormat(s"0.${"0" * decimals}").format(value)

  private def denormalizeAndSerializeSide(side: Seq[LevelAgg], amountAssetDecimals: Int, priceAssetDecimals: Int, jg: JsonGenerator): Unit = {
    side.foreach { levelAgg =>
      val denormalizedPrice  = Denormalization.denormalizePrice(levelAgg.price, amountAssetDecimals, priceAssetDecimals)
      val denormalizedAmount = Denormalization.denormalizeAmountAndFee(levelAgg.amount, amountAssetDecimals)

      jg.writeStartArray(2)
      jg.writeString(formatValue(denormalizedPrice, priceAssetDecimals))
      jg.writeString(formatValue(denormalizedAmount, amountAssetDecimals))
      jg.writeEndArray()
    }
  }

  def toJson(x: HttpOrderBook): String = serialize(x)

  class Serializer extends StdSerializer[HttpOrderBook](classOf[HttpOrderBook]) {
    override def serialize(x: HttpOrderBook, j: JsonGenerator, serializerProvider: SerializerProvider): Unit = {
      j.writeStartObject()
      j.writeNumberField("timestamp", x.timestamp)

      x.assetPairDecimals.fold {

        j.writeFieldName("pair")
        j.writeStartObject()
        j.writeStringField("amountAsset", x.pair.amountAssetStr)
        j.writeStringField("priceAsset", x.pair.priceAssetStr)
        j.writeEndObject()

        j.writeArrayFieldStart("bids")
        x.bids.foreach(j.writeObject)
        j.writeEndArray()

        j.writeArrayFieldStart("asks")
        x.asks.foreach(j.writeObject)
        j.writeEndArray()

      } {
        case (amountAssetDecimals, priceAssetDecimals) =>
          j.writeArrayFieldStart("bids")
          denormalizeAndSerializeSide(x.bids, amountAssetDecimals, priceAssetDecimals, j)
          j.writeEndArray()

          j.writeArrayFieldStart("asks")
          denormalizeAndSerializeSide(x.asks, amountAssetDecimals, priceAssetDecimals, j)
          j.writeEndArray()
      }

      j.writeEndObject()
    }
  }
} 
Example 13
Source File: Trim.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[TrimDeserializer])
sealed case class Trim(value: String) {
  override def toString: String = value
}

object Trim {

  def fromString(value: String): Trim = {
    value.toUpperCase() match {
      case "LEFT"  => Trim.LEFT
      case "RIGHT" => Trim.RIGHT
      case "BOTH"  => Trim.BOTH
      case "NONE"  => Trim.NONE
    }
  }

  object LEFT extends Trim("LEFT")

  object RIGHT extends Trim("RIGHT")

  object BOTH extends Trim("BOTH")

  object NONE extends Trim("NONE")

  val modes: Set[Trim] = Set(LEFT, RIGHT, BOTH, NONE)
}

class TrimDeserializer extends JsonDeserializer[Trim] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): Trim = {
    val value = jp.readValueAs[String](classOf[String])
    Trim.fromString(value)
  }
} 
Example 14
Source File: UserType.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[UserTypeDeserializer])
sealed case class UserType(value: String) {
  override def toString: String = value
}

object UserType {

  def fromString(value: String): UserType = {
    value.toUpperCase match {
      case "SA"    => UserType.SA
      case "USER"  => UserType.USER
      case "GROUP" => UserType.GROUP
    }
  }

  object SA extends UserType("SA")

  object USER extends UserType("USER")

  object GROUP extends UserType("GROUP")

  val formats: Set[UserType] = Set(SA, USER, GROUP)
}

class UserTypeDeserializer extends JsonDeserializer[UserType] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): UserType = {
    val value = jp.readValueAs[String](classOf[String])
    UserType.fromString(value)
  }
} 
Example 15
Source File: Partition.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer, JsonNode}


@JsonDeserialize(using = classOf[PartitionDeserializer])
case class Partition(
  sampling: Option[Double],
  attributes: Option[List[String]]
) {
  def getAttributes(): List[String] = attributes.getOrElse(Nil)

  def getSampling() = sampling.getOrElse(0.0)

}

class PartitionDeserializer extends JsonDeserializer[Partition] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): Partition = {
    val node: JsonNode = jp.getCodec().readTree[JsonNode](jp)
    deserialize(node)
  }

  def deserialize(node: JsonNode): Partition = {
    def isNull(field: String): Boolean =
      node.get(field) == null || node.get(field).isNull

    val sampling =
      if (isNull("sampling")) 0.0
      else
        node.get("sampling").asDouble()

    import scala.collection.JavaConverters._
    val attributes =
      if (isNull("attributes")) None
      else
        Some(
          node
            .get("attributes")
            .asInstanceOf[ArrayNode]
            .elements
            .asScala
            .toList
            .map(_.asText())
        )
    Partition(Some(sampling), attributes)
  }
} 
Example 16
Source File: MetricType.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[MetricTypeDeserializer])
sealed case class MetricType(value: String) {
  override def toString: String = value
}

object MetricType {

  def fromString(value: String): MetricType = {
    value.toUpperCase() match {
      case "DISCRETE"   => MetricType.DISCRETE
      case "CONTINUOUS" => MetricType.CONTINUOUS
      case "TEXT"       => MetricType.TEXT
      case "NONE"       => MetricType.NONE
    }
  }

  object DISCRETE extends MetricType("DISCRETE")

  object CONTINUOUS extends MetricType("CONTINUOUS")

  object TEXT extends MetricType("TEXT")

  object NONE extends MetricType("NONE")

  val metricTypes: Set[MetricType] = Set(NONE, DISCRETE, CONTINUOUS, TEXT)
}

class MetricTypeDeserializer extends JsonDeserializer[MetricType] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): MetricType = {
    val value = jp.readValueAs[String](classOf[String])
    MetricType.fromString(value)
  }
} 
Example 17
Source File: Mode.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[ModeDeserializer])
sealed case class Mode(value: String) {
  override def toString: String = value
}

object Mode {

  def fromString(value: String): Mode = {
    value.toUpperCase() match {
      case "FILE"            => Mode.FILE
      case "STREAM"          => Mode.STREAM
      case "FILE_AND_STREAM" => Mode.FILE_AND_STREAM
    }
  }

  object FILE extends Mode("FILE")

  object STREAM extends Mode("STREAM")

  object FILE_AND_STREAM extends Mode("FILE_AND_STREAM")

  val modes: Set[Mode] = Set(FILE, STREAM, FILE_AND_STREAM)
}

class ModeDeserializer extends JsonDeserializer[Mode] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): Mode = {
    val value = jp.readValueAs[String](classOf[String])
    Mode.fromString(value)
  }
} 
Example 18
Source File: IndexSink.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[IndexSinkDeserializer])
sealed case class IndexSink(value: String) {
  override def toString: String = value
}

object IndexSink {

  def fromString(value: String): IndexSink = {
    value.toUpperCase match {
      case "NONE" => IndexSink.None
      case "FS"   => IndexSink.FS
      case "JDBC" => IndexSink.JDBC
      case "BQ"   => IndexSink.BQ
      case "ES"   => IndexSink.ES
    }
  }

  object None extends IndexSink("None")

  object FS extends IndexSink("FS")

  object BQ extends IndexSink("BQ")

  object ES extends IndexSink("ES")

  object JDBC extends IndexSink("JDBC")

  val sinks: Set[IndexSink] = Set(None, FS, BQ, ES, JDBC)
}

class IndexSinkDeserializer extends JsonDeserializer[IndexSink] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): IndexSink = {
    val value = jp.readValueAs[String](classOf[String])
    IndexSink.fromString(value)
  }
} 
Example 19
Source File: Format.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[FormatDeserializer])
sealed case class Format(value: String) {
  override def toString: String = value
}

object Format {

  def fromString(value: String): Format = {
    value.toUpperCase match {
      case "DSV"                 => Format.DSV
      case "POSITION"            => Format.POSITION
      case "SIMPLE_JSON"         => Format.SIMPLE_JSON
      case "JSON" | "ARRAY_JSON" => Format.JSON
      case "CHEW"                => Format.CHEW
    }
  }

  object DSV extends Format("DSV")

  object POSITION extends Format("POSITION")

  object SIMPLE_JSON extends Format("SIMPLE_JSON")

  object JSON extends Format("JSON")

  object CHEW extends Format("CHEW")

  val formats: Set[Format] = Set(DSV, POSITION, SIMPLE_JSON, JSON, CHEW)
}

class FormatDeserializer extends JsonDeserializer[Format] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): Format = {
    val value = jp.readValueAs[String](classOf[String])
    Format.fromString(value)
  }
} 
Example 20
Source File: Stage.scala    From comet-data-pipeline   with Apache License 2.0 5 votes vote down vote up
package com.ebiznext.comet.schema.model

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonSerialize}
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


@JsonSerialize(using = classOf[ToStringSerializer])
@JsonDeserialize(using = classOf[StageDeserializer])
sealed case class Stage(value: String) {
  override def toString: String = value
}

object Stage {

  def fromString(value: String): Stage = {
    value.toUpperCase() match {
      case "UNIT"   => Stage.UNIT
      case "GLOBAL" => Stage.GLOBAL
    }
  }

  object UNIT extends Stage("UNIT")

  object GLOBAL extends Stage("GLOBAL")

  val stages: Set[Stage] = Set(UNIT, GLOBAL)
}

class StageDeserializer extends JsonDeserializer[Stage] {

  override def deserialize(jp: JsonParser, ctx: DeserializationContext): Stage = {
    val value = jp.readValueAs[String](classOf[String])
    Stage.fromString(value)
  }
} 
Example 21
Source File: InstantModule.scala    From milan   with Apache License 2.0 5 votes vote down vote up
package com.amazon.milan.dataformats

import java.time.format.{DateTimeFormatter, DateTimeParseException}
import java.time.temporal.{TemporalAccessor, TemporalQuery}
import java.time.{Instant, LocalDateTime, ZoneOffset}

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}



class InstantModule extends SimpleModule {
  this.addDeserializer[Instant](classOf[Instant], new MilanInstantDeserializer)
}


class MilanInstantDeserializer extends JsonDeserializer[Instant] {
  private val formatsToTry = List(
    DateTimeFormatter.ISO_INSTANT,
    DateTimeFormatter.ISO_DATE_TIME,
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"),
    DateTimeFormatter.ISO_DATE)

  override def deserialize(parser: JsonParser, context: DeserializationContext): Instant = {
    val textValue = parser.getText
    this.parseInstant(textValue)
  }

  private val createInstant = new TemporalQuery[Instant] {
    override def queryFrom(temporal: TemporalAccessor): Instant = LocalDateTime.from(temporal).toInstant(ZoneOffset.UTC)
  }


  private def parseInstant(dateTimeString: String): Instant = {
    // Try a bunch of formats.
    // TODO: This is awful but will do for now.
    formatsToTry.map(formatter => this.tryParseFormat(dateTimeString, formatter))
      .filter(_.isDefined)
      .map(_.get)
      .headOption match {
      case Some(instant) =>
        instant

      case None =>
        throw new DateTimeParseException(s"Unable to parse datetime string '$dateTimeString'.", dateTimeString, 0)
    }
  }

  private def tryParseFormat(dateTimeString: String,
                             formatter: DateTimeFormatter): Option[Instant] = {
    try {
      Some(formatter.parse(dateTimeString, this.createInstant))
    }
    catch {
      case _: DateTimeParseException =>
        None
    }
  }
}