-
Notifications
You must be signed in to change notification settings - Fork 145
Java Serialization Utils
Rajesh R edited this page Jun 16, 2024
·
1 revision
Some common utils to serialize/deserialize using Java's serialization util
case class Person(name: String, age: Int)
val person = new Person("Chris", 24)
// Write
file.newOutputStream().asObjectOutputStream.serialize(obj).flush()
// Read
val person2 = file.newInputStream().asObjectInputStream.deserialize[Person]
assert(person == person2)
// Read using custom class loader:
file.newInputStream().asObjectInputStreamUsingClassLoader(classLoader = myClassLoader).deserialize[Person]
The above can be simply written as:
val person2: Person = file.writeSerialized(person).readDeserialized()
assert(person == person2)