问题标签 [argonaut]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
175 浏览

json - Json Argonaut 对于案例类来说太大了

有一个我无法影响的 API,它有一个 JSON 结果对象,其成员有 23 个字段。案例类的范式不起作用,因为有 22 个限制。我见过 Slick 和其他库使用 HLists 来解决这个问题。有没有办法在 Argonaut 中做到这一点?如果是这样,请给我一段示例代码以供利用。谢谢!

0 投票
1 回答
2652 浏览

scala - 将类型与数据构造函数相关联的 ADT 编码有什么问题?(例如斯卡拉。)

在 Scala 中,代数数据类型被编码为sealed一级类型层次结构。例子:

使用case classes 和case objects,Scala 生成了一堆东西,比如equals, hashCode, unapply(用于模式匹配)等,它们为我们带来了传统 ADT 的许多关键属性和特性。

不过有一个关键区别——在 Scala 中,“数据构造函数”有自己的类型。例如比较以下两个(从相应的 REPL 复制)。


我一直认为 Scala 变体是有利的。

毕竟,没有类型信息的丢失AppendIf[Int]例如 是 的子类型Positioning[Int]

实际上,您获得了关于 value 的额外编译时不变量。(我们可以称之为依赖类型的受限版本吗?)

这可以很好地利用——一旦您知道使用什么数据构造函数来创建一个值,相应的类型就可以通过流程的其余部分传播,以增加更多的类型安全性。例如,使用这种 Scala 编码的 Play JSON 将只允许您从 中提取fieldsJsObject而不是从任意任意JsValue.

在 Haskell 中,fields可能会有 type JsValue -> Set (String, JsValue)。这意味着它将在运行时失败JsArray等。这个问题也以众所周知的部分记录访问器的形式表现出来。

Scala 对数据构造函数的处理是错误的观点已经被多次表达过——在 Twitter、邮件列表、IRC、SO 等上。不幸的是,除了一对之外,我没有任何链接——Travis Brown 的这个回答,和Argonaut,一个用于 Scala 的纯函数式 JSON 库。

Argonaut有意识地采用 Haskell 方法(通过private案例类,并手动提供数据构造函数)。您可以看到我提到的有关 Haskell 编码的问题也存在于 Argonaut 中。(除了它Option用来表示偏心。)

我一直在思考这个问题,但仍然不明白是什么让 Scala 的编码错误。当然,它有时会妨碍类型推断,但这似乎不是一个足够有力的理由来判定它是错误的。我错过了什么?

0 投票
1 回答
196 浏览

json - 使用 argonaut 生成 JSON 时如何反转字段顺序?

使用argonaut,我可以生成一些 JSON:

但它会以相反的顺序生成 json:

其实我希望它是:

有没有办法做到这一点?我不想把它写成:

我想找到一种方法以相同的顺序编写它们

0 投票
1 回答
339 浏览

json - 如何使用 argonaut 将 json 值“aaa,bbb”转换为“Seq[String]”?

我正在使用 argonaut 将 json 解析为案例类。

杰森:

斯卡拉:

代码无法编译,错误是:

如何修复它,以便我可以解析a@aaa.com, b@bbb.comSeq("a@aaa.com", "b@bbb.com")?

0 投票
0 回答
145 浏览

json - 使用 argonaut 解析 JSON 时,是否有任何更简单的方法可以为未提供或空白字符串字段提供默认值?

我正在使用 Argonaut 解析 JSON 字符串。有一个要求:如果任何字段未提供,或者为空或空白,则将其改为字符串“未提供”。

我有一个解决方案,但似乎很复杂:

你可以看到BlankAsNonSuppliedDecodeJson一个非常复杂,很难理解。有没有办法让它(或整个例子)更简单?

0 投票
1 回答
723 浏览

json - 如何使用 argonaut 解析可选的自定义字段?

我定义了一个具有“视频”信息的用户:

我们有这样一个json:

我可以使用这样的代码来解析它:

从 中DecodeVideo,您可以看到我只想在同时提供“标题”和“url”的情况下提供视频。

如果 json 包含“视频”部分,它运行良好。但如果没有,argonaut 将报告未提供“视频”部分。

如何使“视频”可选?

0 投票
1 回答
243 浏览

scala - argonaut - separating failures and successes when extracting List[A]

I have an instance of DecodeJson[A] for a particular type, A, and I have an instance of Json, call it j. I'd like to do something like j.as[List[A]]. However, this JSON is coming to me from a 3rd party, and if some of the items in the array are missing fields, I'd like to just log errors for those items and collect only the items that can be properly parsed (instead of failing the entire decoding).

It seems to me like what I want to end up with is something like (List[(String, CursorHistory)], List[A]). That is, any successfully decoded elements will end up in the list on the right, and errors for any items that could not be successfully parsed will be accumulated on the left. This looks a lot like the MonadPlus.separate method in scalaz.

I can think of a couple ways of getting there. One way is to do something like:

This should give me a DecodeResult[List[(String, CursorHistory)], List[A]] as desired. However, that will throw away cursor information about where in the JSON array the items that failed were located. I could use something like zipWithIndex, but that starts to get pretty messy.

To me what seems like a better solution is to do something like this:

This will give me the same output type, but the CursorHistory should be populated to include the information about stepping into the elements of the array.

Here is an example:

This seems to behave reasonably (ignoring some terrible List concatenation that is probably happening). However, it seems like this sort of thing would be a fairly common use-case, so I'm wondering if there is a simpler or built-in way to do something like this. If not, I might submit a PR to argonaut for the DecodeJson[DecodeJson[A]] instance and see if anyone is interested.

0 投票
1 回答
244 浏览

scala - Argonaut casecodec3?

我正在尝试 Argonaut 快速入门中的这个简单示例:

我在casecodec3. 我克隆了 argonaut 和 scalaz 存储库并 grep 了源代码,我只在示例代码中看到了这一点。该符号来自哪里,为什么我不能使用它?

0 投票
2 回答
1060 浏览

arrays - Scala Argonaut Json 数组/对象

我需要使用 scala 和 argonaut 创建这个 json 对象:

使用 scala 我创建了这个对象:

但是生成的json是这样的:

请问可以帮忙吗?

0 投票
1 回答
1083 浏览

json - 使用 Scala 和 Argonaut 提取特定的 JSON 字段

我正在尝试使用 Scala 和 Argonaut 解析 json。

假设我从另一端 REST 服务获得 JSON 响应,并且我不知道该响应中 json 字段的顺序和字段数。例如,http://headers.jsontest.com/返回带有五个字段的 JSON,但我只想提取一个字段并删除其他字段:

所以我写了一些代码:

但是当然这段代码不起作用,可能是因为来自 jsontest 的答案不能与Address案例类进行比较。

我收到此错误消息:

我怎样才能只获得其名称指定的一个字段?