spray.json.JsonParser Scala Examples

The following examples show how to use spray.json.JsonParser. 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: JsonSupport.scala    From darwin   with Apache License 2.0 5 votes vote down vote up
package it.agilelab.darwin.server.rest

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import org.apache.avro.Schema
import spray.json.{DefaultJsonProtocol, JsObject, JsString, JsValue, JsonParser, PrettyPrinter, RootJsonFormat}

trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
  implicit val printer: PrettyPrinter.type = PrettyPrinter

  implicit val schemaFormat: RootJsonFormat[Schema] = new RootJsonFormat[Schema] {

    override def write(obj: Schema): JsValue = JsonParser(obj.toString(true))

    override def read(json: JsValue): Schema = new Schema.Parser().parse(json.prettyPrint)
  }

  implicit val schemaWithIdFormat: RootJsonFormat[(Long, Schema)] = new RootJsonFormat[(Long, Schema)] {

    override def write(obj: (Long, Schema)): JsValue = JsObject(Map(
      "id" -> JsString(obj._1.toString),
      "schema" -> schemaFormat.write(obj._2)
    ))

    override def read(json: JsValue): (Long, Schema) = json match {
      case JsObject(fields) =>
        val id = fields.get("id") match {
          case Some(JsString(number)) => number
          case _ => throw new Exception("Id field should be a long")
        }

        val schema = fields.get("schema") match {
          case Some(x@JsObject(_)) => x
          case _ => throw new Exception("schema should be an object")
        }

        (id.toLong, schemaFormat.read(schema))
      case _ => throw new Exception("should be an object")
    }
  }
} 
Example 2
Source File: GitHubActionsAPIReading.scala    From jsoniter-scala   with MIT License 5 votes vote down vote up
package com.github.plokhotnyuk.jsoniter_scala.benchmark

import java.nio.charset.StandardCharsets.UTF_8

import com.avsystem.commons.serialization.json._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.AVSystemCodecs._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.BorerJsonEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.CirceEncodersDecoders._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.JacksonSerDesers._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.JsoniterScalaCodecs._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.SprayFormats._
import com.github.plokhotnyuk.jsoniter_scala.benchmark.WeePickleFromTos._
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.rallyhealth.weejson.v1.jackson.FromJson
import com.rallyhealth.weepickle.v1.WeePickle.ToScala
import io.circe.parser.decode
import org.openjdk.jmh.annotations.Benchmark
import spray.json.JsonParser

class GitHubActionsAPIReading extends GitHubActionsAPIBenchmark {
  @Benchmark
  def avSystemGenCodec(): GitHubActionsAPI.Response =
    JsonStringInput.read[GitHubActionsAPI.Response](new String(jsonBytes, UTF_8))

  @Benchmark
  def borer(): GitHubActionsAPI.Response = io.bullet.borer.Json.decode(jsonBytes).to[GitHubActionsAPI.Response].value

  @Benchmark
  def circe(): GitHubActionsAPI.Response =
    decode[GitHubActionsAPI.Response](new String(jsonBytes, UTF_8)).fold(throw _, identity)

  @Benchmark
  def jacksonScala(): GitHubActionsAPI.Response = jacksonMapper.readValue[GitHubActionsAPI.Response](jsonBytes)

  @Benchmark
  def jsoniterScala(): GitHubActionsAPI.Response = readFromArray[GitHubActionsAPI.Response](jsonBytes)

  @Benchmark
  def sprayJson(): GitHubActionsAPI.Response = JsonParser(jsonBytes).convertTo[GitHubActionsAPI.Response]

  @Benchmark
  def weePickle(): GitHubActionsAPI.Response = FromJson(jsonBytes).transform(ToScala[GitHubActionsAPI.Response])
} 
Example 3
Source File: LambdaTransport.scala    From shield   with MIT License 5 votes vote down vote up
package shield.transports

import com.amazonaws.services.lambda._
import spray.http.HttpHeaders.RawHeader
import spray.http.{HttpEntity, _}
import spray.json._
import spray.json.JsonParser
import com.amazonaws.handlers.AsyncHandler
import com.amazonaws.services.lambda.model._
import com.google.common.io.BaseEncoding
import spray.http.parser.HttpParser

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{Future, Promise}
import scala.util.Try

