在 json4s 4.0.4 中找不到 org.json4s.AsJsonInput 的隐式值

发布于 2025-01-16 13:45:36 字数 1597 浏览 1 评论 0原文

json4s版本

在sbt中: "org.json4s" %% "json4s-jackson" % "4.0.4"

scala 版本

2.12.15

jdk 版本

JDK8

我的问题

当我学会使用 json4s 读取 json 文件“file.json”时。 (在《Scala设计模式》一书中)

import org.json4s._
import org.json4s.jackson.JsonMethods._

trait DataReader {
  def readData(): List[Person]

  def readDataInefficiently(): List[Person]
}

class DataReaderImpl extends DataReader {

  implicit val formats = DefaultFormats

  private def readUntimed(): List[Person] =
    parse(StreamInput(getClass.getResourceAsStream("file.json"))).extract[List[Person]]

  override def readData(): List[Person] = readUntimed()

  override def readDataInefficiently(): List[Person] = {

    (1 to 10000).foreach(_ =>
      readUntimed())
    readUntimed()
  }
}

object DataReaderExample {
  def main(args: Array[String]): Unit = {
    val dataReader = new DataReaderImpl
    println(s"I just read the following data efficiently:${dataReader.readData()}")
    println(s"I just read the following data inefficiently:${dataReader.readDataInefficiently()}")
  }
}

它无法正确编译,并抛出:

could not find implicit value for evidence parameter of type org.json4s.AsJsonInput[org.json4s.StreamInput]
Error occurred in an application involving default arguments.
    parse(StreamInput(getClass.getResourceAsStream("file.json"))).extract[List[Person]]

当我在sbt中更改3.6.0-M2中的json4s版本时: “org.json4s”%%“json4s-jackson”%“3.6.0-M2”

效果很好。

为什么会出现这种情况呢?在4.0.4或更高版本中应该如何修复它?

感谢您的帮助。

json4s version

In sbt:
"org.json4s" %% "json4s-jackson" % "4.0.4"

scala version

2.12.15

jdk version

JDK8

My problem

When I learnt to use json4s to read a json file "file.json".
(In book "Scala design patterns")

import org.json4s._
import org.json4s.jackson.JsonMethods._

trait DataReader {
  def readData(): List[Person]

  def readDataInefficiently(): List[Person]
}

class DataReaderImpl extends DataReader {

  implicit val formats = DefaultFormats

  private def readUntimed(): List[Person] =
    parse(StreamInput(getClass.getResourceAsStream("file.json"))).extract[List[Person]]

  override def readData(): List[Person] = readUntimed()

  override def readDataInefficiently(): List[Person] = {

    (1 to 10000).foreach(_ =>
      readUntimed())
    readUntimed()
  }
}

object DataReaderExample {
  def main(args: Array[String]): Unit = {
    val dataReader = new DataReaderImpl
    println(s"I just read the following data efficiently:${dataReader.readData()}")
    println(s"I just read the following data inefficiently:${dataReader.readDataInefficiently()}")
  }
}

It cannot compile correctly, and throw:

could not find implicit value for evidence parameter of type org.json4s.AsJsonInput[org.json4s.StreamInput]
Error occurred in an application involving default arguments.
    parse(StreamInput(getClass.getResourceAsStream("file.json"))).extract[List[Person]]

when I change json4s version in 3.6.0-M2 in sbt:
"org.json4s" %% "json4s-jackson" % "3.6.0-M2"

It works well.

Why would this happen? How should I fix it in 4.0.4 or higher version?

Thank you for your Help.

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

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

发布评论

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

评论(1

咽泪装欢 2025-01-23 13:45:36

我尝试了很多方法来解决这个问题。
最后:

删除 : 中的 StreamInput

  private def readUntimed(): List[Person] = {
    val inputStream: InputStream = getClass.getResourceAsStream("file.json")
    // parse(StreamInput(inputStream)).extract[List[Person]] // this will work in 3.6.0-M2
    parse(inputStream).extract[List[Person]]
  }

现在它可以工作了!

I tried many ways to solve this problem.
And finally:

remove StreamInput in :

  private def readUntimed(): List[Person] = {
    val inputStream: InputStream = getClass.getResourceAsStream("file.json")
    // parse(StreamInput(inputStream)).extract[List[Person]] // this will work in 3.6.0-M2
    parse(inputStream).extract[List[Person]]
  }

and now it works !

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