Skip to content
Marc edited this page Jan 2, 2017 · 17 revisions

Fields are the simplest serialized data object. It has a name, and stores a single data type. The valid data types are: bool, char, byte, short, int, long long, float, double, std::string
They are a good choice if you only need to store the contents of a single variable.

Using fields

To use fields, you only need to include Cereal.h.
Now, type the following in your code:

C++:

Cereal::Field* myField = new Cereal::Field("Field name", 'A');

C Sharp:

Cereal.Field myField = new Cereal.Field("Field name", 'A');

That will create a field with the name "Field name" and will store the character A. Now, you can use the function Field::write(Buffer& buffer) to serialize its contents to a buffer:

C++:

myField->write(buffer); // Write the contents of a field to the buffer

C Sharp:

myField.write(ref buffer); // Write the contents of a field to the buffer

Instead, if you need to deserialize the contents of a field, you will have to read a buffer containing a field, and then call Field::getValue<dataType>():

C++:

Cereal::Field* myField = new Cereal::Field; // We create an empty field because we will overwrite it anyway
myField->read(buffer); // Now the field is full of data
char deserializedData = myField->getValue<char>(); // We store the contents of the field

C Sharp:

In C#, you can't call myField.getValue<dataType>() because of the limitations of the language. Instead, you have to call a different function for each data type:

Data Type Function Data Type Function
bool getBool() int getInt32()
char getChar() float getFloat()
byte getByte() Int64 getInt64()
short getShort() double getDouble()
string getString()
Cereal.Field myField = new Cereal.Field(); // We create an empty field because we will overwrite it anyway
myField.read(ref buffer); // Now the field is full of data
char deserializedData = myField.getChar(); // We store the contents of the field

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

Sample code

C++:

#include <Cereal.h> // Include the library

int main()
{
    Cereal::Buffer buffer(1024); // Create a buffer with 1024 bytes
    Cereal::Field* myField = new Cereal::Field("Field name", 'A');

    // Writing a field
    myField->write(buffer); // Write the field to the buffer

    // Reading a field
    Cereal::Field* otherField = new Cereal::Field; // Create another field
    buffer.setOffset(0); // Move the buffer back to the beginning
    otherField->read(buffer); // Read the field from the buffer
    char data = otherField->getValue<char>(); // Now data should be equal to 'A'

    // Free dynamic memory
    delete myField;
    delete otherField;

    return 0;
}

C Sharp:

namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Cereal.Buffer buffer = new Cereal.Buffer(1024); // Create a buffer with 1024 bytes
            Cereal.Field myField = new Cereal.Field("Field name", 'A');

            // Writing a field
            myField.write(ref buffer); // Write the field to the buffer

            // Reading a field
            Cereal.Field otherField = new Cereal.Field(); // Create another field
            buffer.Position = 0; // Move the buffer back to the beginning
            otherField.read(ref buffer); // Read the field from the buffer
            char data = otherField.getChar(); // Now data should be equal to 'A'
        }
    }
}