object LambdaTransport {
  type SendReceive = spray.client.pipelining.SendReceive
  val base64 = BaseEncoding.base64()
  class LambdaAsyncHandler(promise: Promise[InvokeResult]) extends AsyncHandler[InvokeRequest, InvokeResult] {
    def onError(exception: Exception) = promise.failure(exception)

    def onSuccess(request: InvokeRequest, result: InvokeResult) = promise.success(result)
  }

  def lambdaTransport(arn: String): SendReceive = {
    val client = AWSLambdaAsyncClientBuilder.defaultClient()
    def executeCall(request: HttpRequest): Future[HttpResponse] = {
      val req = new InvokeRequest()
        .withFunctionName(arn)
        .withPayload(LambdaRequest.translate(request).toJson.compactPrint)
      val p = Promise[InvokeResult]
      client.invokeAsync(req, new LambdaAsyncHandler(p))
      p.future.map(req => {
        JsonParser(ParserInput(req.getPayload.array)).convertTo[LambdaResponse].toResponse
      })
    }
    executeCall
  }

  case class LambdaRequest(method: String, headers: List[(String, String)], uri: String, body: Option[String])

  object LambdaRequest extends DefaultJsonProtocol {
    def translate(request: HttpRequest): LambdaRequest = {
      val processedHeaders = request.entity.toOption match {
        // No body - leave the content-type header (probably a HEAD request)
        case None => request.headers
        // Some body - set the content-type based off what spray associated with the entity
        // * content-type has probably been filtered by HttpProxyLogic before reaching here, so this likely redundant
        // * Adding-content-type from the entity matches Spray's HttpRequest rendering logic
        case Some(entity) => HttpHeaders.`Content-Type`(entity.contentType) :: request.headers.filterNot(_.lowercaseName == "content-type")
      }
      val headers =  processedHeaders.map(header => (header.lowercaseName, header.value))
      val body = request.entity.toOption.map(e => base64.encode(e.data.toByteArray))
      LambdaRequest(request.method.value, headers, request.uri.toString, body)
    }
    implicit val requestFormat : JsonFormat[LambdaRequest] = jsonFormat4(LambdaRequest.apply)
  }

  case class LambdaResponse(status: Int, headers: List[(String, String)], body: Option[String]) {
    def parseHeaders : List[HttpHeader] = HttpParser.parseHeaders(headers.map(RawHeader.tupled))._2
    def toResponse: HttpResponse = {
      val parsedContentType = headers.find(_._1.toLowerCase() == "content-type").map(ct => HttpParser.parse(HttpParser.ContentTypeHeaderValue, ct._2))
      val entity = body.map(base64.decode)

      (parsedContentType, entity) match {
        case (None, None) => HttpResponse(status, headers = parseHeaders)
        case (None, Some(data)) => HttpResponse(status, HttpEntity(ContentTypes.`application/octet-stream`, data), parseHeaders)
        case (Some(Left(err)), _) => HttpResponse(StatusCodes.BadGateway, HttpEntity(s"Upstream supplied an invalid content-type header: ${err.detail}"))
        case (Some(Right(contentType)), None) => HttpResponse(status, headers = parseHeaders)
        case (Some(Right(contentType)), Some(data)) => HttpResponse(status, HttpEntity(contentType, data), parseHeaders)
      }
    }
  }

  object LambdaResponse extends DefaultJsonProtocol {
    implicit val responseFormat : JsonFormat[LambdaResponse] = jsonFormat3(LambdaResponse.apply)
  }
} 
Example 4
Source File: RestServiceActor.scala    From fraud   with Apache License 2.0 5 votes vote down vote up
package fraud.main

import akka.actor.{ Actor, ActorRef }
import com.datastax.driver.core._
import fraud.main.RandomTransaction._
import spray.http.MediaTypes.{ `application/json`, `text/html` }
import spray.httpx.SprayJsonSupport.{ sprayJsonMarshaller, sprayJsonUnmarshaller }
import spray.json.JsonParser
import spray.routing._
import scala.collection.JavaConversions._
import scala.xml.Elem


trait RestService extends HttpService {
  import TransactionJsonProtocol._
  def communicate(t: Transaction)
  def indexHtml(): Elem
  def cleanHtml(): Elem 
  def fraudHtml(): Elem 

