Interface IComponentSerializer
- Namespace
- KeenEyes.Serialization
- Assembly
- KeenEyes.Core.dll
Interface for AOT-compatible component serialization.
public interface IComponentSerializer
Examples
// Use generated serializer for AOT compatibility
var serializer = new ComponentSerializationRegistry();
SnapshotManager.RestoreSnapshot(world, snapshot, serializer: serializer);
Remarks
This interface is implemented by generated code when components are marked with
[Component(Serializable = true)]. The source generator creates a strongly-typed
implementation that avoids runtime reflection.
For applications requiring AOT compatibility, pass the generated serializer to RestoreSnapshot(World, WorldSnapshot, IComponentSerializer) instead of relying on reflection.
Methods
CreateDefault(string)
Creates a default instance of a component type.
object? CreateDefault(string typeName)
Parameters
typeNamestringThe fully-qualified type name of the component.
Returns
- object
A default instance of the component, or null if the type is not registered.
Remarks
This method enables AOT-compatible default value creation without reflection.
The implementation should return default(T) for the appropriate type.
This is primarily used for tag components during delta restoration.
Deserialize(string, JsonElement)
Deserializes a component from JSON.
object? Deserialize(string typeName, JsonElement json)
Parameters
typeNamestringThe fully-qualified type name of the component.
jsonJsonElementThe JSON element containing the component data.
Returns
- object
The deserialized component, or null if the type is not registered.
GetType(string)
Gets the CLR type for a type name.
Type? GetType(string typeName)
Parameters
typeNamestringThe fully-qualified type name.
Returns
- Type
The CLR type, or null if not registered.
IsSerializable(string)
Checks if a component type name is registered for serialization.
bool IsSerializable(string typeName)
Parameters
typeNamestringThe fully-qualified type name to check.
Returns
- bool
True if the type can be serialized; false otherwise.
IsSerializable(Type)
Checks if a component type is registered for serialization.
bool IsSerializable(Type type)
Parameters
typeTypeThe component type to check.
Returns
- bool
True if the type can be serialized; false otherwise.
RegisterComponent(ISerializationCapability, string, bool)
Registers a component type in the world's component registry.
ComponentInfo? RegisterComponent(ISerializationCapability serialization, string typeName, bool isTag)
Parameters
serializationISerializationCapabilityThe serialization capability providing component registry access.
typeNamestringThe fully-qualified type name of the component.
isTagboolWhether the component is a tag component.
Returns
- ComponentInfo
The component info, or null if the type is not registered in this serializer.
Remarks
This method enables AOT-compatible component registration without reflection.
The implementation should call serialization.Components.Register<T>(isTag)
for the appropriate type.
Serialize(Type, object)
Serializes a component to JSON.
JsonElement? Serialize(Type type, object value)
Parameters
Returns
- JsonElement?
The serialized JSON element, or null if the type is not registered.
SetSingleton(ISerializationCapability, string, object)
Sets a singleton value in the world.
bool SetSingleton(ISerializationCapability serialization, string typeName, object value)
Parameters
serializationISerializationCapabilityThe serialization capability providing singleton access.
typeNamestringThe fully-qualified type name of the singleton.
valueobjectThe singleton value.
Returns
- bool
True if the singleton was set; false if the type is not registered.
Remarks
This method enables AOT-compatible singleton setting without reflection.
The implementation should call serialization.SetSingleton<T>((T)value)
for the appropriate type.