org.json4s.JsonAST.JString Scala Examples

The following examples show how to use org.json4s.JsonAST.JString. 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: DateTimeNoMillisSerializer.scala    From avoin-voitto   with MIT License 6 votes vote down vote up
package liigavoitto.util

import org.joda.time.{ DateTime, DateTimeZone }
import org.joda.time.format.ISODateTimeFormat
import org.json4s.CustomSerializer
import org.json4s.JsonAST.{ JNull, JString }

case object DateTimeNoMillisSerializer extends CustomSerializer[DateTime](format => (
  {
    case JString(s) => ISODateTimeFormat.dateTimeNoMillis().withZone(Time.zone).parseDateTime(s)
    case JNull => null
  },
  {
    case d: DateTime => JString(ISODateTimeFormat.dateTimeNoMillis().withZone(Time.zone).print(d))
  }
))

object Time {
  val zone = DateTimeZone.forID("Europe/Helsinki")
} 
Example 2
Source File: TemplateSerializationFormat.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.model.serialization

import io.vamp.model.artifact._
import org.json4s.JsonAST.JString
import org.json4s._

import scala.collection.mutable.ArrayBuffer

object TemplateSerializationFormat extends io.vamp.common.json.SerializationFormat {
  override def customSerializers = super.customSerializers :+ new TemplateSerializer()
}

class TemplateSerializer extends ArtifactSerializer[Template] {
  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case template: Template ⇒
      val list = new ArrayBuffer[JField]
      list += JField("name", JString(template.name))
      list += JField("kind", JString(template.kind))
      list += JField("metadata", Extraction.decompose(template.metadata)(DefaultFormats))
      list += JField("definition", Extraction.decompose(template.definition)(DefaultFormats))
      new JObject(list.toList)
  }
} 
Example 3
Source File: SpecialDoubleSerializer.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.utils.json

import org.json4s.CustomSerializer
import org.json4s.JsonAST.{JDouble, JString, JDecimal}


// scalastyle:off
class SpecialDoubleSerializer extends CustomSerializer[Double](ser =>
  ({
    case JString("NaN") => Double.NaN
    case JString("-Infinity") => Double.NegativeInfinity
    case JString("Infinity") => Double.PositiveInfinity
    case JDouble(v) => v
  }, {
    case v: Double if v.isNaN => JString("NaN")
    case Double.NegativeInfinity => JString("-Infinity")
    case Double.PositiveInfinity => JString("Infinity")
    case v: Double if ser.wantsBigDecimal => JDecimal(v)
    case v: Double => JDouble(v)
  })) 
Example 4
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 5
Source File: MasterWebUISuite.scala    From BigDatalog   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.deploy.master.ui

import java.util.Date

import scala.io.Source
import scala.language.postfixOps

import org.json4s.jackson.JsonMethods._
import org.json4s.JsonAST.{JNothing, JString, JInt}
import org.mockito.Mockito.{mock, when}
import org.scalatest.BeforeAndAfter

import org.apache.spark.{SparkConf, SecurityManager, SparkFunSuite}
import org.apache.spark.deploy.DeployMessages.MasterStateResponse
import org.apache.spark.deploy.DeployTestUtils._
import org.apache.spark.deploy.master._
import org.apache.spark.rpc.RpcEnv


class MasterWebUISuite extends SparkFunSuite with BeforeAndAfter {

  val masterPage = mock(classOf[MasterPage])
  val master = {
    val conf = new SparkConf
    val securityMgr = new SecurityManager(conf)
    val rpcEnv = RpcEnv.create(Master.SYSTEM_NAME, "localhost", 0, conf, securityMgr)
    val master = new Master(rpcEnv, rpcEnv.address, 0, securityMgr, conf)
    master
  }
  val masterWebUI = new MasterWebUI(master, 0, customMasterPage = Some(masterPage))

  before {
    masterWebUI.bind()
  }

  after {
    masterWebUI.stop()
  }

  test("list applications") {
    val worker = createWorkerInfo()
    val appDesc = createAppDesc()
    // use new start date so it isn't filtered by UI
    val activeApp = new ApplicationInfo(
      new Date().getTime, "id", appDesc, new Date(), null, Int.MaxValue)
    activeApp.addExecutor(worker, 2)

    val workers = Array[WorkerInfo](worker)
    val activeApps = Array(activeApp)
    val completedApps = Array[ApplicationInfo]()
    val activeDrivers = Array[DriverInfo]()
    val completedDrivers = Array[DriverInfo]()
    val stateResponse = new MasterStateResponse(
      "host", 8080, None, workers, activeApps, completedApps,
      activeDrivers, completedDrivers, RecoveryState.ALIVE)

    when(masterPage.getMasterState).thenReturn(stateResponse)

    val resultJson = Source.fromURL(
      s"http://localhost:${masterWebUI.boundPort}/api/v1/applications")
      .mkString
    val parsedJson = parse(resultJson)
    val firstApp = parsedJson(0)

    assert(firstApp \ "id" === JString(activeApp.id))
    assert(firstApp \ "name" === JString(activeApp.desc.name))
    assert(firstApp \ "coresGranted" === JInt(2))
    assert(firstApp \ "maxCores" === JInt(4))
    assert(firstApp \ "memoryPerExecutorMB" === JInt(1234))
    assert(firstApp \ "coresPerExecutor" === JNothing)
  }

} 
Example 6
Source File: AdminSvc.scala    From squbs   with Apache License 2.0 5 votes vote down vote up
package org.squbs.admin

import java.net.URLEncoder

import akka.http.scaladsl.model.Uri.Path
import akka.http.scaladsl.model._
import org.json4s.JsonAST.{JObject, JString}
import org.json4s.jackson.JsonMethods._
import org.squbs.unicomplex.{RouteDefinition, WebContext}
import org.squbs.util.ConfigUtil._

class AdminSvc extends RouteDefinition with WebContext {

  val prefix = if (webContext == "") "/bean" else s"/$webContext/bean"

  val exclusions = context.system.settings.config.get[Seq[String]]("squbs.admin.exclusions", Seq.empty[String]).toSet
  val (exBeans, exFieldSet) = exclusions partition { !_.contains("::") }

  val exFields = exFieldSet map { fieldSpec =>
    val fields = fieldSpec split "::"
    fields(0) -> fields(1)
  } groupBy (_._1) mapValues { _.map(_._2) }


  val route =
    get {
      pathEndOrSingleSlash {
        extractUri { uri =>
          complete {
            val kv = MBeanUtil.allObjectNames collect {
              case name if !(exBeans contains name) =>
                val resource = Path(s"$prefix/${URLEncoder.encode(name.replace('=', '~'), "UTF-8")}")
                name -> JString(uri.withPath(resource).toString())
            }
            HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, pretty(render(JObject(kv)))))
          }
        }
      } ~
      path("bean" / Segment) { encName =>
        complete {
          val name = encName.replace('~', '=').replace('%', '/')
          val response: HttpResponse =
            if (exBeans contains name) HttpResponse(StatusCodes.NotFound, entity = StatusCodes.NotFound.defaultMessage)
            else MBeanUtil.asJSON(name, exFields getOrElse (name, Set.empty))
              .map { json => HttpResponse(entity = json) }
              .getOrElse (HttpResponse(StatusCodes.NotFound, entity = StatusCodes.NotFound.defaultMessage))
          response
        }
      }
    }
} 
Example 7
Source File: BoardConfig.scala    From slab   with Apache License 2.0 5 votes vote down vote up
package com.criteo.slab.app

import com.criteo.slab.core.{Box, Layout}
import com.criteo.slab.utils.Jsonable
import org.json4s.JsonAST.{JArray, JString}
import org.json4s.{CustomSerializer, Serializer}


private[slab] case class BoardConfig(
                                      title: String,
                                      layout: Layout,
                                      links: Seq[(Box[_], Box[_])] = Seq.empty,
                                      slo: Double
                                    )

