[英]Scala compile errors: “No implicit view available” and “diverging implicit expansion”
def MyFun(result: ListBuffer[(String, DateTime, List[(String, Int)])]):
String = {
val json =
(result.map {
item => (
("subject" -> item._1) ~
("time" -> item._2) ~
("student" -> item._3.map {
student_description=> (
("name" -> lb_result._1) ~
("age" -> lb_result._2)
)
})
)
}
)
val resultFormat = compact(render(json))
resultFormat
}
error 1: No implicit view available from org.joda.time.DateTime => org.json4s.JsonAST.JValue.("subject" -> item._1) ~ 错误1:从org.joda.time.DateTime => org.json4s.JsonAST.JValue。(“主题”-> item._1)中没有隐式视图可用〜
error 2: diverging implicit expansion for type Nothing => org.json4s.JsonAST.JValue starting with method seq2jvalue in trait JsonDSL val resultFormat = compact(render(json)) 错误2:类型Nothing => org.json4s.JsonAST.JValue的隐式扩展在特征JsonDSL val中的方法seq2jvalue开始发散val resultFormat = compact(render(json))
I hinted at json4s-ext for joda-time support, but only importing this submodule won't solve your problem. 我向json4s-ext暗示了对joda-time的支持,但是仅导入此子模块并不能解决您的问题。
JsonDSL is used to create JValues
and serializers are only used to turn JValues
into JSON and vise versa (serialization and deserialization). JsonDSL用于创建JValues
而序列化程序仅用于将JValues
转换为JSON,反之亦然(序列化和反序列化)。
If we try to create a simpler json object with a DateTime
: 如果我们尝试使用DateTime
创建一个更简单的json对象:
val jvalue = ("subj" -> "foo") ~ ("time" -> DateTime.now)
We get the same error : 我们得到同样的错误:
error: No implicit view available from org.joda.time.DateTime => org.json4s.JsonAST.JValue.
Like I said, the serializers for DateTime
from json4s-ext are not used when we use JsonDSL to create JValues
. 就像我说的那样,当我们使用JsonDSL创建JValues
时,不使用json4s-ext中DateTime
的序列化JValues
。
You could create an implicit function DateTime => JValue
or do something like DateTime.now.getMillis
or DateTime.now.toString
to create respectively a JInt
or a JString
, but why would we reinvent the wheel if joda time serializers already exist ? 您可以创建一个隐式函数DateTime => JValue
或执行类似DateTime.now.getMillis
或DateTime.now.toString
来分别创建JInt
或JString
,但是如果已经存在joda时间序列化JString
,为什么还要重新发明轮子呢?
We can introduce some case classes to hold the data in result
and then json4s can serialize them for us : 我们可以引入一些case类来保存result
的数据,然后json4s可以为我们序列化它们:
import scala.collection.mutable.ListBuffer
import com.github.nscala_time.time.Imports._
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{read, write}
implicit val formats =
Serialization.formats(NoTypeHints) ++ org.json4s.ext.JodaTimeSerializers.all
case class Lesson(subject: String, time: org.joda.time.DateTime, students: List[Student])
case class Student(name: String, age: Int)
val result = ListBuffer(("subj", DateTime.now, ("Alice", 20) :: Nil))
val lessons = result.map { case (subj, time, students) =>
Lesson(subj, time, students.map(Student.tupled))
}
write(lessons)
// String = [{"subject":"subj","time":"2015-09-09T11:22:59.762Z","students":[{"name":"Alice","age":20}]}]
Note that you still need to add json4s-ext like Andreas Neumann explained. 请注意,您仍然需要像Andreas Neumann所解释的那样添加json4s-ext 。
As Peter Neyens allready said for serializing org.joda.time.DateTime
you ned the ext package 正如Peter Neyens早就说过要序列化org.joda.time.DateTime
您已经设置了ext包。
https://github.com/json4s/json4s#extras https://github.com/json4s/json4s#extras
So add this dependency to you build.sbt
因此,将此依赖项添加到您的build.sbt
libraryDependencies += "org.json4s" %% "json4s-ext" % "3.2.11"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.