Interface ISyncSerializer
- Namespace
- SteamNetworkLib.Sync
- Assembly
- SteamNetworkLib.dll
Interface for custom value serialization in HostSyncVar<T> and ClientSyncVar<T>.
public interface ISyncSerializer
Remarks
Implement this interface to provide custom serialization logic for complex types or when the default JsonSyncSerializer doesn't meet your needs.
public class MyCustomSerializer : ISyncSerializer
{
public string Serialize<T>(T value)
{
// Custom serialization logic
return MySerializationLibrary.ToJson(value);
}
public T Deserialize<T>(string data)
{
// Custom deserialization logic
return MySerializationLibrary.FromJson<T>(data);
}
public bool CanSerialize(Type type)
{
return MySerializationLibrary.IsSupported(type);
}
}
Methods
CanSerialize(Type)
Checks if a type can be serialized by this serializer.
bool CanSerialize(Type type)
Parameters
typeTypeThe type to check.
Returns
- bool
True if the type can be serialized; otherwise, false.
Remarks
This method is called during HostSyncVar<T> and ClientSyncVar<T> creation to validate that the type parameter is serializable.
Deserialize<T>(string)
Deserializes a string back to the original value type.
T Deserialize<T>(string data)
Parameters
datastringThe serialized string data.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize to.
Exceptions
- SyncSerializationException
Thrown when deserialization fails.
Serialize<T>(T)
Serializes a value to a string for network transmission.
string Serialize<T>(T value)
Parameters
valueTThe value to serialize.
Returns
- string
A string representation of the value.
Type Parameters
TThe type of value to serialize.
Exceptions
- SyncSerializationException
Thrown when serialization fails.