标签: circe

`circe`类型级Json =>一个函数?

使用circe或者argonaut,我怎么能写一个Json => A(注释 - Json可能不是类型的名称)ASSN类给出的位置:

  // A USA Social Security Number has exactly 8 digits.
  case class SSN(value: Sized[List[Nat], _8])
Run Code Online (Sandbox Code Playgroud)

伪代码:

// assuming this function is named f

f(JsArray(JsNumber(1)))A因为它的大小是1而不会成为一个

f(JsArray(JsNumber(1), ..., JsNumber(8))) === SSN(SizedList(1,...,8))

scala argonaut circe

4
推荐指数
1
解决办法
328
查看次数

如何将JSON空值解码为空集合

假设我有一个这样的Scala案例类:

case class Stuff(id: String, values: List[String])
Run Code Online (Sandbox Code Playgroud)

而且我希望能够将以下JSON值解码为:

{ "id": "foo", "values": ["a", "b", "c"] }
{ "id": "bar", "values": [] }
{ "id": "qux", "values": null }
Run Code Online (Sandbox Code Playgroud)

Circe解码器中,您从前两种情况的通用推导工作获得,但在第三种情况下却没有:

scala> decode[Stuff]("""{ "id": "foo", "values": ["a", "b", "c"] }""")
res0: Either[io.circe.Error,Stuff] = Right(Stuff(foo,List(a, b, c)))

scala> decode[Stuff]("""{ "id": "foo", "values": [] }""")
res1: Either[io.circe.Error,Stuff] = Right(Stuff(foo,List()))

scala> decode[Stuff]("""{ "id": "foo", "values": null }""")
res2: Either[io.circe.Error,Stuff] = Left(DecodingFailure(C[A], List(DownField(values))))
Run Code Online (Sandbox Code Playgroud)

我如何使我的解码器在这种情况下工作,最好不必处理完全手写定义的样板。

json scala circe

4
推荐指数
2
解决办法
208
查看次数

使用Circe展平嵌套的JSON对象

假设我有一个像这样的JSON对象:

{
   "foo": true,
   "bar": {
      "baz": 1,
      "qux": {
        "msg": "hello world",
        "wow": [null]
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我想将其递归展平到单个层,并用下划线合并键:

{
   "foo": true,
   "bar_baz": 1,
   "baz_qux_msg": "hello world",
   "baz_qux_wow": [null]
}
Run Code Online (Sandbox Code Playgroud)

如何使用Circe做到这一点?

(注意:这是Circe Gitter频道的另一个常见问题解答。)

json scala circe

4
推荐指数
1
解决办法
123
查看次数

合并几个json数组

假设我们有2个json数组.如何将它们合并为一个带有circe的单个数组?例:

数组1:

[{"id": 1}, {"id": 2}, {"id": 3}]
Run Code Online (Sandbox Code Playgroud)

数组2:

[{"id": 4}, {"id": 5}, {"id": 6}]
Run Code Online (Sandbox Code Playgroud)

需要:

[{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}, {"id": 6}]
Run Code Online (Sandbox Code Playgroud)

我试过了deepMerge,但它只保留参数的内容,而不是调用对象的内容.

json scala circe

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

为任意JSON创建一个"解码器"

我正在使用Finch,Circe和Sangria为API构建GraphQL端点.将variables通过在GraphQL查询来基本上是任意的JSON对象(假设没有嵌套).例如,在我的测试代码中String,这是两个例子:

val variables = List(
  "{\n  \"foo\": 123\n}",
  "{\n  \"foo\": \"bar\"\n}"
)
Run Code Online (Sandbox Code Playgroud)

Sangria API期望这些类型Map[String, Any].

我已经尝试了很多方法但到目前为止Decoder还没能在Circe 写一个.任何帮助赞赏.

scala finch graphql sangria circe

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

避免使用 Circe json 序列化器序列化为 null

如何避免使用 Circe json 序列化器将 None 序列化为 null?我无法强制该库跳过“无”的序列化字段。有可能实现吗?

json scala circe

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

如何通过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
查看次数

使用circe递归将JSON树转换为其他格式(XML,CSV等)

为了使用circe 将JSON节点转换为JSON 以外的其他格式(例如XML,CSV等),我想出了一个解决方案,其中我必须访问circe的内部数据结构。

这是我的工作示例,该示例将JSON转换为XML字符串(虽然不完美,但您可以理解):

package io.circe

import io.circe.Json.{JArray, JBoolean, JNull, JNumber, JObject, JString}
import io.circe.parser.parse

object Sample extends App {

  def transformToXMLString(js: Json): String = js match {
    case JNull => ""
    case JBoolean(b) => b.toString
    case JNumber(n) => n.toString
    case JString(s) => s.toString
    case JArray(a) => a.map(transformToXMLString(_)).mkString("")
    case JObject(o) => o.toMap.map {
      case (k, v) => s"<${k}>${transformToXMLString(v)}</${k}>"
    }.mkString("")
  }

  val json =
    """{
      | "root": {
      |  "sampleboolean": true,
      |  "sampleobj": {
      |    "anInt": 1,
      |    "aString": …
Run Code Online (Sandbox Code Playgroud)

xml json scala scala-cats circe

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

How do I get circe to have an either/or output scenario for the generated Json?

Here's what I intend - let's say I have a field called medical_payments - it can "either" be a limit if one elects or waived

{
  "medical_payments": 
    {
      "limit_value":"one_hundred"
    }
} 
Run Code Online (Sandbox Code Playgroud)

如果被选为豁免,则应为:

{
  "medical_payments": 
    {
      "waived":true
    }
} 
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我所拥有的:

sealed trait LimitOrWaiver
case class Limit(limit_key: String) extends LimitOrWaiver
case class Waived(waived: Boolean) extends LimitOrWaiver

case class Selection(medical_payments: LimitOrWaiver)
Run Code Online (Sandbox Code Playgroud)

样本数据:

Selection(medical_payments = Limit("one_hundred")).asJson
Run Code Online (Sandbox Code Playgroud)

输出:

{
  "medical_payments": 
    {
      "Limit": { "limit_value":"one_hundred" } // additional object added
    }
} 
Run Code Online (Sandbox Code Playgroud)

同样为Json添加了Selection(medical_payments = Waived(true)).asJson …

scala circe

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

How to remove null values in list of objects using Circe

I'm trying to encode a list of objects using Circe, something that looks similar to:

val test = Seq(MyObject("hello", None, 1, 2, None)

I'm trying to parse this using Circe:

test.asJson

But this creates the JSON object:

[
  {
    name: "hello",
    someVal: null,
    someNum: 1,
    anotherNum: 2,
    anotherVal: null
  }
]
Run Code Online (Sandbox Code Playgroud)

I've tried running asJson with .dropNullValues, but that doesn't seem to access the null values inside of the object. Is there a way to drop the null values …

scala circe

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

标签 统计

circe ×10

scala ×10

json ×6

argonaut ×1

finch ×1

graphql ×1

sangria ×1

scala-cats ×1

xml ×1