Serialisation – Arma Reforger
Jump to navigation
Jump to search
m (Added return type) |
m (Add warning about parameterless constructor and rename context to make the example easy to copy paste) |
||
Line 8: | Line 8: | ||
<enforce> | <enforce> | ||
SCR_JsonSaveContext | SCR_JsonSaveContext saveContext = new SCR_JsonSaveContext(); | ||
string stringValue = "data"; | string stringValue = "data"; | ||
int integerValue = 123; | int integerValue = 123; | ||
saveContext.WriteValue("key1", stringValue); | |||
saveContext.WriteValue("key2", integerValue); | |||
// process saved data (export, send, save...), in this case data are exported as json string | // process saved data (export, send, save...), in this case data are exported as json string | ||
string dataString = | string dataString = saveContext.ExportToString(); | ||
</enforce> | </enforce> | ||
Line 23: | Line 23: | ||
<enforce> | <enforce> | ||
SCR_JsonLoadContext | SCR_JsonLoadContext loadContext = new SCR_JsonLoadContext(); | ||
loadContext.ImportFromString(dataString); | |||
string stringValue; | string stringValue; | ||
Line 30: | Line 30: | ||
// order does not matter for JSON as it uses key names | // order does not matter for JSON as it uses key names | ||
loadContext.ReadValue("key2", integerValue); | |||
loadContext.ReadValue("key1", stringValue); | |||
</enforce> | </enforce> | ||
Line 40: | Line 40: | ||
<enforce> | <enforce> | ||
SCR_BinSaveContext | SCR_BinSaveContext saveContext = new SCR_BinSaveContext(); | ||
string stringValue = "data"; | string stringValue = "data"; | ||
int integerValue = 123; | int integerValue = 123; | ||
saveContext.WriteValue("key1", stringValue); | |||
saveContext.WriteValue("key2", integerValue); | |||
// process saved data (export, send, save...), in this case data are saved to "file.bin" | // process saved data (export, send, save...), in this case data are saved to "file.bin" | ||
saveContext.SaveToFile("file.bin"); | |||
</enforce> | </enforce> | ||
Line 55: | Line 55: | ||
<enforce> | <enforce> | ||
SCR_BinLoadContext | SCR_BinLoadContext loadContext = new SCR_BinLoadContext(); | ||
loadContext.LoadFromFile("file.bin"); | |||
string stringValue; | string stringValue; | ||
Line 62: | Line 62: | ||
// order matters for Binary serialisation, as Binary ignores names | // order matters for Binary serialisation, as Binary ignores names | ||
loadContext.ReadValue("key1", stringValue); | |||
loadContext.ReadValue("key2", integerValue); | |||
</enforce> | </enforce> | ||
Line 72: | Line 72: | ||
The following class set to serialise will serialise all its properties. | The following class set to serialise will serialise all its properties. | ||
{{Feature|warning|Data structures that shall be serialised are not allowed to have parameters in their constructor or they can not be read back.}} | |||
<enforce> | <enforce> |
Revision as of 12:37, 5 August 2022
This page talks about SCR_JsonSaveContext/SCR_JsonLoadContext and SCR_BinSaveContext/SCR_BinLoadContext.
JSON
Serialisation
SCR_JsonSaveContext saveContext = new SCR_JsonSaveContext();
string stringValue = "data";
int integerValue = 123;
saveContext.WriteValue("key1", stringValue);
saveContext.WriteValue("key2", integerValue);
// process saved data (export, send, save...), in this case data are exported as json string
string dataString = saveContext.ExportToString();
Deserialisation
SCR_JsonLoadContext loadContext = new SCR_JsonLoadContext();
loadContext.ImportFromString(dataString);
string stringValue;
int integerValue;
// order does not matter for JSON as it uses key names
loadContext.ReadValue("key2", integerValue);
loadContext.ReadValue("key1", stringValue);
Binary
Serialisation
SCR_BinSaveContext saveContext = new SCR_BinSaveContext();
string stringValue = "data";
int integerValue = 123;
saveContext.WriteValue("key1", stringValue);
saveContext.WriteValue("key2", integerValue);
// process saved data (export, send, save...), in this case data are saved to "file.bin"
saveContext.SaveToFile("file.bin");
Deserialisation
SCR_BinLoadContext loadContext = new SCR_BinLoadContext();
loadContext.LoadFromFile("file.bin");
string stringValue;
int integerValue;
// order matters for Binary serialisation, as Binary ignores names
loadContext.ReadValue("key1", stringValue);
loadContext.ReadValue("key2", integerValue);
Object Serialisation
Simple
The following class set to serialise will serialise all its properties.
NonSerialized
Adding the NonSerialized() decorator to a field will make the serialisation ignore it.
Advanced
The following methods allow to define a custom serialisation per class. This is useful to avoid saving lengthy yet useless information for loading as well as load values in a certain order.
SerializationSave
If an object has the SerializationSave method defined, the SaveContext will use it and not process object's properties automatically at all.
class MyClass : Managed
{
protected int m_iVariable = 42;
protected string m_sVariable = "Hello there";
protected float m_fVariable = 33.3;
bool SerializationSave(BaseSerializationSaveContext context)
{
if (!context.IsValid())
return false;
context.WriteValue("theString", m_sVariable);
context.WriteValue("integer", m_iVariable);
context.WriteValue("floatingpoint", m_fVariable);
return true;
}
};
SerializationLoad
If an object has the SerializationLoad method defined, the SaveContext will use it and not process object's properties automatically at all.
class MyClass : Managed
{
protected int m_iVariable = 42;
protected string m_sVariable = "Hello there";
protected float m_fVariable = 33.3;
bool SerializationLoad(BaseSerializationLoadContext context)
{
if (!context.IsValid())
return false;
context.ReadValue("theString", m_sVariable);
context.ReadValue("integer", m_iVariable);
context.ReadValue("floatingpoint", m_fVariable);
return true;
}
};