object BoardConfig {
  implicit object ToJSON extends Jsonable[BoardConfig] {
    override val serializers: Seq[Serializer[_]] =
      implicitly[Jsonable[Box[_]]].serializers ++
        implicitly[Jsonable[Layout]].serializers :+
        LinkSer

    object LinkSer extends CustomSerializer[Box[_] Tuple2 Box[_]](_ => ( {
      case _ => throw new NotImplementedError("Not deserializable")
    }, {
      case (Box(title1, _, _, _, _), Box(title2, _, _, _, _)) => JArray(List(JString(title1), JString(title2)))
    }
    ))

  }
} 
Example 8
Source File: Status.scala    From slab   with Apache License 2.0 5 votes vote down vote up
package com.criteo.slab.core

import com.criteo.slab.utils.Jsonable
import org.json4s.JsonAST.JString
import org.json4s.{CustomSerializer, Serializer}


sealed class Status(val name: String, val level: Int) extends Ordered[Status] {
  override def compare(that: Status) = this.level.compare(that.level)
}

object Status {
  case object Success extends Status("SUCCESS", 0)
  case object Warning extends Status("WARNING", 1)
  case object Error extends Status("ERROR", 2)
  case object Unknown extends Status("UNKNOWN", 3)

  def from(in: String) = in.toUpperCase match {
    case "SUCCESS" => Success
    case "WARNING" => Warning
    case "ERROR" => Error
    case "UNKNOWN" => Unknown
  }

  implicit object ToJSON extends Jsonable[Status] {
    override val serializers: Seq[Serializer[_]] = List(Ser)

    object Ser extends CustomSerializer[Status](_ => (
      {
        case JString(status) => Status.from(status)
      },
      {
        case s: Status => JString(s.name)
      }
    ))

  }
} 
Example 9
Source File: WorkflowSerializationFormat.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.model.serialization

import java.time.format.DateTimeFormatter._

import io.vamp.model.artifact.TimeSchedule.RepeatCount
import io.vamp.model.artifact._
import org.json4s.JsonAST.JString
import org.json4s._

import scala.collection.mutable.ArrayBuffer

object WorkflowSerializationFormat extends io.vamp.common.json.SerializationFormat {
  override def customSerializers: List[Serializer[_]] = super.customSerializers :+
    new WorkflowSerializer()
}

class WorkflowSerializer
    extends ArtifactSerializer[Workflow]
    with ReferenceSerialization
    with ArgumentListSerializer
    with HealthCheckSerializer
    with TraitDecomposer
    with DialectSerializer {
  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case workflow: Workflow ⇒
      val list = new ArrayBuffer[JField]
      list += JField("name", JString(workflow.name))
      list += JField("kind", JString(workflow.kind))
      list += JField("metadata", Extraction.decompose(workflow.metadata)(DefaultFormats))
      list += JField("breed", Extraction.decompose(workflow.breed))
      list += JField("status", JString(workflow.status.toString))

      workflow.schedule match {
        case TimeSchedule(period, repeatTimes, start) ⇒
          val time = new ArrayBuffer[JField]
          time += JField("period", JString(period.format))
          repeatTimes match {
            case RepeatCount(count) ⇒ time += JField("repeat", JInt(count))
            case _                  ⇒
          }
          start.foreach(start ⇒ time += JField("start", JString(start.format(ISO_OFFSET_DATE_TIME))))
          list += JField("schedule", new JObject(JField("time", new JObject(time.toList)) :: Nil))

        case EventSchedule(tags) ⇒
          val tagList = JField("tags", Extraction.decompose(tags)) :: Nil
          val event = JField("event", new JObject(tagList)) :: Nil
          list += JField("schedule", new JObject(event))

        case DaemonSchedule ⇒
          list += JField("schedule", JString("daemon"))

        case _ ⇒
      }

      if (workflow.environmentVariables.nonEmpty) list += JField("environment_variables", traits(workflow.environmentVariables.asInstanceOf[List[Trait]]))
      if (workflow.scale.isDefined) list += JField("scale", Extraction.decompose(workflow.scale.get))
      if (workflow.network.isDefined) list += JField("network", Extraction.decompose(workflow.network.get))
      if (workflow.arguments.nonEmpty) list += JField("arguments", serializeArguments(workflow.arguments))
      if (workflow.instances.nonEmpty) list += JField("instances", Extraction.decompose(workflow.instances))
      if (workflow.dialects.nonEmpty) list += JField("dialects", serializeDialects(workflow.dialects))

      // Add optional values
      workflow.health.foreach(h ⇒ list += JField("health", Extraction.decompose(h)))
      workflow.healthChecks.foreach(hcs ⇒ list += JField("health_checks", serializeHealthChecks(hcs)))

      new JObject(list.toList)
  }
} 
Example 10
Source File: DeploymentSerializationFormat.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.model.serialization

import java.time.format.DateTimeFormatter

import io.vamp.common.Lookup
import io.vamp.model.artifact.DeploymentService.Status.Phase.Failed
import io.vamp.model.artifact._
import io.vamp.model.notification.ModelNotificationProvider
import org.json4s.JsonAST.JString
import org.json4s._

import scala.collection.mutable.ArrayBuffer

object DeploymentSerializationFormat extends io.vamp.common.json.SerializationFormat {
  override def customSerializers = super.customSerializers :+
    new DeploymentSerializer(full = false) :+
    new DeploymentServiceStatusSerializer() :+
    new DeploymentServiceStatusPhaseSerializer()
}

object FullDeploymentSerializationFormat extends io.vamp.common.json.SerializationFormat {
  override def customSerializers = super.customSerializers :+
    new DeploymentSerializer(full = true) :+
    new DeploymentServiceStatusSerializer() :+
    new DeploymentServiceStatusPhaseSerializer()
}

class DeploymentSerializer(full: Boolean) extends ArtifactSerializer[Deployment] with TraitDecomposer with BlueprintGatewaySerializer with DialectSerializer {
  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case deployment: Deployment ⇒
      val list = new ArrayBuffer[JField]
      list += JField("name", JString(deployment.name))
      list += JField("kind", JString(deployment.kind))
      list += JField("metadata", Extraction.decompose(deployment.metadata)(DefaultFormats))
      list += JField(Lookup.entry, JString(deployment.lookupName))
      list += JField("clusters", Extraction.decompose(deployment.clusters.map(cluster ⇒ cluster.name → cluster).toMap))
      list += JField("ports", traits(deployment.ports))

      if (full) list += JField("environment_variables", Extraction.decompose(deployment.environmentVariables))
      else list += JField("environment_variables", traitsEnv(deployment.environmentVariables, alias = false))

      list += JField("hosts", traits(deployment.hosts))
      list += JField("dialects", serializeDialects(deployment.dialects))

      new JObject(list.toList)
  }
}

class DeploymentServiceStatusSerializer extends ArtifactSerializer[DeploymentService.Status] {
  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case status: DeploymentService.Status ⇒
      val list = new ArrayBuffer[JField]

      list += JField("intention", JString(status.intention.toString))
      list += JField("since", JString(status.since.format(DateTimeFormatter.ISO_DATE_TIME)))
      list += JField("phase", Extraction.decompose(status.phase))

      new JObject(list.toList)
  }
}

class DeploymentServiceStatusPhaseSerializer extends ArtifactSerializer[DeploymentService.Status.Phase] with ModelNotificationProvider {
  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case step: DeploymentService.Status.Phase ⇒

      val list = new ArrayBuffer[JField]

      list += JField("name", JString(step.name))
      list += JField("since", JString(step.since.format(DateTimeFormatter.ISO_DATE_TIME)))

      step match {
        case failure: Failed ⇒ list += JField("notification", JString(message(failure.notification)))
        case _               ⇒
      }

      new JObject(list.toList)
  }
} 
Example 11
Source File: BreedSerializationFormat.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.model.serialization

import io.vamp.common.Reference
import io.vamp.model.artifact._
import io.vamp.model.resolver.TraitNameAliasResolver
import org.json4s.FieldSerializer._
import org.json4s.JsonAST.JString
import org.json4s._