  val route =
    path("") {
      get {
        respondWithMediaType(`text/html`) {
          complete {
            indexHtml()
          }
        }
      }
    } ~ path("transaction") {
      post {
        entity(as[Transaction]) { transaction =>
          complete {
            communicate(transaction)
            transaction
          }
        }
      }
    } ~ path("transactions") {
      get {
        respondWithMediaType(`application/json`) {
          complete { randomTransactions(10) }
        }
      }
    } ~ path("fraud") {
      get {
        respondWithMediaType(`text/html`) {
          complete {
            fraudHtml()
          }
        }
      }
    } ~ path("clean") {
      get {
        respondWithMediaType(`text/html`) {
          complete {
            cleanHtml()
          }
        }
      }
    }
} 
Example 5
Source File: AsyncInterface.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.master.interfaces.async

import io.hydrosphere.mist.master.JobDetails.Source
import io.hydrosphere.mist.master.MainService
import io.hydrosphere.mist.master.interfaces.JsonCodecs._
import io.hydrosphere.mist.master.models._
import io.hydrosphere.mist.utils.Logger
import spray.json.{DeserializationException, JsValue, JsonParser}
import spray.json.enrichString

import scala.util.{Failure, Success, Try}

class AsyncInterface(
  mainService: MainService,
  input: AsyncInput,
  val name: String
) extends Logger {

  private val source = Source.Async(name)

  def start(): this.type  = {
    input.start(process)
    this
  }

  private def process(message: String): Unit = {
    def asFunctionRequest(js: JsValue) = js.convertTo[AsyncFunctionStartRequest]
    def asDevRequest(js: JsValue) = js.convertTo[DevJobStartRequestModel]

    try {
      val js = message.parseJson

      Try[Any](asDevRequest(js)).orElse(Try(asFunctionRequest(js))) match {
        case Success(req: AsyncFunctionStartRequest) =>
          mainService.runJob(req.toCommon, source)
        case Success(req: DevJobStartRequestModel) =>
          mainService.devRun(req.toCommon, source)
        case Success(x) => throw new IllegalArgumentException("Unknown request type")
        case Failure(e) => throw e
      }
    } catch {
      case e: DeserializationException =>
        logger.warn(s"Message $message does not conform with start request")
      case e: JsonParser.ParsingException =>
        logger.warn(s"Handled invalid message $message")
      case e: Throwable =>
        logger.error(s"Error occurred during handling message $message", e)
    }
  }


  def close(): Unit = input.close()

} 
Example 6
Source File: DataFileIngress.scala    From cloudflow   with Apache License 2.0 5 votes vote down vote up
package cloudflow.akkastreamsdoc

import java.nio.file
import java.nio.file._

import akka.NotUsed
import akka.stream.IOResult
import akka.stream.alpakka.file.scaladsl.Directory
import akka.stream.scaladsl._
import akka.util.ByteString
import cloudflow.akkastream._
import cloudflow.akkastream.scaladsl._
import cloudflow.streamlets._
import cloudflow.streamlets.avro._
import spray.json.JsonParser

import scala.concurrent.Future
import scala.concurrent.duration._

class DataFileIngress extends AkkaStreamlet {

  import JsonSupport._

  val out   = AvroOutlet[Data]("out").withPartitioner(RoundRobinPartitioner)
  def shape = StreamletShape.withOutlets(out)

  private val sourceData = VolumeMount("source-data-mount", "/mnt/data", ReadWriteMany)

  override def volumeMounts = Vector(sourceData)

  // Streamlet processing steps
  // 1. Every X seconds
  // 2. Enumerate all files in the mounted path
  // 3. Read each file *)
  // 4. Deserialize file content to a Data value *)

  // *) Note that reading and deserializing the file content is done in separate steps for readability only, in production they should be merged into one step for performance reasons.

  override def createLogic = new RunnableGraphStreamletLogic() {
    val listFiles: NotUsed ⇒ Source[file.Path, NotUsed] = { _ ⇒
      Directory.ls(getMountedPath(sourceData))
    }
    val readFile: Path ⇒ Source[ByteString, Future[IOResult]] = { path: Path ⇒
      FileIO.fromPath(path).via(JsonFraming.objectScanner(Int.MaxValue))
    }
    val parseFile: ByteString ⇒ Data = { jsonByteString ⇒
      JsonParser(jsonByteString.utf8String).convertTo[Data]
    }

    val emitFromFilesContinuously = Source
      .tick(1.second, 5.second, NotUsed)
      .flatMapConcat(listFiles)
      .flatMapConcat(readFile)
      .map(parseFile)
    def runnableGraph = emitFromFilesContinuously.to(plainSink(out))
  }
} 
Example 7
Source File: IssueServiceImpl.scala    From BacklogMigration-Redmine   with MIT License 5 votes vote down vote up
package com.nulabinc.backlog.r2b.redmine.service

