标签: circe

不相容的杰克逊版本:2.7.1在sbt?

从sbt运行TwitterServer时出现此错误:

SEVERE: LoadService: failed to 
instantiate 'com.twitter.finagle.stats.MetricsExporter' 
for the requested service 'com.twitter.finagle.http.HttpMuxHandler'
com.fasterxml.jackson.databind.JsonMappingException: 
Incompatible Jackson version: 2.7.1
Run Code Online (Sandbox Code Playgroud)

2.7杰克逊的依赖从其他地方(circe)传递.我以为我可以用这种方式将它覆盖到2.6.7,但似乎没有效果:

val jacksonV = "2.6.7"
val `jackson-core` = "com.fasterxml.jackson.core" % "jackson-core" % jacksonV
val `jackson-databind` = "com.fasterxml.jackson.core" % "jackson-databind" % jacksonV
val `jackson-annotations` = "com.fasterxml.jackson.core" % "jackson-annotations" % jacksonV
val `jackson-datatype-jsr310` = "com.fasterxml.jackson.datatype" % "jackson-datatype-jsr310" % jacksonV
dependencyOverrides += `jackson-core`
dependencyOverrides += `jackson-databind`
dependencyOverrides += `jackson-annotations`
dependencyOverrides += `jackson-datatype-jsr310`
Run Code Online (Sandbox Code Playgroud)

知道为什么这不会覆盖任何东西吗?

scala jackson sbt finagle circe

5
推荐指数
1
解决办法
2528
查看次数

带有Http4的Circe编码器和解码器

我正在尝试使用http4s,circe和http4s-circe.

下面我试图使用circe的自动派生功能.

import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s.Status.ResponseClass.Successful
import io.circe.syntax._
import org.http4s._
import org.http4s.headers._
import org.http4s.circe._
import scalaz.concurrent.Task
import io.circe._

final case class Login(username: String, password: String)
final case class Token(token: String)

object JsonHelpers {
   import io.circe.generic.auto._
   implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
   implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
   implicit val tokenEntityEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
   implicit val tokenEntityDecoder : EntityDecoder[Token] = jsonOf[Token]
}

object Http4sTest2 extends App {
   import JsonHelpers._
   val url = "http://"
   val uri = Uri.fromString(url).valueOr(throw _)
   val …
Run Code Online (Sandbox Code Playgroud)

scala circe http4s http4s-circe

5
推荐指数
1
解决办法
2427
查看次数

如何使用Circe解码Scala中的JSON列表/数组

我有代码片段

cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]
Run Code Online (Sandbox Code Playgroud)

Clip是一个简单的字符串和数字类.传入的Json应该包含一个json对象"播放列表",其中包含一个"项目"数组,其中每个项目都是一个剪辑.所以json看起来应该是这样的