import scala.collection.mutable.ArrayBuffer
import scala.language.postfixOps

object BreedSerializationFormat extends io.vamp.common.json.SerializationFormat {

  override def customSerializers = super.customSerializers :+
    new BreedSerializer() :+
    new ArgumentSerializer

  override def fieldSerializers = super.fieldSerializers :+
    new BreedFieldSerializer()
}

trait TraitDecomposer extends TraitNameAliasResolver {

  def traits(traits: List[Trait], alias: Boolean = true) = {
    def traitName(name: String) = TraitReference.referenceFor(name) match {
      case Some(TraitReference(c, g, Host.host)) if g == TraitReference.groupFor(TraitReference.Hosts) ⇒ c
      case Some(TraitReference(c, g, n)) ⇒ s"$c${TraitReference.delimiter}$n"
      case None ⇒ name
    }

    new JObject(traits.map(t ⇒ t.name → t).toMap.values.map { t ⇒
      val name = traitName(if (alias) asName(t.name, t.alias) else t.name)
      val value = if (t.value == null || t.value.isEmpty) JNull
      else {
        JString(t match {
          case EnvironmentVariable(_, _, v, i) ⇒ i.getOrElse(v.get)
          case any                             ⇒ t.value.get
        })
      }

      JField(name, value)
    } toList)
  }

  def traitsEnv(traits: List[Trait], alias: Boolean = true) = {
    def traitName(name: String) = name

    new JObject(traits.map(t ⇒ t.name → t).toMap.values.map { t ⇒
      val name = traitName(if (alias) asName(t.name, t.alias) else t.name)
      val value = if (t.value == null || t.value.isEmpty) JNull
      else {
        JString(t match {
          case EnvironmentVariable(_, _, v, i) ⇒ i.getOrElse(v.get)
          case any                             ⇒ t.value.get
        })
      }

      JField(name, value)
    } toList)
  }
}

class BreedSerializer extends ArtifactSerializer[Breed] with TraitDecomposer with ReferenceSerialization with HealthCheckSerializer {
  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case breed: BreedReference ⇒ serializeReference(breed)
    case breed: DefaultBreed ⇒
      val list = new ArrayBuffer[JField]
      list += JField("name", JString(breed.name))
      list += JField("kind", JString(breed.kind))
      list += JField("metadata", Extraction.decompose(breed.metadata)(DefaultFormats))
      list += JField("deployable", Extraction.decompose(breed.deployable))
      list += JField("ports", traits(breed.ports))
      list += JField("environment_variables", traitsEnv(breed.environmentVariables))
      list += JField("constants", traits(breed.constants))
      list += JField("arguments", Extraction.decompose(breed.arguments))

      val dependencies = breed.dependencies.map {
        case (name, reference: Reference) ⇒ JField(name, JString(reference.name))
        case (name, dependency)           ⇒ JField(name, Extraction.decompose(dependency))
      } toList

      list += JField("dependencies", new JObject(dependencies))
      breed.healthChecks.foreach(healthChecks ⇒ list += JField("health_checks", serializeHealthChecks(healthChecks)))

      new JObject(list.toList)
  }
}

class ArgumentSerializer extends ArtifactSerializer[Argument] {
  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case argument: Argument ⇒ new JObject(JField(argument.key, JString(argument.value)) :: Nil)
  }
}

class BreedFieldSerializer extends ArtifactFieldSerializer[DefaultBreed] {
  override val serializer: PartialFunction[(String, Any), Option[(String, Any)]] =
    ignore("traits") orElse renameTo("environmentVariables", "environment_variables")
} 
Example 12
Source File: BatchClientSuite.scala    From hail   with MIT License 5 votes vote down vote up
package is.hail.services.batch_client

import is.hail.utils._

import org.json4s.JsonAST.{JArray, JBool, JInt, JObject, JString}
import org.json4s.{DefaultFormats, Formats}
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test

class BatchClientSuite extends TestNGSuite {
  @Test def testBasic(): Unit = {
    val client = new BatchClient()
    val token = tokenUrlSafe(32)
    val batch = client.run(
      JObject(
        "billing_project" -> JString("test"),
        "n_jobs" -> JInt(1),
        "token" -> JString(token)),
      FastIndexedSeq(
        JObject(
          "always_run" -> JBool(false),
          "image" -> JString("ubuntu:18.04"),
          "mount_docker_socket" -> JBool(false),
          "command" -> JArray(List(
            JString("/bin/bash"),
            JString("-c"),
            JString("echo 'Hello, world!'"))),
          "job_id" -> JInt(0),
          "parent_ids" -> JArray(List()))))
    implicit val formats: Formats = DefaultFormats
    assert((batch \ "state").extract[String] == "success")
  }
} 
Example 13
Source File: ThrowableSerializer.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.common.json

import org.json4s.JsonAST.{ JObject, JString }
import org.json4s._

object ThrowableSerializer {
  def apply(message: Option[String]): SerializationFormat = new SerializationFormat {
    override def customSerializers = super.customSerializers :+ new ThrowableSerializer(message)
  }
}

class ThrowableSerializer(message: Option[String] = None) extends Serializer[Throwable] {

  override def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
    case t: Throwable ⇒ new JObject(List(JField("message", JString(message.getOrElse(t.getMessage)))))
  }

  override def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Throwable] = SerializationFormat.unsupported
} 
Example 14
Source File: New.scala    From aardpfark   with Apache License 2.0 5 votes vote down vote up
package com.ibm.aardpfark.pfa.expression

import com.ibm.aardpfark.pfa.document.SchemaSerializer
import com.sksamuel.avro4s.{AvroSchema, SchemaFor, ToSchema}
import org.apache.avro.Schema
import org.json4s.JValue
import org.json4s.JsonAST.JString
import org.json4s.native.JsonMethods.parse

trait New {

  object NewRecord {
    def apply(schema: Schema, init: Map[String, PFAExpression], fullSchema: Boolean = true) =
      NewRecordExpr(schema, init, fullSchema)
  }

  case class NewRecordExpr(schema: Schema, init: Map[String, PFAExpression], fullSchema: Boolean)
    extends PFAExpression {
    import org.json4s.JsonDSL._

    private val s = if (fullSchema) SchemaSerializer.convert(schema) else JString(schema.getFullName)
    override def json: JValue = {
      ("type" -> s) ~ ("new" -> init.mapValues(_.json))
    }
  }

  case class NewArrayExpr(schema: Schema, init: Seq[PFAExpression]) extends PFAExpression {
    import org.json4s.JsonDSL._

    override def json: JValue = {
      ("type" -> parse(schema.toString)) ~ ("new" -> init.map(_.json))
    }
  }

  object NewArray {
    def apply(schema: Schema, init: Seq[PFAExpression]) = NewArrayExpr(schema, init)
    def apply[T](init: Seq[PFAExpression])(implicit s: ToSchema[Seq[T]]) = {
      NewArrayExpr(s(), init)
    }
  }

  case class NewMap(schema: Schema, init: Map[String, PFAExpression]) extends PFAExpression {
    import org.json4s.JsonDSL._

    override def json: JValue = {
      ("type" -> parse(schema.toString)) ~ ("new" -> init.mapValues(_.json))
    }
  }

} 
Example 15
Source File: ComponentFlagSerializer.scala    From PackUpdate   with Apache License 2.0 5 votes vote down vote up
package at.chaosfield.packupdate.json.serializer

import at.chaosfield.packupdate.common.ComponentFlag
import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString

object ComponentFlagSerializer extends CustomSerializer[ComponentFlag](format => (
  {
    case JString(string: String) => ComponentFlag.fromString(string) match {
      case Some(flag) => flag
      case None => throw new Exception(s"unknown component flag $string")
    }
  },
  {
    case ty: ComponentFlag => JString(ty.internalName)
  }
)) 
Example 16
Source File: ComponentTypeSerializer.scala    From PackUpdate   with Apache License 2.0 5 votes vote down vote up
package at.chaosfield.packupdate.json.serializer

import at.chaosfield.packupdate.common.ComponentType
import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString

object ComponentTypeSerializer extends CustomSerializer[ComponentType](format => (
  {
    case JString(string: String) => ComponentType.fromString(string) match {
      case Some(ty) => ty
      case None => throw new Exception(s"unknown component type $string")
    }
  },
  {
    case ty: ComponentType => JString(ty.stringValue)
  }
)) 
Example 17
Source File: Library.scala    From scala-clippy   with Apache License 2.0 5 votes vote down vote up
package com.softwaremill.clippy

import org.json4s.JsonAST.{JField, JObject, JString, JValue}

case class Library(groupId: String, artifactId: String, version: String) {
  def toJson: JValue = JObject(
    "groupId"    -> JString(groupId),
    "artifactId" -> JString(artifactId),
    "version"    -> JString(version)
  )

  override def toString = s"$groupId:$artifactId:$version"
}

object Library {
  def fromJson(jvalue: JValue): Option[Library] =
    (for {
      JObject(fields)                           <- jvalue
      JField("groupId", JString(groupId))       <- fields
      JField("artifactId", JString(artifactId)) <- fields
      JField("version", JString(version))       <- fields
    } yield Library(groupId, artifactId, version)).headOption
} 
Example 18
Source File: CustomFormats.scala    From twitter4s   with Apache License 2.0 5 votes vote down vote up
package com.danielasfregola.twitter4s.http.serializers

import java.time._

import com.danielasfregola.twitter4s.entities.enums.DisconnectionCode
import com.danielasfregola.twitter4s.entities.enums.DisconnectionCode.DisconnectionCode
import com.danielasfregola.twitter4s.entities.ProfileImage
import org.json4s.JsonAST.{JInt, JLong, JNull, JString}
import org.json4s.{CustomSerializer, Formats}

private[twitter4s] object CustomFormats extends FormatsComposer {

  override def compose(f: Formats): Formats =
    f + InstantSerializer + LocalDateSerializer + DisconnectionCodeSerializer + ProfileImageSerializer

}

private[twitter4s] case object InstantSerializer
    extends CustomSerializer[Instant](format =>
      ({
        case JInt(i)                                            => DateTimeFormatter.parseInstant(i.toLong)
        case JLong(l)                                           => DateTimeFormatter.parseInstant(l)
        case JString(s) if DateTimeFormatter.canParseInstant(s) => DateTimeFormatter.parseInstant(s)
        case JString(stringAsUnixTime) if stringAsUnixTime.forall(_.isDigit) =>
          Instant.ofEpochMilli(stringAsUnixTime.toLong)
      }, {
        case instant: Instant => JString(DateTimeFormatter.formatInstant(instant))
      }))

private[twitter4s] case object LocalDateSerializer
    extends CustomSerializer[LocalDate](format =>
      ({
        case JString(dateString) =>
          dateString.split("-") match {
            case Array(year, month, date) => LocalDate.of(year.toInt, month.toInt, date.toInt)
            case _                        => null
          }
        case JNull => null
      }, {
        case date: LocalDate => JString(date.toString)
      }))

private[twitter4s] case object DisconnectionCodeSerializer
    extends CustomSerializer[DisconnectionCode](format =>
      ({
        case JInt(n) => DisconnectionCode(n.toInt)
        case JNull   => null
      }, {
        case code: DisconnectionCode => JInt(code.id)
      }))

private[twitter4s] case object ProfileImageSerializer
    extends CustomSerializer[ProfileImage](format =>
      ({
        case JString(n) => ProfileImage(n)
        case JNull      => null
      }, {
        case img: ProfileImage => JString(img.normal)
      })) 
Example 19
Source File: Durations.scala    From scalapb-json4s   with Apache License 2.0 5 votes vote down vote up
package scalapb.json4s

import java.text.ParseException

import com.google.protobuf.duration.Duration
import org.json4s.JsonAST.JString

object Durations {
  val DURATION_SECONDS_MIN = -315576000000L
  val DURATION_SECONDS_MAX = 315576000000L

  def checkValid(duration: com.google.protobuf.duration.Duration): Unit = {
    val secondsInRange = (duration.seconds >= DURATION_SECONDS_MIN &&
      duration.seconds <= DURATION_SECONDS_MAX)
    val nanosInRange =
      duration.nanos >= -999999999L && duration.nanos <= Timestamps.NANOS_PER_SECOND
    val sameSign =
      !((duration.seconds < 0 || duration.nanos < 0) && (duration.seconds > 0 || duration.nanos > 0))
    require(
      secondsInRange && nanosInRange && sameSign,
      "Duration is not valid."
    )
  }

  def writeDuration(duration: com.google.protobuf.duration.Duration): String = {
    checkValid(duration)
    val result = new StringBuilder
    val (seconds, nanos) = if (duration.seconds < 0 || duration.nanos < 0) {
      result.append("-")
      (-duration.seconds, -duration.nanos)
    } else (duration.seconds, duration.nanos)

    result.append(seconds)
    if (nanos != 0) {
      result.append(".")
      result.append(Timestamps.formatNanos(nanos))
    }
    result.append("s")
    result.result()
  }

  def parseNanos(value: String): Int = {
    val h = value.take(9)
    if (!h.forall(_.isDigit)) {
      throw new ParseException("Invalid nanoseconds.", 0)
    }
    h.padTo(9, '0').toInt
  }

  def parseDuration(value: String): Duration = {
    if (!value.endsWith("s")) {
      throw new ParseException("Invalid duration string: " + value, 0)
    }
    val (negative, number) = if (value.startsWith("-")) {
      (true, value.substring(1, value.length - 1))
    } else (false, value.substring(0, value.length - 1))

    val pointPosition = number.indexOf('.')
    val (secondsStr, nanosStr) = if (pointPosition != -1) {
      (number.substring(0, pointPosition), number.substring(pointPosition + 1))
    } else {
      (number, "")
    }
    val seconds = secondsStr.toLong
    val nanos =
      if (nanosStr.isEmpty) 0
      else
        parseNanos(nanosStr)

    if (seconds < 0) {
      throw new ParseException("Invalid duration string: " + value, 0)
    }

    // TODO(thesamet): normalizedDuration?

    com.google.protobuf.duration.Duration(
      seconds = if (negative) -seconds else seconds,
      nanos = if (negative) -nanos else nanos
    )
  }
} 
Example 20
Source File: AnyFormat.scala    From scalapb-json4s   with Apache License 2.0 5 votes vote down vote up
package scalapb.json4s

import com.google.protobuf.any.{Any => PBAny}
import org.json4s.JsonAST.{JNothing, JObject, JString, JValue}

import scala.language.existentials

object AnyFormat {
  val anyWriter: (Printer, PBAny) => JValue = {
    case (printer, any) =>
      // Find the companion so it can be used to JSON-serialize the message. Perhaps this can be circumvented by
      // including the original GeneratedMessage with the Any (at least in memory).
      val cmp = printer.typeRegistry
        .findType(any.typeUrl)
        .getOrElse(
          throw new IllegalStateException(
            s"Unknown type ${any.typeUrl} in Any.  Add a TypeRegistry that supports this type to the Printer."
          )
        )

      // Unpack the message...
      val message = any.unpack(cmp)

      // ... and add the @type marker to the resulting JSON
      printer.toJson(message) match {
        case JObject(fields) =>
          JObject(("@type" -> JString(any.typeUrl)) +: fields)
        case value =>
          // Safety net, this shouldn't happen
          throw new IllegalStateException(
            s"Message of type ${any.typeUrl} emitted non-object JSON: $value"
          )
      }
  }

