Scala Setup
Fory Scala provides binary Object Serialization, generated models, and Fory gRPC. The Fory Scala artifact supports Scala 2.13 and Scala 3; generated Scala models require Scala 3.
Verify the Toolchain
java -version
scala -version
sbt --version
Object Serialization
Add the Fory Scala library to build.sbt:
ThisBuild / scalaVersion := "3.3.1"
libraryDependencies += "org.apache.fory" %% "fory-scala" % "1.5.0"
Create src/main/scala/ScalaExample.scala:
import org.apache.fory.Fory
import org.apache.fory.scala.ForyScala
case class User(id: Long, name: String)
object ScalaExample {
def main(args: Array[String]): Unit = {
val fory: Fory = ForyScala.builder()
.withXlang(true)
.build()
fory.register(classOf[User], 1)
val bytes = fory.serialize(User(1, "Alice"))
val decoded = fory.deserialize(bytes).asInstanceOf[User]
println(decoded.name)
}
}
sbt run
Use xlang mode when a peer uses a different Fory implementation family; use native mode for data within the JVM Fory implementation family. Continue with Scala Object Serialization, xlang, or native mode.
Other Capabilities
- Fory IDL and Compiler generates Scala 3 models and registration helpers. See Compiler Getting Started and the Scala generated-code guide.
- Fory gRPC uses grpc-java transports with Fory-encoded messages. See Scala gRPC.