{
  "playlist": {
      "name": "Sample Playlist",
      "items": [
        {
          "clipId":"xyz", 
          "name":"abc"
        },
        {
          "clipId":"pqr", 
          "name":"def"
        } 
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码片段,我收到编译错误:

 Error:(147, 81) could not find implicit value for parameter d:     
 io.circe.Decoder[List[com.packagename.model.Clip]]
      cursor.downField("params").downField("playlist").downField("items").as[List[Clip]]
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如何使用circe为简单项目的列表/数组设置解码?

json scala circe

5
推荐指数
1
解决办法
8626
查看次数

使用Circe for Scala编码为json时忽略无字段

我使用scala 2.11.8与circe 0.7.0

我使用scalajs与API区分null发送的JSON中不存在的字段和字段.

我正在寻找一种编码为JSON的方法,case class包含Option[T]我将设置None为表示缺失值的字段的scala :

case class Foo(
optionalFieldOne: Option[Int] = 42,
optionalFieldTwo: Option[Int] = null,
optionalFieldThree: Option[Int] = None
)

implicit FooEncoder: Encoder[Foo] = deriveEncoder[Foo]
val test = Foo()
//What the code actually produces
test.asJson.noSpace
//>{"optionalFieldOne": 42,"optionalFieldTwo":null, "optionalFieldThree":null}

//What I would like
test.asJson.noSpace
//>{"optionalFieldOne": 42,"optionalFieldTwo":null}
Run Code Online (Sandbox Code Playgroud)

circe是否提供了任何可能的配置?你知道如何访问它,我已经查看了所有发行说明,github问题和他们的网站文档没有成功.

在这种配置选项不可用的情况下,有人会如何正确实现它?

json scala circe

5
推荐指数
1
解决办法
2408
查看次数

找不到 io.finch.Decode.Json 类型的证据参数的隐式值

已经为此工作了几天,但仍然不知道发生了什么:得到了 finch Web 服务,build.sbt 依赖项如下所示:

  "com.github.finagle" %% "finch-circe" % finchVersion changing(),
  "com.github.finagle" %% "finch-core" % finchVersion changing(),
  "com.github.finagle" %% "finch-jackson" % finchVersion changing(),
  "com.github.finagle" %% "finch-test" % finchVersion changing(),
  "com.twitter" %% "twitter-server" % "1.28.0",
Run Code Online (Sandbox Code Playgroud)

Finch 版本是 0.14.0。端点看起来像:

  def makeService(statsReceiver: StatsReceiver): Service[Request, Response] = {
    //val getUserCounter = statsReceiver.counter("get_user_counter")
    (
      MyEndpoint.endpoint1()
        :+: SomeEndpoint.deleteEntity()
        :+: SomeEndpoint.createEntity()
        :+: SomeEndpoint.updateEntity()
      ) handle {
      case e: InvalidClientError => Unauthorized(e)
      case e: InvalidContextError => BadRequest(e)
      case e: RelevanceError => BadRequest(e)
      case e: Exception => InternalServerError(e)
    } …
Run Code Online (Sandbox Code Playgroud)

finch circe

5
推荐指数
0
解决办法
2545
查看次数

在 Scala 中使用 Akka Http 和 Circe 解码 JSON

我正在尝试创建 Akka Http REST post 端点,将 JSON 对象映射到定义的案例类

import io.circe.Decoder, io.circe.generic.auto._

case class JobEntity(id: Option[Long] = None, name: String, description: String, json_data :java.sql.blob) 
Run Code Online (Sandbox Code Playgroud)

JSON 的类型为

{
 "id": "124",
 "name": "MYJOB",
 "description": "Test Job"
}
Run Code Online (Sandbox Code Playgroud)

现在我想将整个 JSON 映射到“json_data”,作为案例类中定义的 blob

  post {
  entity(as[JobEntity]) { jobEntity: JobEntity =>
    complete(createJob(jobEntity).map(_.asJson))
  }
}
Run Code Online (Sandbox Code Playgroud)

我知道 .map(_.asJson) 会将 json 映射到 JobEntity,如果不是这样,请纠正
我如何将整个 JSON 映射到 json_data。

json scala akka-http circe

5
推荐指数
1
解决办法
5752
查看次数

具有案例类的抽象类的 scala circe 编码器/解码器

我想将 FieldMapping 类的集合保存为 json 字符串 -

abstract class Field {
  def clazz: Class[_]

  def name: String
}

case class StringField(name: String) extends Field {
  override def clazz: Class[_] = classOf[String]
}

case class DateField(name: String) extends Field {
  override def clazz: Class[_] = classOf[Date]
}
Run Code Online (Sandbox Code Playgroud)

...等 - 完整代码在这里: https://github.com/alexeyOnGitHub/scala-typesafe/blob/master/src/main/scala/com/example/model/Field.scala

瑟茜代码:

import com.example.model.{DateField, Field, FieldMapping, StringField}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}

object CirceBoilerplateForConfigs {
  implicit val fieldDecoder: Decoder[StringField] = deriveDecoder[StringField]
  implicit val fieldEncoder: Encoder[StringField] = deriveEncoder[StringField]

  implicit val dateDecoder: Decoder[DateField] …
Run Code Online (Sandbox Code Playgroud)

scala circe

5
推荐指数
1
解决办法
2989
查看次数

Circe Scala - 编码和解码 Map[] 和案例类

我正在尝试为我拥有的案例类创建编码器和解码器:

case class Road(id: String, light: RoadLight, names: Map[String, String])
Run Code Online (Sandbox Code Playgroud)

RoadLight 是一个 java 类,带有枚举。

public enum RoadLight {
red,yellow,green
}
Run Code Online (Sandbox Code Playgroud)

我尝试过进行半自动编码和解码:制作隐式编码器和解码器。
我从 Map[String,String] 类型开始:

implicit val namesDecoder: Decoder[Map[String, String]] = deriveDecoder[Map[String, String]]
implicit val namesEncoder: Encoder[Map[String, String]] = deriveEncoder[Map[String, String]]
Run Code Online (Sandbox Code Playgroud)

但我确实得到了他们两个的错误!

1: 找不到 io.circe.generic.decoding.DerivedDecoder[A] 类型的 Lazy 隐式值

2:错误:方法deriveDecoder没有足够的参数:(隐式解码:shapeless.Lazy[io.circe.generic.decoding.DerivedDecoder[A]])io.circe.Decoder[A]。未指定值参数解码。隐式 val 名称解码器:解码器[Map[String,String]]=deriveDecoder

我已经按照书本做了一切,无法理解出了什么问题。我什至没有尝试解析案例类,只是解析地图,甚至这也不起作用。

有任何想法吗?谢谢!

scala circe

5
推荐指数
2
解决办法
7530
查看次数

即使 Json.toString 返回正确的值,Json.asString 也返回 None

给定以下案例类别LogMessage

import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import enumeratum.{CirceEnum, Enum, EnumEntry}
import io.circe.syntax._

sealed trait LogLevel extends EnumEntry

object LogLevel extends Enum[LogLevel] with CirceEnum[LogLevel] {
  val values = findValues

  case object Warning extends LogLevel
  case object Error   extends LogLevel
  case object Info    extends LogLevel
  case object Success extends LogLevel
}

object LogMessage {
  implicit val logMessageDecoder: Decoder[LogMessage] = deriveDecoder[LogMessage]
  implicit val logMessageEncoder: Encoder[LogMessage] = deriveEncoder[LogMessage]
}

case class LogMessage(level: LogLevel, text: String, args: List[String], date: Long)
Run Code Online (Sandbox Code Playgroud)
case class …
Run Code Online (Sandbox Code Playgroud)

json scala circe

5
推荐指数
1
解决办法
858
查看次数

Http4s Client Encode Entity as x-www-form-urlencoded Recursively

I have a request like the following

val request =
    Request[IO](
      method = POST,
      uri = Uri.uri("..."),
      headers = Headers(
        Authorization(BasicCredentials("...", "..."))
      )
    )
    .withEntity(PaymentIntentRequest2(2000, "usd"))
Run Code Online (Sandbox Code Playgroud)

我正在查看源代码,看起来它withEntity继承了嵌套的标头EntityDecoder,因此上面的代码默认为Content-Type: application/json. 好像我明确地传递UrlForm一切都很好。

不幸的是,我所点击的 API 预期数据为,x-www-form-urlencoded并且考虑到目标 API 与所有不同端点/请求的复杂性,我希望找到一种将给定案例类编码为表单的方法。最好的方法是什么?

我努力了:

  1. 显式指定Content-Type但这不起作用,因为继承类型优先

  2. Product构建从到 的隐式泛型转换UrlForm(目前是扩展方法)

implicit class UrlFormEncode[+B <: Product](val u: B) {
    def asUrlForm: UrlForm =
      u.productElementNames
        .zip(u.productIterator)
        .foldLeft(UrlForm()) { (a, b) =>
          a.combine(UrlForm(b._1 -> b._2.toString))
        }
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是 …

scala circe http4s http4s-circe

5
推荐指数
1
解决办法
1211
查看次数

标签 统计

circe ×10

scala ×9

json ×4

http4s ×2

http4s-circe ×2

akka-http ×1

finagle ×1

finch ×1

jackson ×1

sbt ×1