  val anyParser: (Parser, JValue) => PBAny = {
    case (parser, obj @ JObject(fields)) =>
      obj \ "@type" match {
        case JString(typeUrl) =>
          val cmp = parser.typeRegistry
            .findType(typeUrl)
            .getOrElse(
              throw new JsonFormatException(
                s"Unknown type ${typeUrl} in Any.  Add a TypeRegistry that supports this type to the Parser."
              )
            )
          val message = parser.fromJson(obj, true)(cmp)
          PBAny(typeUrl = typeUrl, value = message.toByteString)

        case JNothing =>
          throw new JsonFormatException(s"Missing type url when parsing $obj")

        case unknown =>
          throw new JsonFormatException(
            s"Expected string @type field, got $unknown"
          )
      }

    case (_, unknown) =>
      throw new JsonFormatException(s"Expected an object, got $unknown")
  }
} 
Example 21
Source File: FilterCluKeysFromDirectory.scala    From eidos   with Apache License 2.0 5 votes vote down vote up
package org.clulab.wm.eidos.apps.batch

import org.clulab.wm.eidos.utils.FileUtils
import org.clulab.wm.eidos.utils.meta.CluText
import org.json4s.JsonAST.JField
import org.json4s.JsonAST.JObject
import org.json4s.JsonAST.JString

import scala.collection.mutable

object FilterCluKeysFromDirectory extends App {
  val metaDir = args(0)
  val files = FileUtils.findFiles(metaDir, "json")
  val keys = mutable.Set.empty[String]

  files.foreach { file =>
    try {
      println(s"Extracting from ${file.getName}")
      val jValue = CluText.getJValue(file)

      val newKeys = for {
        JObject(mt) <- jValue
        JField("N", JString(key)) <- mt
      } yield
        key.toString

      keys ++= newKeys
      println(keys)
    }
    catch {
      case exception: Exception =>
        println(s"Exception for file $file")
        exception.printStackTrace()
    }
  }
  println(keys)
} 
Example 22
Source File: EncryptedKeyJsonCodec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.keystore

import java.util.UUID

import akka.util.ByteString
import io.iohk.ethereum.domain.Address
import io.iohk.ethereum.keystore.EncryptedKey._
import org.json4s.JsonAST.{JObject, JString, JValue}
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods._
import org.json4s.{CustomSerializer, DefaultFormats, Extraction, JField}
import org.spongycastle.util.encoders.Hex

import scala.util.Try

object EncryptedKeyJsonCodec {

  private val byteStringSerializer = new CustomSerializer[ByteString](_ => (
    { case JString(s) => ByteString(Hex.decode(s)) },
    { case bs: ByteString => JString(Hex.toHexString(bs.toArray)) }
  ))

  private implicit val formats = DefaultFormats + byteStringSerializer

  private def asHex(bs: ByteString): String =
    Hex.toHexString(bs.toArray)

  def toJson(encKey: EncryptedKey): String = {
    import encKey._
    import cryptoSpec._

    val json =
      ("id" -> id.toString) ~
      ("address" -> asHex(address.bytes)) ~
      ("version" -> version) ~
      ("crypto" -> (
        ("cipher" -> cipher) ~
        ("ciphertext" -> asHex(ciphertext)) ~
        ("cipherparams" -> ("iv" -> asHex(iv))) ~
        encodeKdf(kdfParams) ~
        ("mac" -> asHex(mac))
      ))

    pretty(render(json))
  }

  def fromJson(jsonStr: String): Either[String, EncryptedKey] = Try {
    val json = parse(jsonStr).transformField { case JField(k, v) => JField(k.toLowerCase, v) }

    val uuid = UUID.fromString((json \ "id").extract[String])
    val address = Address((json \ "address").extract[String])
    val version = (json \ "version").extract[Int]

    val crypto = json \ "crypto"
    val cipher = (crypto \ "cipher").extract[String]
    val ciphertext = (crypto \ "ciphertext").extract[ByteString]
    val iv = (crypto \ "cipherparams" \ "iv").extract[ByteString]
    val mac = (crypto \ "mac").extract[ByteString]

    val kdfParams = extractKdf(crypto)
    val cryptoSpec = CryptoSpec(cipher, ciphertext, iv, kdfParams, mac)
    EncryptedKey(uuid, address, cryptoSpec, version)

  }.fold(ex => Left(ex.toString), encKey => Right(encKey))

  private def encodeKdf(kdfParams: KdfParams): JObject =
    kdfParams match {
      case ScryptParams(salt, n, r, p, dklen) =>
        ("kdf" -> Scrypt) ~
        ("kdfparams" -> Extraction.decompose(kdfParams))

      case Pbkdf2Params(salt, prf, c, dklen) =>
        ("kdf" -> Pbkdf2) ~
        ("kdfparams" -> Extraction.decompose(kdfParams))
    }

  private def extractKdf(crypto: JValue): KdfParams = {
    val kdf = (crypto \ "kdf").extract[String]
    kdf.toLowerCase match {
      case Scrypt =>
        (crypto \ "kdfparams").extract[ScryptParams]

      case Pbkdf2 =>
        (crypto \ "kdfparams").extract[Pbkdf2Params]
    }
  }

} 
Example 23
Source File: OrderDomain.scala    From akka-serialization-test   with Apache License 2.0 5 votes vote down vote up
package com.github.dnvriend.domain

import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString

object OrderDomain {
  final val CD = ItemType.CD.toString
  final val DVD = ItemType.DVD.toString
  final val BluRay = ItemType.BluRay.toString
  final val Game = ItemType.Game.toString

  case object DirectDebitTypeSerializer extends CustomSerializer[ItemType](_ ⇒ ({
    case JString(CD)     ⇒ ItemType.CD
    case JString(DVD)    ⇒ ItemType.DVD
    case JString(BluRay) ⇒ ItemType.BluRay
    case JString(Game)   ⇒ ItemType.Game
  }, {
    case msg: ItemType ⇒ JString(msg.toString)
  }))

  type Title = String
  type Price = Double
  type ItemId = Option[String]

  type ZipCode = String
  type HouseNumber = Int

  type OrderName = String
  type Items = List[Item]
  type UnixTimestamp = Long
  type OrderId = Option[String]

  sealed trait ItemType
  object ItemType {
    case object CD extends ItemType
    case object DVD extends ItemType
    case object BluRay extends ItemType
    case object Game extends ItemType
  }

  final case class Item(itemType: ItemType, title: Title, price: Price, id: ItemId)
  final case class Address(zipcode: ZipCode, houseNumber: HouseNumber)
  final case class Order(name: OrderName, address: Address, items: Items, date: UnixTimestamp, id: OrderId)

  def withOrder(f: Order ⇒ Order = identity[Order])(g: Order ⇒ Unit): Unit = (g compose f)(Order(
    name = "",
    address = Address("", 1),
    items = List(Item(ItemType.BluRay, "", 25.0, Option("itemId"))),
    date = 1L,
    Some("orderId")
  ))
} 
Example 24
Source File: SparkKind.scala    From Linkis   with Apache License 2.0 5 votes vote down vote up
package com.webank.wedatasphere.linkis.engine.spark.common

import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString


object SparkKind {
  val SCALA_LAN = "scala"
  val PYTHON_LAN = "python"
  val PYTHON_END = "py"
  val R_LAN = "r"
  val SQL_LAN = "sql"
  val ML_LAN = "ml"

  val SPARK_TYPE = "spark"
  val SPARKSCALA_TYPE = "sparkscala"
  val PYSPARK_TYPE = "pyspark"
  val SPARKR_TYPE = "sparkr"
  val SPARKMIX_TYPE = "sparkmix"
  val MIX_TYPE = "mix"
  val SPARKSQL_TYPE = "sparksql"
  val SPARKMLSQL_TYPE = "mlsql"
  val FUNCTION_MDQ_TYPE = "function.mdq"

  def getCodeKind(code: String): Kind = {
    getKind(Kind.getKind(code))
  }

  def getSessionKind(code: String) = {
    val kindStr = Kind.getKindString(code)
    if(kindStr.indexOf("@") == 0) getKind(kindStr.substring(1)) else SparkMix()
  }

