如何使用 Dispatch 解析 JSON 请求?

发布于 2024-11-11 03:49:05 字数 426 浏览 6 评论 0原文

我正在学习 Scala,并试图了解特征是如何工作的(特别是与 Dispatch 库一起使用)。

我有这样的事情:

import dispatch._
import dispatch.liftjson._

object Foo
{
    def main(cmd: Array[String])
    {
        val http = new Http;
        val req = :/("example.com") / path ># (list ! obj);
        val res = http(req);
    }
}

不幸的是,它抱怨 ># 没有在dispatch.Request 中注册。该特征在dispatch.liftjson中进行了描述,我的假设是我只需要导入该特征(_应该覆盖)即可注册。

I'm learning Scala, and attempting to understand how traits are working (specifically with the Dispatch library).

I've got something like this:

import dispatch._
import dispatch.liftjson._

object Foo
{
    def main(cmd: Array[String])
    {
        val http = new Http;
        val req = :/("example.com") / path ># (list ! obj);
        val res = http(req);
    }
}

Unfortunately, it's complaining that ># is not registered with dispatch.Request. The trait is described within dispatch.liftjson, and it was my assumption that I should just need to import that trait (which _ should cover) for it to register.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苹果你个爱泡泡 2024-11-18 03:49:05

您应该从 dispatch.liftjson.Js._ 导入。

拥有某种特质并没有什么帮助,因为你并没有使用它。 JS._ 导入会将 JS 对象的所有内容引入您的范围,包括 隐式转换 requestToJsonVerbs,它来自 trait ImplicitJsonVerbs。此方法将您从 :/("example.com") / path 获得的标准 Dispatch Request 转换为 JsonVerbs,其中有方法>#

下面是我如何查询 API 的简短示例:

import dispatch._
import dispatch.liftjson.Js._

import net.liftweb.common.{Box, Failure, Full}
import net.liftweb.util.Helpers

case class Device(device_token: String, alias: Option[String])

val req = devicesReq / device_token as (app_token, secret)
Helpers.tryo(http(req ># (json => {
  json.extract[Device]
})))

如您所见,我有正确的导入(加上一些 Lift< /a> 我喜欢的库),然后我的 Request '有'一个 ># 方法。我给 ># 一个与预期签名 ((JValue) ⇒ T) 匹配的函数,然后我们就可以开始了。

如果您想知道,我专门使用 lift-json 提取案例类的功能,这意味着 T 将是 Device。但是,如果 lift-json 无法将 JValue 转换为 Device,它也会引发异常,因此我用 Helper.tryo 包装了整个请求,一个 Lift 辅助方法,它包装了一个 try-catch 调用,返回一个 BoxBox 类似于标准 Scala Option,但添加了 Failure,它指示为什么 Box 为空。因此,在这种情况下,我将得到 Full[Device]Failure。便利!

You should be importing from dispatch.liftjson.Js._.

Having a trait isn't helpful, as you're not then using it. The JS._ import will bring all the contents of the JS object into your scope, including the implicit conversion requestToJsonVerbs which it has from trait ImplicitJsonVerbs. This method converts a standard Dispatch Request, which you have from :/("example.com") / path, to a JsonVerbs, which has the method >#.

Here's an abridged sample of how I query an API:

import dispatch._
import dispatch.liftjson.Js._

import net.liftweb.common.{Box, Failure, Full}
import net.liftweb.util.Helpers

case class Device(device_token: String, alias: Option[String])

val req = devicesReq / device_token as (app_token, secret)
Helpers.tryo(http(req ># (json => {
  json.extract[Device]
})))

As you can see, I have the correct imports (plus some for some Lift libraries I like), and my Request then 'has' a ># method. I give ># a function that matches the expected signature ((JValue) ⇒ T) and away we go.

In case you're wondering, I'm specifically using lift-json's ability to extract to case classes, meaning that T will be Device. However, lift-json also throws an exception if it is unable to convert the JValue to a Device, so I've wrapped my whole request with Helper.tryo, a Lift helper method that wraps a try-catch call, returning a Box. Box is like the standard Scala Option but with the addition of Failure, which indicates why a Box is empty. So, in this case I will get either a Full[Device] or a Failure. Handy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文