Skip to content
Marc edited this page Aug 16, 2018 · 5 revisions

Arrays are used to store C# arrays. The supported data types are: bool, char, byte, short, int, UInt64, float, double and string.
Arrays are really useful to store coordinates, vectors, vertices...

Using arrays

To create an array, we just have to give it a name and an array of data. Because C# arrays already contain the number of elements in the array, in the C# version of Cereal we do not need to specify an item count:

int[] myArrayData = { 1, 2, 3, 4 }; // Data to be stored by the array
Cereal.Array myArray = new Cereal.Array("Array name", myArrayData); // Create an array with the data 'myArrayData'

That will create an array with the name Array name and it will contain the data { 1, 2, 3, 4 }.
Next, we can write everything into a buffer:

myArray.Write(ref buffer) // Write 'myArray' to the buffer 'buffer'

Array.Write will return true if the array was successfully serialized, and false otherwise. Normally, if the function fails is because the buffer is too small to store that array.

To read an array, we just have to call Array.Read(ref Buffer buffer):

Cereal.Array otherArray = new Cereal.Array(); // Create an empty array
otherArray.Read(ref buffer); // Read the array data from the buffer

And now our array should contain the data. To retrieve the original array we can use two functions: the first one is type[] GetRawArray(type[] mem), which copies the array data to a memory address we give to it, so it's better if we want to have a pure C# array:

int[] arrayData = new int[otherArray.ItemCount]; // Create a pointer to store our data
arrayData = otherArray.GetRawArray(arrayData); // Store the array data in 'arrayData'

If we instead want to get a List<> containing our array data, we will need to call a different function depending on the data type contained by the array. Use the following table to find out the function you have to use:

Data Type Function Description
bool getArrayBool() Returns a List<bool> containing the array data
char getArrayChar() Returns a List<char> containing the array data
byte getArrayByte() Returns a List<byte> containing the array data
short getArrayShort() Returns a List<short> containing the array data
string getArrayString() Returns a List<string> containing the array data
int getArrayInt32() Returns a List<int> containing the array data
float getArrayFloat() Returns a List<float> containing the array data
Int64 getArrayInt64() Returns a List<Int64> containing the array data
double getArrayDouble() Returns a List<double> containing the array data

Here's an example code to get an int array out of a serialized array using this function:

List<int> arrayData = otherArray.GetArrayInt32(); // Get the list with the array data

For more information about how to use arrays with objects, please check out objects.

Properties

This is the list of the properties exposed by this class and their C++ equivalent, along with the accessor of each property:

Property Accessor C++ getter C++ setter Return data
Name get; set getName() - Name of the array (string)
Size get getSize() - Size (in bytes) of the serialized array (uint)
ItemCount get getCount() - Amount of items in the array (uint)
DataType get getDataType() - Data type of the data (Cereal.Global.DataType)

Sample code

namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = { "array", "of", "strings", "to save in the serialized array" };
            Cereal.Buffer buffer = new Cereal.Buffer(1024); // Create a buffer with 1024 bytes
            Cereal.Array myArray = new Cereal.Array("Array name", words); // Create an array with the strings in 'words'

            // Writing an array
            myArray.Write(ref buffer); // Write the array to the buffer

            // Reading an array
            Cereal.Array otherArray = new Cereal.Array(); // Create another array
            buffer.Position = 0; // Move the buffer back to the beginning
            otherArray.Read(ref buffer); // Read the array from the buffer

            List<string> data = otherArray.GetArrayString(); // Get the array. 'data' now contains the strings in'words'
        }
    }
}