Ray Rahke
05/17/2024, 2:16 AMclass Bar { }
class Foo {
bar: Bar
}
@Serializable
data class Context(val a: Int, val obj: Foo) {}
Do all three of these classes need to be marked as @Serializable? (edited)
or will it recursively serialize everythingPrateek
05/17/2024, 5:16 AMJoffrey
05/17/2024, 7:54 AM@Serializable
is necessary so the compiler plugin generates a serializer for the annotated class.
Serializing a root object in a composition does serialize everything recursively, but everything in the chain needs to be serializable.Ray Rahke
05/17/2024, 10:26 AMGame
object that contains a reference to all entities and items and buildings in my game, and then just pickle / serialize that object into binary and then reconstruct it or somethingRay Rahke
05/17/2024, 10:26 AMRay Rahke
05/17/2024, 10:28 AMRay Rahke
05/17/2024, 10:28 AMJoffrey
05/17/2024, 10:28 AMhaving to mark every single class in my game as @Serializable is not appealingIt's even less appealing to accidentally realize at runtime that one of the classes accidentally gets serialized, and happens to have properties of unserializable types like
InputStream
or function types.Prateek
05/17/2024, 10:29 AMJoffrey
05/17/2024, 10:30 AMRay Rahke
05/17/2024, 10:30 AMJoffrey
05/17/2024, 10:30 AMRay Rahke
05/17/2024, 10:31 AMRay Rahke
05/17/2024, 10:31 AMJoffrey
05/17/2024, 10:31 AMIs serializing one enormous tree of serializable objects, representing the entire "game state" at that moment, a respectable approach?Yes, but you most likely want to control what actually is saved as part of the Game state
Joffrey
05/17/2024, 10:32 AMRay Rahke
05/17/2024, 10:32 AMCharacter
class, I will have to serialize the Vector3d
that represents character.position
Ray Rahke
05/17/2024, 10:33 AMRay Rahke
05/17/2024, 10:33 AMInventory
inside of Character
Ray Rahke
05/17/2024, 10:33 AMInventory
contains a list of all of your Item
instancesRay Rahke
05/17/2024, 10:33 AMItem
Joffrey
05/17/2024, 10:33 AMRay Rahke
05/17/2024, 10:34 AMRay Rahke
05/17/2024, 10:34 AMJoffrey
05/17/2024, 10:34 AMdata
keywork to mark your data classes. Do you consider that a big hassle to mark all data classes with the data
keyword?Ray Rahke
05/17/2024, 10:34 AMRay Rahke
05/17/2024, 10:34 AMRay Rahke
05/17/2024, 10:34 AMRay Rahke
05/17/2024, 10:35 AMJoffrey
05/17/2024, 10:36 AM@Serializable
classes should be data
classes, nor the other way around. I was just mentioning data class
as a very close analogy to what you do with serializable classesRay Rahke
05/17/2024, 10:36 AMRay Rahke
05/17/2024, 10:37 AM@Serializable
class Bar { }
@Serializable
class Foo {
@Serializable
bar: Bar
}
@Serializable
data class Context(
val a: Int,
@Serializable
val obj: Foo) {}
Joffrey
05/17/2024, 10:37 AMJoffrey
05/17/2024, 10:38 AMPrateek
05/17/2024, 10:38 AMRay Rahke
05/17/2024, 10:40 AMdata class
, I will have to actually implement the boilerplate/algorithm myself?Joffrey
05/17/2024, 10:40 AMJoffrey
05/17/2024, 10:41 AMJoffrey
05/17/2024, 10:44 AMInputStream
or any class that you didn't design with serialization in mind
• readability: when reading the code, you can clearly see what is part of the serialized game state or not