我正在尝试编码一个case类(其中一些属性也是case类),并且我将嵌套的case类名作为JSON中的键名。是否有一种简单的方法可以避免这种情况,而无需创建自定义编码器?嵌套类继承自密封特征。
我目前正在使用半自动推导。
以下工作表示例显示了我的问题:
case class A(foo: Int, bar: Sub)
sealed trait Sub
case class B(x: Int, y: Int) extends Sub
case class C(x: Int, y: Int, z: Int) extends Sub
import io.circe._, io.circe.generic.semiauto._
import io.circe.syntax._
implicit val bEncoder: Encoder[Sub] = deriveEncoder
implicit val aEncoder: Encoder[A] = deriveEncoder
A(123, B(8, 8)).asJson
A(456, C(8, 8, 8)).asJson
Run Code Online (Sandbox Code Playgroud)
而不是:
res0: io.circe.Json = {
"foo" : 123,
"bar" : {
"x" : 8,
"y" : 8
}
}
res1: io.circe.Json = {
"foo" : 456, …Run Code Online (Sandbox Code Playgroud) 当字段可以具有不同的原始值类型时,我在解析json时遇到问题。例如,我可以获取json:
{
"name" : "john",
"age" : 31
}
Run Code Online (Sandbox Code Playgroud)
也可以采用以下形式:
{
"name" : "john",
"age" : "thirty one"
}
Run Code Online (Sandbox Code Playgroud)
或以这种方式:
{
"name" : "john",
"age" : 31.0
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将字段解析age为以下ADT实例:
sealed trait PrimitiveWrapper
case class IntWrapper(v: Int) extends PrimitiveWrapper
case class StringWrapper(v: String) extends PrimitiveWrapper
case class FloatWrapper(v: Float) extends PrimitiveWrapper
Run Code Online (Sandbox Code Playgroud)
所以最后我可以得到这样的东西:
case class Person(name: String, age: PrimitiveWrapper)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我找到了这个主题:如何在不区分对象的情况下使用circe解码ADT
但是在这种解决方案中,我们不解析原始字段。
我正在尝试为两个案例类生成编码器和解码器:
object EventBusCases {
case class ValuationRequest(function: RequestValue = ALL_DAY_VALS, interval: RequestValue = IntraDayIntervals.MIN_5)
implicit val requestDecoder: Decoder[ValuationRequest] = deriveDecoder[ValuationRequest]
implicit val requestEncoder: Encoder[ValuationRequest] = deriveEncoder[ValuationRequest]
case class ValuationResponse(values: List[Valuation], function: RequestValue)
implicit val responseDecoder: Decoder[ValuationResponse] = deriveDecoder[ValuationResponse]
implicit val responseEncoder: Encoder[ValuationResponse] = deriveEncoder[ValuationResponse]
}
Run Code Online (Sandbox Code Playgroud)
我不断收到这样的错误,但对于这两种情况:
could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder[eventbus.eventBusCases.ValuationResponse]
我决定还尝试为这些类中的自定义类派生编码器和解码器,例如“Valuation”,但我在这些类上遇到了相同的错误。
我正在使用Circe 0.12.3这些Scala 2.12.8是我的 Circe 相关的 Scala 依赖项:
"com.beachape" %% "enumeratum" % "1.5.14",
"com.beachape" %% "enumeratum-circe" % "1.5.22",
"io.circe" %% "circe-core" …Run Code Online (Sandbox Code Playgroud) 我使用 Scala circe 库将案例类的对象转换为 JSON,并根据 JSON 表示Message创建对象。Message该类的实现如下:
import io.circe
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.parser
import io.circe.syntax._
object Message {
implicit val measurementDecoder = deriveDecoder[Message]
implicit val measurementEncoder = deriveEncoder[Message]
def fromJson(jsonString: String): Either[circe.Error, Message] =
parser.decode[Message](jsonString)
}
case class Message(id: Int, text: String) {
def toJson() = (this).asJson.noSpaces
def toJson2() = this.asJson.noSpaces // syntax error: No implicit arguments of type: Encoder[Message.this.type]
}
Run Code Online (Sandbox Code Playgroud)
我的观点是方法的实现toJson。虽然这个变体有效
import io.circe
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.parser
import io.circe.syntax._
object Message …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一些类编码为 json 字符串,但是无论我尝试什么,我的类似乎都无法为我正在使用的案例类找到隐式编码器。
这是我能精简到的最小的例子。
import io.circe._
import io.circe.generic.semiauto._
import io.circe.generic.auto._
import io.circe.syntax._
case class OneCol(value: String)
object testObject {
def main(args: Array[String]): Unit = {
val testVal = OneCol("someVal")
println(testVal.asJson)
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下编译错误
错误:(30, 21) 找不到参数编码器的隐式值:io.circe.Encoder[OneCol] println(testVal.asJson)
我在半自动编码器创建中尝试过同样的事情
def main(args: Array[String]): Unit = {
implicit val enc : Encoder[OneCol] = deriveEncoder
val testVal = OneCol("someVal")
println(testVal.asJson)
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下错误
错误:(25, 42) 找不到 io.circe.generic.encoding.DerivedObjectEncoder[A] 隐式 val enc 类型的惰性隐式值:Encoder[OneCol] =deriveEncoder
错误:(25, 42) 方法deriveEncoder 没有足够的参数:(隐式编码:shapeless.Lazy[io.circe.generic.encoding.DerivedObjectEncoder[A]])io.circe.ObjectEncoder[A]。未指定值参数编码。隐式 val enc : 编码器 [OneCol] = 派生编码器
我相当确定自动和半自动编码器生成的全部目的是处理这样的情况,所以我对我做错了什么感到有点茫然。
如果版本信息相关,我使用 …
已经为此工作了几天,但仍然不知道发生了什么:得到了 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) 我想做的是将字段描述符定义为json 的field1.field2[1].field3访问值two:
{
"field1": {
"field2": [
{
"field3": "one"
},
{
"field3": "two"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用applyDynamicand做到这一点root.field1.field2.index(1).field3,但是有没有办法使用字符串来创建这样的镜头?
我有一个json复杂的结构。像这样的东西:
{
"a":"aa",
"b":"bb",
"c":[
"aaa",
"bbb"
],
"d":{
"e":"ee",
"f":"ff"
}
}
Run Code Online (Sandbox Code Playgroud)
我想大写所有字符串值。文档说:
root.each.string.modify(_.toUpperCase)
Run Code Online (Sandbox Code Playgroud)
但正如预期的那样,只更新了根值。
如何circe-optics递归遍历所有字符串值?
JSON结构事先未知。
这是关于 Scastie的例子。
通过评论:我希望所有字符串值都大写,而不仅仅是根值:
{
"a":"AA",
"b":"BB",
"c":[
"AAA",
"BBB"
],
"d":{
"e":"EE",
"f":"FF"
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个 ZIO 模块的示例,它有两个实现:
我的通用界面如下所示:
trait Service[R] {
def load[T <: Component](ref: CompRef): RIO[R, T]
}
Run Code Online (Sandbox Code Playgroud)
现在我的 YAML 实现看起来像:
def loadYaml[T <: Component: Decoder](ref: CompRef): RIO[Any, T] = {...}
Run Code Online (Sandbox Code Playgroud)
这Decoder是特定于实现的。
现在的问题是如何从 Service 实现委托给loadYaml.
我尝试了以下方法:
val components: Components.Service[Any] = new Components.Service[Any] {
implicit val decodeComponent: Decoder[Component] =
List[Decoder[Component]](
Decoder[DbConnection].widen,
...
).reduceLeft(_ or _)
def load[T <: Component](ref: CompRef): RIO[Any, T] = loadYaml[T] (ref)
}
Run Code Online (Sandbox Code Playgroud)
这给了我:
Error:(62, 20) could …Run Code Online (Sandbox Code Playgroud) Is there a common approach to handle PATCH requests in REST API using circe library? By default, circe does not allow decoding partial JSON with only a part of the fields specified, i.e. it requires all fields to be set. You could use a withDefaults config, but it will be impossible to know if the field you received is null or just not specified. Here is a simplified sample of the possible solution. It uses Left[Unit] as a value to …