StructuredSerialization

File & Network View source
StructuredSerialization

Standard tuple codecs.

Field names passed to these codecs are converted by FieldSerializationWriter to fixed-width numeric IDs; names are not stored in the save string. Classes and tuples therefore use the same wire representation and migration rules. The compiler does not derive persistence IDs from field annotations; serialization metadata deliberately remains owned by this library.

Custom tuples need three overloads because tuples cannot implement FieldSerializable. The field intrinsics keep the codec concise and support mixed component types through overload resolution:

tuple InventorySlot(int itemId, int charges)

public function FieldSerializationWriter.write(string name, InventorySlot value)
let child = new FieldSerializationWriter(1, this.getIntegrityKey())
wurstForFields(value, (fieldName, fieldValue) -> child.write(fieldName, fieldValue))
this.writeObject(name, child)

public function FieldSerializationReader.read(string name, InventorySlot oldValue) returns InventorySlot
let child = this.readObject(name)
var result = oldValue
if child.isValid()
	wurstMapFields(result, (fieldName, fieldValue) -> fieldValue.readSerializedField(child, fieldName))
destroy child
return result

public function InventorySlot.readSerializedField(FieldSerializationReader reader, string name) returns InventorySlot
return reader.read(name, this)

class InventoryState implements FieldSerializable
use FieldSerializableLifecycle
InventorySlot slot = InventorySlot(0, 0)

override function writeSerializedFields(FieldSerializationWriter writer)
	wurstForFields((fieldName, fieldValue) -> writer.write(fieldName, fieldValue))

override function readSerializedFields(FieldSerializationReader reader)
	wurstMapFields((fieldName, fieldValue) -> fieldValue.readSerializedField(reader, fieldName))

SerializableFields can automatically use codecs declared by StructuredSerialization. For package-local codecs, use FieldSerializableLifecycle and keep the two intrinsic mapping methods in the same package as the overloads, as above. Nested custom tuples then compose once each tuple type provides the three overloads. Keep component names stable. For tuple-specific schema changes, increment the child writer’s schema version and call child.renameField(oldName, newName) before wurstMapFields when reading older versions.

Re-exports: StructuredSerializationCore

Types

Functions and constants

public function FieldSerializationWriter.write(string name, vec2 value)
public function FieldSerializationReader.read(string name, vec2 oldValue) returns vec2
public function vec2.readSerializedField(FieldSerializationReader reader, string name) returns vec2
public function FieldSerializationWriter.write(string name, vec3 value)
public function FieldSerializationReader.read(string name, vec3 oldValue) returns vec3
public function vec3.readSerializedField(FieldSerializationReader reader, string name) returns vec3
public function FieldSerializationWriter.write(string name, angle value)
public function FieldSerializationReader.read(string name, angle oldValue) returns angle
public function angle.readSerializedField(FieldSerializationReader reader, string name) returns angle
public function FieldSerializationWriter.write(string name, color value)
public function FieldSerializationReader.read(string name, color oldValue) returns color
public function color.readSerializedField(FieldSerializationReader reader, string name) returns color
public function FieldSerializationWriter.write(string name, colorA value)
public function FieldSerializationReader.read(string name, colorA oldValue) returns colorA
public function colorA.readSerializedField(FieldSerializationReader reader, string name) returns colorA
public function FieldSerializationWriter.write(string name, colorHSV value)
public function FieldSerializationReader.read(string name, colorHSV oldValue) returns colorHSV
public function colorHSV.readSerializedField(FieldSerializationReader reader, string name) returns colorHSV