  private def getKind(kindStr: String) = {
    kindStr match {
      case SPARKSCALA_TYPE | SCALA_LAN => SparkScala()
      case PYSPARK_TYPE | PYTHON_LAN | PYTHON_END => PySpark()
      case SPARKR_TYPE | R_LAN => SparkR()
      case SPARKMIX_TYPE | MIX_TYPE => SparkMix()
      case SQL_LAN | SPARKSQL_TYPE => SparkSQL()
      case SPARKMLSQL_TYPE | ML_LAN => SparkMLSQL()
      case _ => throw new RuntimeException("Unknown code kind: " + kindStr)
    }
  }

}
import com.webank.wedatasphere.linkis.engine.spark.common.SparkKind._
case class SparkScala() extends Kind {
  override val toString = SPARKSCALA_TYPE
}
case class PySpark() extends Kind {
  override val toString = PYSPARK_TYPE
}

case class SparkR() extends Kind {
  override val toString = SPARKR_TYPE
}

case class SparkMix() extends Kind {
  override val toString: String = SPARKMIX_TYPE
}

case class SparkSQL() extends Kind {
  override val toString: String = SPARKSQL_TYPE
}

case class SparkMLSQL() extends Kind {
  override val toString = SPARKMLSQL_TYPE
}

case object SparkSessionKindSerializer extends CustomSerializer[Kind](implicit formats => ( {
  case JString(SPARKSCALA_TYPE) | JString(SCALA_LAN) => SparkScala()
  case JString(PYSPARK_TYPE) | JString(PYTHON_LAN) | JString(PYTHON_END) => PySpark()
  case JString(SPARKR_TYPE) | JString(R_LAN) => SparkR()
  case JString(SPARKMIX_TYPE) | JString(MIX_TYPE) => SparkMix()
  case JString(SQL_LAN) | JString(SPARKSQL_TYPE) => SparkSQL()
  case JString(SPARKMLSQL_TYPE) | JString(ML_LAN) => SparkMLSQL()
}, {
  case kind: Kind => JString(kind.toString)
})
) 
Example 25
Source File: CustomSerializerWithTypeHints.scala    From reliable-http-client   with Apache License 2.0 5 votes vote down vote up
package rhttpc.transport.json4s

import org.json4s.JsonAST.{JObject, JString}
import org.json4s._

import scala.reflect.ClassTag

class CustomSerializerWithTypeHints[T: Manifest, JV <: JValue: ClassTag](
  ser: Formats => (PartialFunction[JV, T], PartialFunction[T, JV]))
  extends CustomSubTypesSerializer[T, JObject](implicit formats => {
  val (deserialize, serialize) = ser(formats)
  (
    {
      case JObject(_ :: ("value", jValue: JV) :: Nil) if deserialize.isDefinedAt(jValue) =>
        deserialize(jValue)
    },
    {
      case obj: T if serialize.isDefinedAt(obj) =>
        JObject(
          formats.typeHintFieldName -> JString(obj.getClass.getName),
          "value" -> serialize(obj)
        )
    }
  )
}) 
Example 26
Source File: DynamicConfigurationUtils.scala    From maha   with Apache License 2.0 5 votes vote down vote up
// Copyright 2017, Yahoo Holdings Inc.
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms.
package com.yahoo.maha.service.config.dynamic

import java.util.regex.Pattern

import grizzled.slf4j.Logging
import org.json4s.JsonAST.JString
import org.json4s.{JField, JValue}

import scala.collection.mutable

object DynamicConfigurationUtils extends Logging {
  private val START = Pattern.quote("<%(")
  private val END = Pattern.quote(")%>")
  val DYNAMIC_CONFIG_PATTERN = Pattern.compile(s"$START(.*),(.*)$END")

  def extractDynamicFields(json: JValue): Map[String, (String, String)] = {
    val dynamicFieldMap = new mutable.HashMap[String, (String, String)]()
    val dynamicFields = getDynamicFields(json)
    dynamicFields.foreach(f => {
      require(f._2.isInstanceOf[JString], s"Cannot extract dynamic property from non-string field: $f")
      implicit val formats = org.json4s.DefaultFormats
      val matcher = DYNAMIC_CONFIG_PATTERN.matcher(f._2.extract[String])
      require(matcher.find(), s"Field does not contain dynamic property $f. Pattern - $DYNAMIC_CONFIG_PATTERN")
      require(matcher.groupCount() == 2, s"Expected name and default value in dynamic property field: $f")
      val propertyKey = matcher.group(1).trim
      val defaultValue = matcher.group(2).trim
      dynamicFieldMap.put(propertyKey, (f._1, defaultValue))
    })
    dynamicFieldMap.toMap
  }

  def getDynamicFields(json: JValue): List[JField] = {
    implicit val formats = org.json4s.DefaultFormats
     json.filterField(_._2 match {
      case JString(s) => {
        DYNAMIC_CONFIG_PATTERN.matcher(s).find()
      }
      case a => false
    })
  }
} 
Example 27
Source File: SendSlackMessage.scala    From hyperion   with Apache License 2.0 5 votes vote down vote up
package com.krux.hyperion.contrib.activity.notification

import java.net.{ HttpURLConnection, URL }

import org.json4s.JsonAST.{ JString, JObject }
import org.json4s.jackson.JsonMethods._
import scopt.OptionParser

object SendSlackMessage {
  case class Options(
    failOnError: Boolean = false,
    webhookUrl: String = "",
    user: Option[String] = None,
    message: Seq[String] = Seq.empty,
    iconEmoji: Option[String] = None,
    channel: Option[String] = None
  )

  def apply(options: Options): Boolean = try {
    // Setup the connection
    val connection = new URL(options.webhookUrl).openConnection().asInstanceOf[HttpURLConnection]
    connection.setDoOutput(true)
    connection.setRequestProperty("Content-Type", "application/json")
    connection.setRequestProperty("Accept", "application/json")

    // Write the message
    val output = connection.getOutputStream
    try {
      val message = Seq(
        "icon_emoji" -> options.iconEmoji,
        "channel" -> options.channel,
        "username" -> options.user,
        "text" -> Option(options.message.mkString("\n"))
      ).flatMap {
        case (k, None) => None
        case (k, Some(v)) => Option(k -> JString(v))
      }

      output.write(compact(render(JObject(message: _*))).getBytes)
    } finally {
      output.close()
    }

    // Check the response code
    connection.getResponseCode == 200 || !options.failOnError
  } catch {
    case e: Throwable =>
      System.err.println(e.toString)
      !options.failOnError
  }

  def main(args: Array[String]): Unit = {
    val parser = new OptionParser[Options](s"hyperion-notification-slack-activity") {
      override def showUsageOnError = true

      note("Sends a notification message to a Slack incoming webhook.")
      help("help").text("prints this usage text")
      opt[Unit]("fail-on-error").optional().action((_, c) => c.copy(failOnError = true))
        .text("Causes the activity to fail if any error received from the webhook")
      opt[String]("webhook-url").valueName("WEBHOOK").required().action((x, c) => c.copy(webhookUrl = x))
        .text("Sends the message to the given WEBHOOK url")
      opt[String]("user").valueName("NAME").optional().action((x, c) => c.copy(user = Option(x)))
        .text("Sends the message as the user with NAME")
      opt[String]("emoji").valueName("EMOJI").optional().action((x, c) => c.copy(iconEmoji = Option(x)))
        .text("Use EMOJI for the icon")
      opt[String]("to").valueName("CHANNEL or USERNAME").optional().action((x, c) => c.copy(channel = Option(x)))
        .text("Sends the message to #CHANNEL or @USERNAME")
      arg[String]("MESSAGE").required().unbounded().action((x, c) => c.copy(message = c.message :+ x))
        .text("Sends the given MESSAGE")
    }

    if (!parser.parse(args, Options()).exists(apply)) {
      System.exit(3)
    }
  }
} 
Example 28
Source File: SQLInterpreter.scala    From incubator-livy   with Apache License 2.0 5 votes vote down vote up
package org.apache.livy.repl

import java.lang.reflect.InvocationTargetException
import java.sql.Date

import scala.util.control.NonFatal

import org.apache.spark.SparkConf
import org.apache.spark.sql.Row
import org.apache.spark.sql.SparkSession
import org.json4s._
import org.json4s.JsonAST.{JNull, JString}
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._

