Class Serialization

Class Documentation

class Serialization

Simple serializer which serials every given type into the following format: (The type needs to be convertable into a string via cxx::convert::toString) LENGTH:DATALENGTH:DATA… Example: Serializes “hello”, 123, 123.01 into 5:hello3:1236:123.01.

auto serial = cxx::Serialization::create("fuu", 123, 12.12f, 'c');
std::cout << serial.toString() << std::endl;

std::string v1;
int v2;
float v3;
char v4;

if ( serial.extract(v1, v2, v3, v4) ) {} // succeeds since every value is convertable

if ( serial.getNth(0, v2) ) {} // fails since "fuu" is not an integer

// if you'd like to write a serializable class they need to have a CTor
// with a cxx::Serialization argument and an operator cxx::Serialization
class Fuu {
    public:
        Fuu(const cxx::Serialization & s) {
            if ( !s.Extract(v1, v2, v3) ) {} // error handling
        }
        operator cxx::Serialization() const {
            return cxx::Serialization::Create(v1, v2, v3);
        }
    private:
        int v1 = 123;
        char v2 = 'c';
        std::string v3 = "hello world";

};

Public Types

enum class Error

This is an error which can be used for cxx::expected on a custom deserialization when extract fails.

Values:

enumerator DESERIALIZATION_FAILED

indicates a failed deserialization

Public Functions

explicit Serialization(const std::string &value) noexcept

Creates a serialization object from a given raw serialization.

Parameters:

value[in] string of serialized data

std::string toString() const noexcept

string conversion operator, returns the raw serialized string

Returns:

serialized string

operator std::string() const noexcept

string conversion operator, returns the raw serialized string

Returns:

serialized string

template<typename T, typename ...Targs>
bool extract(T &t, Targs&... args) const noexcept

Extracts the values from the serialization and writes them into the the given args, if one value is not convertable it returns false (e.g. convert “hello” to an integer) It also returns false if the underlying serialization string has a wrong syntax.

Parameters:
  • t[in] reference where the first value in the serialization will be stored in

  • args[in] reference where the remainding values in the serialization will be stored in

Returns:

true if extraction of all values was successfull, otherwise false

template<typename T>
bool getNth(const unsigned int index, T &t) const noexcept

Extracts the value at index and writes it into t. If the conversion failed it returns false It also returns false if the underlying serialization string has a wrong syntax.

Parameters:
  • index[in] index to the value which should be extracted

  • t[in] variable where the value should be stored

Returns:

true if extraction was successful, otherwise false

Public Static Functions

template<typename ...Targs>
static Serialization create(const Targs&... args) noexcept

Create Serialization if every arguments is convertable to string via cxx::convert::toString, this means if the argument is either a pod (plain old data) type or is convertable to string (operator std::string())

Parameters:

args[in] list of string convertable data

Returns:

Serialization object which contains the serialized data