这是什么“和"?在ScalaJsonCombinator中(在定义Writes时)? [英] What is this "and" in ScalaJsonCombinator (when defining a Writes)?

查看:115
本文介绍了这是什么“和"?在ScalaJsonCombinator中(在定义Writes时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在几种基本/标准情况下使用了这个json组合器,但并没有真正了解它的工作方式.一切都很好.

I've been using this json combinator for several basic / standard cases without really understanding how it works. All was fine.

现在,我想为可能发生的任何高级案件做好准备;我需要了解代码.

Now I want to get myself prepared for whatever advanced cases might come; I need to understand the code.

参考: https://www.playframework.com/documentation/2.3. x/ScalaJsonCombinators

我想我能读懂读物:

implicit val locationReads: Reads[Location] = (
  (JsPath \ "lat").read[Double] and
  (JsPath \ "long").read[Double]
)(Location.apply _)

它会创建一个Reads,

It creates a Reads that:

  1. 首先-当被赋予JsValue时(通过其"reads"方法)-它先拉"lat",再拉"long".从这两个中创建一个元组(Double,Double). - https: //www.playframework.com/documentation/2.3.x/api/scala/index.html#play.api.libs.json.Reads

然后将该元组分配给该Reads的部分函数,​​在这种情况下,这是"Location.apply _"返回的值.我在repl中尝试过:

That tuple is then assigned to the partial function of that Reads..., which in this case is whatever returned by "Location.apply _". I tried it in repl:

...

scala> val yowMan = Location.apply _
yowMan: (Double, Double) => Location = <function2>

scala> yowMan(1, 2)
res14: Location = Location(1.0,2.0)

该部分函数将(Double,Double)的元组作为输入.所以...,步骤1的结果被引导至步骤2,我们得到了Location的一个实例,作为"reads"的返回.

That partial function takes the a tuple of (Double, Double) as input. So..., the outcome of step 1 is channeled to step 2, and we get an instance of Location as the return of "reads".

现在开始写作:

implicit val locationWrites: Writes[Location] = (
  (JsPath \ "lat").write[Double] and
  (JsPath \ "long").write[Double]
)(unlift(Location.unapply))

首先取消应用".我尝试了repl:

First the "unapply". I tried in repl:

scala> val heyDude = Location.unapply
<console>:16: error: missing arguments for method unapply in object Location;
follow this method with `_' if you want to treat it as a partially applied function
   val heyDude = Location.unapply

糟糕,好的,我按照指示进行了操作

Oops, ok, I followed the instruction:

scala> val heyDude = Location.unapply _
heyDude: Location => Option[(Double, Double)] = <function1>

好,所以我们得到了一个局部函数,它将Location的实例转换为(Double,Double)的(可选)元组.

Ok, so we get a partial function that transforms an instance of Location to an (optional) tuple of (Double, Double).

接下来,提起":

scala> val hohoho = unlift(heyDude)
hohoho: Location => (Double, Double) = <function1>

scala> val loc = Location(1, 2)
loc: Location = Location(1.0,2.0)

scala> hohoho(loc)
res16: (Double, Double) = (1.0,2.0)

好吧,所以...松开只是扔掉了选项",直接把我们带到元组.

Ok, so... unlift simply throws away the "Option", and takes us directly to the tuple.

好吧...所以...我想...这是Writes的写操作" * *),当给定Location实例时,它将:

Ok... so... I guess... this "writes" of the Writes... *) when given an instance of Location, it will:

  1. 通过unlift(Location.unapply)产生的部分函数传递该对象.

  1. Pass that object through that partial function produced by unlift(Location.unapply).

然后将该部分函数返回的元组(Double,Double)引导到由此产生的任何值:

The tuple (Double, Double) returned by that partial function is then channeled to whatever is produced by this:

(JsPath \"lat").write [Double]和 (JsPath \"long").write [Double]

(JsPath \ "lat").write[Double] and (JsPath \ "long").write[Double]

什么"到底是什么?遵循JsPath的API文档,我认为它是OWrites:

What exactly is that "whatever"? Following the API doc of JsPath, I think it is OWrites: https://www.playframework.com/documentation/2.3.x/api/scala/index.html#play.api.libs.json.OWrites

但是...我看不到OWrites中有一个名为"and"的方法.这个和"在哪里声明?它是做什么的?是:"oWrites1和oWrites2"产生"oWrites3"吗?而这个"oWrites3"是一种特殊的OWrite,它以元组作为输入? ...如果是这样的话...元组在case类("lat"和"long")中没有有关属性名称的信息.怎么知道生成的json字符串应为{"lat":1,1,"long":2}然后?

But... I can't see there's a method named "and" in OWrites. Where is this "and" declared? And what does it do? Is it: "oWrites1 and oWrites2" produces "oWrites3"? And this "oWrites3" is a special type of OWrites that takes tuple as input? ... If that's the case... the tuple doesn't have information about the name of the property in the case class ("lat" and "long"). How does it know that the produced json string should be {"lat": 1, "long": 2} then?

很抱歉,我们提出了一系列问题.请帮助我对此有一个清晰的了解.谢谢!

Sorry for the train of questions. Please help me obtaining a clear understanding of this. Thanks!

*) https://www.playframework.com/documentation/2.3.x/api/scala/index.html#play.api.libs.json.Writes

更新:

  • 添加相关问题:Scala/的语法和含义/玩!代码示例

推荐答案

如有疑问,请对其进行反编译.这表明存在一个隐式toFunctionalBuilderOps,然后该隐式toFunctionalBuilderOps 您可以在FunctionalBuilderOps 中看到您的and方法

When in doubt, decompile it. This showed that there is an implicit toFunctionalBuilderOps, which then you can see in FunctionalBuilderOps that there is your and method

这篇关于这是什么“和"?在ScalaJsonCombinator中(在定义Writes时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