import org.apache.livy.Logging
import org.apache.livy.rsc.RSCConf
import org.apache.livy.rsc.driver.SparkEntries


class SQLInterpreter(
    sparkConf: SparkConf,
    rscConf: RSCConf,
    sparkEntries: SparkEntries) extends Interpreter with Logging {

  case object DateSerializer extends CustomSerializer[Date](_ => ( {
    case JString(s) => Date.valueOf(s)
    case JNull => null
  }, {
    case d: Date => JString(d.toString)
  }))

  private implicit def formats: Formats = DefaultFormats + DateSerializer

  private var spark: SparkSession = null

  private val maxResult = rscConf.getInt(RSCConf.Entry.SQL_NUM_ROWS)

  override def kind: String = "sql"

  override def start(): Unit = {
    require(!sparkEntries.sc().sc.isStopped)
    spark = sparkEntries.sparkSession()
  }

  override protected[repl] def execute(code: String): Interpreter.ExecuteResponse = {
    try {
      val result = spark.sql(code)
      val schema = parse(result.schema.json)

      // Get the row data
      val rows = result.take(maxResult)
        .map {
          _.toSeq.map {
            // Convert java BigDecimal type to Scala BigDecimal, because current version of
            // Json4s doesn't support java BigDecimal as a primitive type (LIVY-455).
            case i: java.math.BigDecimal => BigDecimal(i)
            case e => e
          }
        }

      val jRows = Extraction.decompose(rows)

      Interpreter.ExecuteSuccess(
        APPLICATION_JSON -> (("schema" -> schema) ~ ("data" -> jRows)))
    } catch {
      case e: InvocationTargetException =>
        warn(s"Fail to execute query $code", e.getTargetException)
        val cause = e.getTargetException
        Interpreter.ExecuteError("Error", cause.getMessage, cause.getStackTrace.map(_.toString))

      case NonFatal(f) =>
        warn(s"Fail to execute query $code", f)
        Interpreter.ExecuteError("Error", f.getMessage, f.getStackTrace.map(_.toString))
    }
  }

  override def close(): Unit = { }
} 
Example 29
Source File: DataTypeUtils.scala    From incubator-livy   with Apache License 2.0 5 votes vote down vote up
package org.apache.livy.thriftserver.types

import org.json4s.{DefaultFormats, JValue}
import org.json4s.JsonAST.{JObject, JString}
import org.json4s.jackson.JsonMethods.parse


  def schemaFromSparkJson(sparkJson: String): Schema = {
    val schema = parse(sparkJson) \ "fields"
    val fields = schema.children.map { field =>
      val name = (field \ "name").extract[String]
      val hiveType = toFieldType(field \ "type")
      // TODO: retrieve comment from metadata
      Field(name, hiveType, "")
    }
    Schema(fields.toArray)
  }
} 
Example 30
Source File: GenderDetectStrategy.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.stages.impl.feature

import enumeratum.{Enum, EnumEntry}
import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString

import scala.util.Try
import scala.util.matching.Regex


sealed class GenderDetectStrategy extends EnumEntry
case object GenderDetectStrategy extends Enum[GenderDetectStrategy] {
  val values: Seq[GenderDetectStrategy] = findValues
  val delimiter = " WITH VALUE "
  val ByIndexString = "ByIndex"
  val ByLastString = "ByLast"
  val ByRegexString = "ByRegex"
  val FindHonorificString = "FindHonorific"

  case class ByIndex(index: Int) extends GenderDetectStrategy {
    override def toString: String = ByIndexString + delimiter + index.toString
  }
  case class ByLast() extends GenderDetectStrategy {
    override def toString: String = ByLastString
  }
  case class ByRegex(pattern: Regex) extends GenderDetectStrategy {
    override def toString: String = ByRegexString + delimiter + pattern.toString
  }
  case class FindHonorific() extends GenderDetectStrategy {
    override def toString: String = FindHonorificString
  }

  def fromString(s: String): GenderDetectStrategy = {
    Option(s).map(_.split(delimiter)) match {
      case Some(Array(ByIndexString, index)) if Try(index.toInt).isSuccess => ByIndex(index.toInt)
      case Some(Array(ByLastString)) => ByLast()
      case Some(Array(ByRegexString, regex)) if Try (regex.r).isSuccess => ByRegex(regex.r)
      case Some(Array(FindHonorificString)) => FindHonorific()
      case None => sys.error("Attempted to deserialize GenderDetectStrategy but found empty value")
      case _ => sys.error(s"Attempted to deserialize GenderDetectStrategy but no matching entry found for value '$s'")
    }
  }

  def json4s: CustomSerializer[GenderDetectStrategy] = new CustomSerializer[GenderDetectStrategy](_ =>
    (
      { case JString(s) => GenderDetectStrategy.fromString(s) },
      { case x: GenderDetectStrategy => JString(x.toString) }
    )
  )
} 
Example 31
Source File: JobFrequency.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.scheduler.config.job

import org.json4s.CustomSerializer
import org.json4s.JsonAST.{JNull, JString}

sealed trait JobFrequency
case object Daily extends JobFrequency
case object Hourly extends JobFrequency

case object JobFrequencySerializer extends CustomSerializer[JobFrequency](format => (
  {
    case JString(frequency) => frequency match {
      case "Daily" => Daily
      case "Hourly" => Hourly
    }
    case JNull => null
  },
  {
    case frequency: JobFrequency => JString(frequency.getClass.getSimpleName.replace("$", ""))
  }
)) 
Example 32
Source File: JobType.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.scheduler.config.job

import org.json4s.CustomSerializer
import org.json4s.JsonAST.{JNull, JString}

sealed trait JobType
case object Console extends JobType
case object Sql extends JobType

object JobTypeSerializer extends CustomSerializer[JobType](format => (
  {
    case JString(jobType) => jobType match {
      case "Console" => Console
      case "Sql" => Sql
    }
    case JNull => null
  },
  {
    case jobType: JobType => JString(jobType.getClass.getSimpleName.replace("$", ""))
  }
)) 
Example 33
Source File: JobFrequency.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.scheduler.config.job

import org.json4s.CustomSerializer
import org.json4s.JsonAST.{JNull, JString}

sealed trait JobFrequency
case object Daily extends JobFrequency
case object Hourly extends JobFrequency

case object JobFrequencySerializer extends CustomSerializer[JobFrequency](format => (
  {
    case JString(frequency) => frequency match {
      case "Daily" => Daily
      case "Hourly" => Hourly
    }
    case JNull => null
  },
  {
    case frequency: JobFrequency => JString(frequency.getClass.getSimpleName.replace("$", ""))
  }
)) 
Example 34
Source File: JobType.scala    From Scala-Design-Patterns-Second-Edition   with MIT License 5 votes vote down vote up
package com.ivan.nikolov.scheduler.config.job

import org.json4s.CustomSerializer
import org.json4s.JsonAST.{JNull, JString}

sealed trait JobType
case object Console extends JobType
case object Sql extends JobType

object JobTypeSerializer extends CustomSerializer[JobType](format => (
  {
    case JString(jobType) => jobType match {
      case "Console" => Console
      case "Sql" => Sql
    }
    case JNull => null
  },
  {
    case jobType: JobType => JString(jobType.getClass.getSimpleName.replace("$", ""))
  }
)) 
Example 35
Source File: CheckPoint.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.blockchain.electrum

import java.io.InputStream