import javax.inject.Inject

import com.nulabinc.backlog.migration.common.utils.Logging
import com.nulabinc.backlog.r2b.redmine.conf.RedmineApiConfiguration
import com.nulabinc.backlog.r2b.redmine.domain.RedmineProjectId
import com.taskadapter.redmineapi.bean.Issue
import com.taskadapter.redmineapi.{Include, RedmineManager}
import spray.json.{JsNumber, JsonParser}

import scala.jdk.CollectionConverters._


class IssueServiceImpl @Inject()(apiConfig: RedmineApiConfiguration, projectId: RedmineProjectId, redmine: RedmineManager)
    extends IssueService
    with Logging {

  override def countIssues(): Int = {
    val url    = s"${apiConfig.url}/issues.json?limit=1&subproject_id=!*&project_id=${projectId.value}&key=${apiConfig.key}&status_id=*"
    val string = scala.io.Source.fromURL(url, "UTF-8").mkString
    JsonParser(string).asJsObject.getFields("total_count") match {
      case Seq(JsNumber(totalCount)) => totalCount.intValue
      case _                         => 0
    }
  }

  override def allIssues(params: Map[String, String]): Seq[Issue] =
    redmine.getIssueManager.getIssues(params.asJava).asScala.toSeq

  override def issueOfId(id: Integer, include: Include*): Issue = {
    logger.debug("Get an issue ID: " + id)
    redmine.getIssueManager.getIssueById(id, include: _*)
  }

  override def tryIssueOfId(id: Integer, include: Include*): Either[Throwable, Issue] =
    try {
      Right(redmine.getIssueManager.getIssueById(id, include: _*))
    } catch {
      case e: Throwable =>
        logger.warn(e.getMessage, e)
        Left(e)
    }

} 
Example 8
Source File: UserMappingFile.scala    From BacklogMigration-Redmine   with MIT License 5 votes vote down vote up
package com.nulabinc.backlog.r2b.mapping.file

import better.files.File
import com.nulabinc.backlog.migration.common.conf.{BacklogApiConfiguration, BacklogConfiguration}
import com.nulabinc.backlog.migration.common.domain.BacklogUser
import com.nulabinc.backlog.migration.common.modules.{ServiceInjector => BacklogInjector}
import com.nulabinc.backlog.migration.common.service.{UserService => BacklogUserService}
import com.nulabinc.backlog.migration.common.utils.{IOUtil, StringUtil}
import com.nulabinc.backlog.r2b.mapping.core.MappingDirectory
import com.nulabinc.backlog.r2b.mapping.domain.MappingJsonProtocol._
import com.nulabinc.backlog.r2b.mapping.domain.{Mapping, MappingsWrapper}
import com.nulabinc.backlog.r2b.redmine.conf.RedmineApiConfiguration
import com.nulabinc.backlog.r2b.redmine.modules.{ServiceInjector => RedmineInjector}
import com.nulabinc.backlog.r2b.redmine.service.{UserService => RedmineUserService}
import com.osinka.i18n.Messages
import com.taskadapter.redmineapi.bean.{User => RedmineUser}
import spray.json.JsonParser


