首页 > 解决方案 > 如何在scala中反序列化protobuf

问题描述

我在 Python 进程生成的文件中有一个 protobuf 序列化对象。我现在需要在 Scala 中反序列化它。我在官方 google protobuf 文档中没有看到 scala 文档。有参考吗?我该怎么做。

我开始为此研究 ScalaPB,但遇到了错误。有什么建议么 -

plugins.sbt 文件:`addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.34")

libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.10.7"`

错误 -[error] (update) sbt.librarymanagement.ResolveException: Error downloading com.thesamet:sbt-protoc;sbtVersion=1.0;scalaVersion=2.12:0.99.34 [error] Not found [error] Not found [error] not found: C:\Users\USER\.ivy2\local\com.thesamet\sbt-protoc\scala_2.12\sbt_1.0\0.99.34\ivys\ivy.xml [error] not found: https://repo1.maven.org/maven2/com/thesamet/sbt-protoc_2.12_1.0/0.99.34/sbt-protoc-0.99.34.pom [error] (ssExtractDependencies) sbt.librarymanagement.ResolveException: Error downloading com.thesamet:sbt-protoc;sbtVersion=1.0;scalaVersion=2.12:0.99.34 [error] Not found [error] Not found [error] not found: C:\Users\USER\.ivy2\local\com.thesamet\sbt-protoc\scala_2.12\sbt_1.0\0.99.34\ivys\ivy.xml [error] not found: https://repo1.maven.org/maven2/com/thesamet/sbt-protoc_2.12_1.0/0.99.34/sbt-protoc-0.99.34.pom [error] Total time: 8 s, completed Jul 23, 2020, 6:41:59 PM [info] shutting down sbt server

标签: scalaprotocol-buffers

解决方案


您需要能够访问描述在 python 进程中序列化的对象的消息模式的 proto 文件。文件必须在项目之间共享,这是定义合同的强制性信息。

然后,您可以使用ScalaPB生成相应的案例类。这些类由 sbt 插件自动生成,它们提供可用于反序列化的辅助方法。检查parseFrom生成Message类的伴随对象中提供的方法


推荐阅读