【问题标题】:could not find implicit value for FromRecord找不到 FromRecord 的隐含值
【发布时间】:2017-07-24 19:54:14
【问题描述】:

我正在尝试开始使用 avro 和 avro4s,但在让一个愚蠢的示例正常工作时遇到了麻烦。

我什至无法在源代码库中找到我自己的测试版本来编译 - https://github.com/sksamuel/avro4s/blob/master/avro4s-core/src/test/scala/com/sksamuel/avro4s/AvroJsonInputTest.scala#L14-L20

我收到了标题could not find implicit value for parameter fromRecord: com.sksamuel.avro4s.FromRecord[Foo] 中提到的错误。

package test

import java.io.ByteArrayInputStream

import com.sksamuel.avro4s._
import org.specs2.mutable.Specification

class AvroSpec extends Specification {
  "My classes" should {
    "be deserialized by avro" in {
      case class Foo(num: Int, name: String)
      val json = """{ "num": 17, "name": "jibba-jabbba" }"""
      val inst = Foo(num = 17, name = "jibba-jabba")
      val in =  new ByteArrayInputStream(json.getBytes("UTF-8"), 0, json.length)
      val foo = new AvroJsonInputStream[Foo](in).iterator.toSet
      in.close()

      foo mustEqual Set(inst)
    }
  }
}

我正在使用 Scala 2.11.8 和 avro 1.7.0。我做错了什么?

【问题讨论】:

  • 我可以使用 avro4s 序列化我的实际域对象,只是反序列化是个问题。

标签: scala avro4s


【解决方案1】:

将案例类声明移到方法定义之外:

package test

import java.io.ByteArrayInputStream

import com.sksamuel.avro4s._
import org.specs2.mutable.Specification

case class Foo(num: Int, name: String)
class AvroSpec extends Specification {
  "My classes" should {
    "be deserialized by avro" in {
      val json = """{ "num": 17, "name": "jibba-jabbba" }"""
      val inst = Foo(num = 17, name = "jibba-jabba")
      val in =  new ByteArrayInputStream(json.getBytes("UTF-8"), 0, json.length)
      val foo = new AvroJsonInputStream[Foo](in).iterator.toSet
      in.close()

      foo mustEqual Set(inst)
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多