import fr.acinq.bitcoin.{Block, ByteVector32, encodeCompact}
import fr.acinq.eclair.blockchain.electrum.db.HeaderDb
import org.json4s.JsonAST.{JArray, JInt, JString}
import org.json4s.jackson.JsonMethods


  def load(chainHash: ByteVector32, headerDb: HeaderDb): Vector[CheckPoint] = {
    val checkpoints = CheckPoint.load(chainHash)
    val checkpoints1 = headerDb.getTip match {
      case Some((height, header)) =>
        val newcheckpoints = for {h <- checkpoints.size * RETARGETING_PERIOD - 1 + RETARGETING_PERIOD to height - RETARGETING_PERIOD by RETARGETING_PERIOD} yield {
          // we * should * have these headers in our db
          val cpheader = headerDb.getHeader(h).get
          val nextDiff = headerDb.getHeader(h + 1).get.bits
          CheckPoint(cpheader.hash, nextDiff)
        }
        checkpoints ++ newcheckpoints
      case None => checkpoints
    }
    checkpoints1
  }
} 
Example 36
Source File: BasicBitcoinJsonRPCClient.scala    From eclair   with Apache License 2.0 5 votes vote down vote up
package fr.acinq.eclair.blockchain.bitcoind.rpc

import com.softwaremill.sttp._
import com.softwaremill.sttp.json4s._
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.eclair.KamonExt
import fr.acinq.eclair.blockchain.Monitoring.{Metrics, Tags}
import org.json4s.{CustomSerializer, DefaultFormats}
import org.json4s.JsonAST.{JString, JValue}
import org.json4s.jackson.Serialization

import scala.concurrent.{ExecutionContext, Future}

class BasicBitcoinJsonRPCClient(user: String, password: String, host: String = "127.0.0.1", port: Int = 8332, ssl: Boolean = false)(implicit http: SttpBackend[Future, Nothing]) extends BitcoinJsonRPCClient {

  // necessary to properly serialize ByteVector32 into String readable by bitcoind
  object ByteVector32Serializer extends CustomSerializer[ByteVector32](_ => ( {
    null
  }, {
    case x: ByteVector32 => JString(x.toHex)
  }))
  implicit val formats = DefaultFormats.withBigDecimal + ByteVector32Serializer
  private val scheme = if (ssl) "https" else "http"
  private val serviceUri = uri"$scheme://$host:$port/wallet/" // wallet/ specifies to use the default bitcoind wallet, named ""
  implicit val serialization = Serialization

  override def invoke(method: String, params: Any*)(implicit ec: ExecutionContext): Future[JValue] =
    invoke(Seq(JsonRPCRequest(method = method, params = params))).map(l => jsonResponse2Exception(l.head).result)

  def jsonResponse2Exception(jsonRPCResponse: JsonRPCResponse): JsonRPCResponse = jsonRPCResponse match {
    case JsonRPCResponse(_, Some(error), _) => throw JsonRPCError(error)
    case o => o
  }

  def invoke(requests: Seq[JsonRPCRequest])(implicit ec: ExecutionContext): Future[Seq[JsonRPCResponse]] = {
    requests.groupBy(_.method).foreach {
      case (method, calls) => Metrics.RpcBasicInvokeCount.withTag(Tags.Method, method).increment(calls.size)
    }
    KamonExt.timeFuture(Metrics.RpcBasicInvokeDuration.withoutTags()) {
      for {
        res <- sttp
          .post(serviceUri)
          .body(requests)
          .auth.basic(user, password)
          .response(asJson[Seq[JsonRPCResponse]])
          .send()
      } yield res.unsafeBody
    }
  }

} 
Example 37
Source File: TableType.scala    From hail   with MIT License 5 votes vote down vote up
package is.hail.types

import is.hail.expr.ir._
import is.hail.types.physical.{PStruct, PType}
import is.hail.types.virtual.{TStruct, Type}
import is.hail.rvd.RVDType
import is.hail.utils._
import org.json4s.CustomSerializer
import org.json4s.JsonAST.JString

class TableTypeSerializer extends CustomSerializer[TableType](format => (
  { case JString(s) => IRParser.parseTableType(s) },
  { case tt: TableType => JString(tt.toString) }))

object TableType {
  def keyType(ts: TStruct, key: IndexedSeq[String]): TStruct = ts.typeAfterSelect(key.map(ts.fieldIdx))
  def valueType(ts: TStruct, key: IndexedSeq[String]): TStruct = ts.filterSet(key.toSet, include = false)._1
}

case class TableType(rowType: TStruct, key: IndexedSeq[String], globalType: TStruct) extends BaseType {
  lazy val canonicalRowPType = PType.canonical(rowType).setRequired(true).asInstanceOf[PStruct]
  lazy val canonicalRVDType = RVDType(canonicalRowPType, key)

  key.foreach {k =>
    if (!rowType.hasField(k))
      throw new RuntimeException(s"key field $k not in row type: $rowType")
  }

  @transient lazy val globalEnv: Env[Type] = Env.empty[Type]
    .bind("global" -> globalType)

  @transient lazy val rowEnv: Env[Type] = Env.empty[Type]
    .bind("global" -> globalType)
    .bind("row" -> rowType)

  @transient lazy val refMap: Map[String, Type] = Map(
    "global" -> globalType,
    "row" -> rowType)

  def isCanonical: Boolean = rowType.isCanonical && globalType.isCanonical

  lazy val keyType: TStruct = TableType.keyType(rowType, key)
  def keyFieldIdx: Array[Int] = canonicalRVDType.kFieldIdx
  lazy val valueType: TStruct = TableType.valueType(rowType, key)
  def valueFieldIdx: Array[Int] = canonicalRVDType.valueFieldIdx

  def pretty(sb: StringBuilder, indent0: Int = 0, compact: Boolean = false) {
    var indent = indent0

    val space: String = if (compact) "" else " "

    def newline() {
      if (!compact) {
        sb += '\n'
        sb.append(" " * indent)
      }
    }

    sb.append(s"Table$space{")
    indent += 4
    newline()

    sb.append(s"global:$space")
    globalType.pretty(sb, indent, compact)
    sb += ','
    newline()

    sb.append(s"key:$space[")
    key.foreachBetween(k => sb.append(prettyIdentifier(k)))(sb.append(s",$space"))
    sb += ']'
    sb += ','
    newline()

    sb.append(s"row:$space")
    rowType.pretty(sb, indent, compact)

    indent -= 4
    newline()
    sb += '}'
  }
} 
Example 38
Source File: JsonSerializers.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.jsonrpc

import akka.util.ByteString
import io.iohk.ethereum.domain.Address
import org.json4s.JsonAST.{JNull, JString}
import org.json4s.CustomSerializer
import org.spongycastle.util.encoders.Hex

object JsonSerializers {

  object UnformattedDataJsonSerializer extends CustomSerializer[ByteString](_ =>
    (
      { PartialFunction.empty },
      { case bs: ByteString => JString(s"0x${Hex.toHexString(bs.toArray)}") }
    )
  )

  object QuantitiesSerializer extends CustomSerializer[BigInt](_ =>
    (
      {PartialFunction.empty},
      {
        case n: BigInt =>
          if(n == 0)
            JString("0x0")
          else
            JString(s"0x${Hex.toHexString(n.toByteArray).dropWhile(_ == '0')}")
      }
    )
  )

  object OptionNoneToJNullSerializer extends CustomSerializer[Option[_]](formats =>
    (
      {PartialFunction.empty},
      { case None => JNull }
    )
  )

  object AddressJsonSerializer extends CustomSerializer[Address](_ =>
    (
      { PartialFunction.empty },
      { case addr: Address => JString(s"0x${Hex.toHexString(addr.bytes.toArray)}")  }
    )
  )

} 
Example 39
Source File: ExecuteResponse.scala    From Linkis   with Apache License 2.0 4 votes vote down vote up
package com.webank.wedatasphere.linkis.engine

import com.webank.wedatasphere.linkis.engine.exception.QueryFailedException
import org.json4s.JValue
import org.json4s.JsonAST.JString


sealed abstract class ExecuteResponse()
case class ExecuteComplete(value: JValue) extends ExecuteResponse() {
  def this(output: String) = this(JString(output))
}
object ExecuteComplete {
  def apply(output: String) = new ExecuteComplete(output)
  //  def apply(value: JValue) = new ExecuteComplete(value)
}
case class ExecuteIncomplete(output: String) extends ExecuteResponse()
case class ExecuteError(t: Throwable) extends ExecuteResponse() {
  def this(errorMsg: String) = this(new QueryFailedException(60001,errorMsg))
}