class UserMappingFile(redmineApiConfig: RedmineApiConfiguration, backlogApiConfig: BacklogApiConfiguration, users: Seq[RedmineUser])
    extends MappingFile
    with BacklogConfiguration {

  private[this] val redmineItems = getRedmineItems()
  private[this] val backlogItems = getBacklogItems()

  private[this] def getRedmineItems(): Seq[MappingItem] = {
    val injector    = RedmineInjector.createInjector(redmineApiConfig)
    val userService = injector.getInstance(classOf[RedmineUserService])

    def resolve(user: RedmineUser): Option[RedmineUser] = {
      (Option(user.getLogin), Option(user.getFullName)) match {
        case (Some(_), Some(_)) => Some(user)
        case _                  => userService.optUserOfId(user.getId)
      }
    }

    def condition(user: RedmineUser): Boolean = {
      StringUtil.notEmpty(user.getLogin).nonEmpty
    }

    def createItem(user: RedmineUser): MappingItem = {
      MappingItem(user.getLogin, user.getFullName)
    }

    val redmineUsers = users.flatMap(resolve).filter(condition)
    redmineUsers.map(createItem)
  }

  private[this] def getBacklogItems(): Seq[MappingItem] = {
    def createItem(user: BacklogUser): MappingItem = {
      if (backlogApiConfig.url.contains(NaiSpaceDomain)) {
        MappingItem(user.optMailAddress.getOrElse(""), user.name)
      } else {
        MappingItem(user.optUserId.getOrElse(""), user.name)
      }
    }
    val backlogUsers = allUsers()
    backlogUsers.map(createItem)
  }

  private[this] def allUsers(): Seq[BacklogUser] = {
    val injector    = BacklogInjector.createInjector(backlogApiConfig)
    val userService = injector.getInstance(classOf[BacklogUserService])
    userService.allUsers()
  }

  private[this] def convertForNAI(backlogUsers: Seq[BacklogUser])(mapping: Mapping) = {
    if (backlogApiConfig.url.contains(NaiSpaceDomain)) {
      val targetBacklogUser = backlogUsers
        .find(backlogUser => backlogUser.optMailAddress.getOrElse("") == mapping.backlog)
        .getOrElse(throw new NoSuchElementException(s"User ${mapping.backlog} not found"))
      mapping.copy(backlog = targetBacklogUser.optUserId.getOrElse(s"UserId ${mapping.backlog} not found"))
    } else mapping
  }

  override def tryUnmarshal(): Seq[Mapping] = {
    val path    = File(filePath).path.toAbsolutePath
    val json    = IOUtil.input(path).getOrElse("")
    val convert = convertForNAI(allUsers()) _
    JsonParser(json).convertTo[MappingsWrapper].mappings.map(convert)
  }

  override def matchItem(redmine: MappingItem): String =
    backlogs.map(_.name).find(_ == redmine.name).getOrElse("")

  override def redmines: Seq[MappingItem] = redmineItems

  override def backlogs: Seq[MappingItem] = backlogItems

  override def filePath: String = MappingDirectory.USER_MAPPING_FILE

  override def itemName: String = Messages("common.users")

  override def description: String = {
    Messages("cli.mapping.configurable", itemName, backlogs.map(_.name).mkString(","))
  }

  override def isDisplayDetail: Boolean = true

} 
Example 9
Source File: SensorDataFileIngress.scala    From pipelines-examples   with Apache License 2.0 5 votes vote down vote up
package pipelines.examples.sensordata

import java.nio.file
import java.nio.file._

import akka.NotUsed
import akka.stream.IOResult
import akka.stream.alpakka.file.scaladsl.Directory
import akka.stream.scaladsl._
import akka.util.ByteString
import pipelines.akkastream._
import pipelines.akkastream.scaladsl._
import pipelines.streamlets._
import pipelines.streamlets.avro._
import spray.json.JsonParser

import scala.concurrent.Future
import scala.concurrent.duration._

class SensorDataFileIngress extends AkkaStreamlet {

  import SensorDataJsonSupport._

  val out = AvroOutlet[SensorData]("out").withPartitioner(RoundRobinPartitioner)
  def shape = StreamletShape.withOutlets(out)

  private val sourceData = VolumeMount("source-data-mount", "/mnt/data", ReadWriteMany)

  override def volumeMounts = Vector(sourceData)

  // Streamlet processing steps
  // 1. Every X seconds
  // 2. Enumerate all files in the mounted path
  // 3. Read each file *)
  // 4. Deserialize file content to a SensorData value *)

  // *) Note that reading and deserializing the file content is done in separate steps for readability only, in production they should be merged into one step for performance reasons.

  override def createLogic = new RunnableGraphStreamletLogic() {
    val listFiles: NotUsed ⇒ Source[file.Path, NotUsed] = { _ ⇒ Directory.ls(getMountedPath(sourceData)) }
    val readFile: Path ⇒ Source[ByteString, Future[IOResult]] = { path: Path ⇒ FileIO.fromPath(path).via(JsonFraming.objectScanner(Int.MaxValue)) }
    val parseFile: ByteString ⇒ SensorData = { jsonByteString ⇒ JsonParser(jsonByteString.utf8String).convertTo[SensorData] }

    val emitFromFilesContinuously = Source.tick(1.second, 5.second, NotUsed)
      .flatMapConcat(listFiles)
      .flatMapConcat(readFile)
      .map(parseFile)
    def runnableGraph = emitFromFilesContinuously.to(plainSink(out))
  }
}