小编Lau*_*eng的帖子

如何通过Circe修改json的值类型

我需要捕获 json 的 int 值并通过某个映射表将其更改为字符串值。我使用 circe,我知道如何在不更改值类型的情况下修改值,例如:

package com.accenture.soh.driver

import io.circe._, io.circe.parser._
import cats.syntax.either._
import io.circe._, io.circe.parser._
import io.circe.optics.JsonPath._

/**
  * Created by laurence.geng on 2017/2/2.
  */
object CirceTest extends App {

  val json: Json = parse(
    """
  [
  {
    "metric":"my-metric",
    "dps":{"1484214238":5,"1484214239":1,"1484214240":4,"1484214241":11}
  }
  ]
  """).getOrElse(Json.Null)

  val doubleValue: Json => Json = root.each.dps.each.int.modify(_ * 2)

  println(doubleValue(json).toString())

}
Run Code Online (Sandbox Code Playgroud)

输出是:

[
  {
    "metric" : "my-metric",
    "dps" : {
      "1484214238" : 10,
      "1484214239" : 2,
      "1484214240" : 8,
      "1484214241" : 22
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

但是,实际上,我需要做的是将 …

json scala circe

3
推荐指数
1
解决办法
2145
查看次数

在Akka-Http DSL中遇到了什么?

我刚刚接触过akka,在学习akka-http时,我被Rest API DSL所吸引,这里是一段代码:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.io.StdIn

object WebServer {
   def main(args: Array[String]) {
      implicit val system = ActorSystem("my-system")
      implicit val materializer = ActorMaterializer()
      // needed for the future flatMap/onComplete in the end
      implicit val executionContext = system.dispatcher
      val route =
         path("hello") {
            get {
               complete("Say hello to akka-http")
            }
         }
      val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
      println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
      StdIn.readLine() // let it run until user presses …
Run Code Online (Sandbox Code Playgroud)

dsl scala implicit-conversion akka-http

2
推荐指数
1
解决办法
387
查看次数

标签 统计

scala ×2

akka-http ×1

circe ×1

dsl ×1

implicit-conversion ×1

json ×1