Class JsonSyncSerializer
- Namespace
- SteamNetworkLib.Sync
- Assembly
- SteamNetworkLib.dll
Default JSON serializer for HostSyncVar<T> and ClientSyncVar<T>. Provides IL2CPP-compatible JSON serialization without external dependencies.
public class JsonSyncSerializer : ISyncSerializer
- Inheritance
-
JsonSyncSerializer
- Implements
- Inherited Members
Remarks
Supported Types:
- Primitives: int, long, float, double, bool, string, byte, short, uint, ulong
- Enums: Any enum type (serialized as underlying integer value)
- Collections: List<T>, arrays (T[]), Dictionary<string, T>
- Custom types: Classes and structs with parameterless constructor and public properties
Requirements for Custom Types:
- Must have a public parameterless constructor
- Properties must be public with both getter and setter
- Property types must themselves be serializable
- Circular references are not supported
// Valid custom type
public class GameSettings
{
public int MaxPlayers { get; set; } = 4;
public string MapName { get; set; } = "default";
public bool FriendlyFire { get; set; } = false;
public List<string> EnabledMods { get; set; } = new();
}
// Usage
var settings = client.CreateHostSyncVar("Settings", new GameSettings());
Constructors
JsonSyncSerializer()
public JsonSyncSerializer()
Methods
CanSerialize(Type)
Checks if a type can be serialized by this serializer.
public bool CanSerialize(Type type)
Parameters
typeTypeThe type to check.
Returns
- bool
True if the type can be serialized; otherwise, false.
Deserialize<T>(string)
Deserializes a JSON string to the specified type.
public T Deserialize<T>(string data)
Parameters
datastringThe JSON string to deserialize.
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 JSON string.
public string Serialize<T>(T value)
Parameters
valueTThe value to serialize.
Returns
- string
A JSON string representation of the value.
Type Parameters
TThe type of value to serialize.
Exceptions
- SyncSerializationException
Thrown when serialization fails.