Serialisation – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Fix comment)
m (Text replacement - "<syntaxhighlight lang="c#">" to "<enforce>")
Line 7: Line 7:
=== Serialisation ===
=== Serialisation ===


<syntaxhighlight lang="c#">
<enforce>
SCR_JsonSaveContext context = new SCR_JsonSaveContext();
SCR_JsonSaveContext context = new SCR_JsonSaveContext();


Line 22: Line 22:
=== Deserialisation ===
=== Deserialisation ===


<syntaxhighlight lang="c#">
<enforce>
SCR_JsonLoadContext context = new SCR_JsonLoadContext();
SCR_JsonLoadContext context = new SCR_JsonLoadContext();
context.ImportFromString(dataString);
context.ImportFromString(dataString);
Line 39: Line 39:
=== Serialisation ===
=== Serialisation ===


<syntaxhighlight lang="c#">
<enforce>
SCR_BinSaveContext context = new SCR_BinSaveContext();
SCR_BinSaveContext context = new SCR_BinSaveContext();


Line 54: Line 54:
=== Deserialisation ===
=== Deserialisation ===


<syntaxhighlight lang="c#">
<enforce>
SCR_BinLoadContext context = new SCR_BinLoadContext();
SCR_BinLoadContext context = new SCR_BinLoadContext();
context.LoadFromFile("file.bin");
context.LoadFromFile("file.bin");
Line 73: Line 73:
The following class set to serialise will serialise all its properties.
The following class set to serialise will serialise all its properties.


<syntaxhighlight lang="c#">
<enforce>
class MyClass : Managed
class MyClass : Managed
{
{
Line 84: Line 84:
==== NonSerialized ====
==== NonSerialized ====
Adding the {{hl|NonSerialized()}} decorator to a field will make the serialisation ignore it.
Adding the {{hl|NonSerialized()}} decorator to a field will make the serialisation ignore it.
<syntaxhighlight lang="c#">
<enforce>
class MyClass : Managed
class MyClass : Managed
{
{
Line 103: Line 103:
==== SerializationSave ====
==== SerializationSave ====
If an object has the {{hl|SerializationSave}} method defined, the SaveContext will use it and not process object's properties automatically at all.
If an object has the {{hl|SerializationSave}} method defined, the SaveContext will use it and not process object's properties automatically at all.
<syntaxhighlight lang="c#">
<enforce>
class MyClass : Managed
class MyClass : Managed
{
{
Line 127: Line 127:
If an object has the {{hl|SerializationLoad}} method defined, the SaveContext will use it and not process object's properties automatically at all.
If an object has the {{hl|SerializationLoad}} method defined, the SaveContext will use it and not process object's properties automatically at all.


<syntaxhighlight lang="c#">
<enforce>
class MyClass : Managed
class MyClass : Managed
{
{

Revision as of 19:17, 30 July 2022

This page talks about SCR_JsonSaveContext/SCR_JsonLoadContext and SCR_BinSaveContext/SCR_BinLoadContext.


JSON

Serialisation

<enforce> SCR_JsonSaveContext context = new SCR_JsonSaveContext();

string stringValue = "data"; int integerValue = 123;

context.WriteValue("key1", stringValue); context.WriteValue("key2", integerValue);

// process saved data (export, send, save...), in this case data are exported as json string dataString = context.ExportToString(); </syntaxhighlight>

Deserialisation

<enforce> SCR_JsonLoadContext context = new SCR_JsonLoadContext(); context.ImportFromString(dataString);

string stringValue; int integerValue;

// order does not matter for JSON as it uses key names context.ReadValue("key2", integerValue); context.ReadValue("key1", stringValue); </syntaxhighlight>


Binary

Serialisation

<enforce> SCR_BinSaveContext context = new SCR_BinSaveContext();

string stringValue = "data"; int integerValue = 123;

context.WriteValue("key1", stringValue); context.WriteValue("key2", integerValue);

// process saved data (export, send, save...), in this case data are saved to "file.bin" context.SaveToFile("file.bin"); </syntaxhighlight>

Deserialisation

<enforce> SCR_BinLoadContext context = new SCR_BinLoadContext(); context.LoadFromFile("file.bin");

string stringValue; int integerValue;

// order matters for Binary serialisation, as Binary ignores names context.ReadValue("key1", stringValue); context.ReadValue("key2", integerValue); </syntaxhighlight>


Object Serialisation

Simple

The following class set to serialise will serialise all its properties.

<enforce> class MyClass : Managed { protected int m_iVariable = 42; protected string m_sVariable; protected float m_fVariable = 33.3; }; </syntaxhighlight>

NonSerialized

Adding the NonSerialized() decorator to a field will make the serialisation ignore it. <enforce> class MyClass : Managed { protected int m_iVariable = 42; protected string m_sVariable = "Hello there";

[NonSerialized()] protected float m_fVariable = 33.3; }; </syntaxhighlight>

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.

The NonSerialized decorator is only useful with the simple object serialisation - SerializationSave/SerializationLoad will ignore it.

SerializationSave

If an object has the SerializationSave method defined, the SaveContext will use it and not process object's properties automatically at all. <enforce> class MyClass : Managed { protected int m_iVariable = 42; protected string m_sVariable = "Hello there"; protected float m_fVariable = 33.3;

bool SerializationSave(SerializationSaveContext context) { if (!context.IsValid()) return false;

context.WriteValue("theString", m_sVariable); context.WriteValue("integer", m_iVariable); context.WriteValue("floatingpoint", m_fVariable);

return true; } }; </syntaxhighlight>

SerializationLoad

If an object has the SerializationLoad method defined, the SaveContext will use it and not process object's properties automatically at all.

<enforce> class MyClass : Managed { protected int m_iVariable = 42; protected string m_sVariable = "Hello there"; protected float m_fVariable = 33.3;

bool SerializationLoad(SerializationLoadContext context) { if (!context.IsValid()) return false;

context.ReadValue("theString", m_sVariable); context.ReadValue("integer", m_iVariable); context.ReadValue("floatingpoint", m_fVariable);

return true; } }; </syntaxhighlight>