Zakero's C++ Header Libraries
A collection of reusable C++ libraries
Public Types | Public Member Functions | List of all members
zakero::MessagePack Class Reference

Object and Data Serialization. More...

Public Types

enum class  DataType
 The supported C++ data-types.
 

Public Member Functions

void append (const bool, size_t *const =nullptr) noexcept
 Append a boolean value. More...
 
void append (const double, size_t *=nullptr) noexcept
 Append a 64-bit floating point value. More...
 
void append (const float, size_t *=nullptr) noexcept
 Append a 32-bit floating point value. More...
 
void append (const int64_t, size_t *const =nullptr) noexcept
 Append a signed integer value. More...
 
void append (const std::string_view, size_t *=nullptr) noexcept
 Append a string. More...
 
void append (const std::vector< uint8_t > &, size_t *=nullptr) noexcept
 Append a binary data. More...
 
void append (const uint64_t, size_t *const =nullptr) noexcept
 Append an unsigned integer value. More...
 
void appendNull (size_t *const =nullptr) noexcept
 Append a "null" value. More...
 
void clear () noexcept
 Remove all data from the MessagePack.
 
void deserialize (const std::vector< uint8_t > &) noexcept
 Deserialize MessagePack data. More...
 
Objectobject (const size_t index) noexcept
 Access a data object. More...
 
std::vector< uint8_t > serialize () noexcept
 Serialize MessagePack data. More...
 
size_t size () noexcept
 Get the number of data objects. More...
 

Detailed Description

This class implements the MessagePack specification. Usage is simple:

To Serialize:

  1. Create a MessagePack instance
  2. Add data
  3. Serialize the data into a std::vector

To Deserialize:

  1. Create a MessagePack instance
  2. Pass a std::vector to be deserialized
  3. Access the data objects

Member Function Documentation

◆ append() [1/7]

void zakero::MessagePack::append ( const bool  value,
size_t * const  index = nullptr 
)
noexcept

The boolean value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
message_pack.append(true, &index);
Object and Data Serialization.
Definition: Zakero_MessagePack.h:221
void append(const bool, size_t *const =nullptr) noexcept
Append a boolean value.
Definition: Zakero_MessagePack.h:511
Parameters
valueThe value to add
indexThe index

◆ append() [2/7]

void zakero::MessagePack::append ( const double  value,
size_t *  index = nullptr 
)
noexcept

The value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
message_pack.append(double(0), &index);
Parameters
valueThe value to add
indexThe index

◆ append() [3/7]

void zakero::MessagePack::append ( const float  value,
size_t *  index = nullptr 
)
noexcept

The value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
message_pack.append(float(0), &index);
Parameters
valueThe value to add
indexThe index

◆ append() [4/7]

void zakero::MessagePack::append ( const int64_t  value,
size_t * const  index = nullptr 
)
noexcept

The value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
message_pack.append(int64_t(0), &index);
Parameters
valueThe value to add
indexThe index

◆ append() [5/7]

void zakero::MessagePack::append ( const std::string_view  value,
size_t *  index = nullptr 
)
noexcept

The value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
message_pack.append("foo", &index);
Parameters
valueThe value to add
indexThe index

◆ append() [6/7]

void zakero::MessagePack::append ( const std::vector< uint8_t > &  value,
size_t *  index = nullptr 
)
noexcept

The value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
std::vector<uint8_t> binary_data(1024, 0);
message_pack.append(binary_data, &index);
Parameters
valueThe value to add
indexThe index

◆ append() [7/7]

void zakero::MessagePack::append ( const uint64_t  value,
size_t * const  index = nullptr 
)
noexcept

The value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
message_pack.append(uint64_t(0), &index);
Parameters
valueThe value to add
indexThe index

◆ appendNull()

void zakero::MessagePack::appendNull ( size_t * const  index = nullptr)
noexcept

A "null" value will be appended to the current contents of the MessagePack data. If index is not nullptr, the index location of the "null" value will be stored in index.

Example
zakero::MessagePack message_pack;
size_t index;
message_pack.appendNull(&index);
void appendNull(size_t *const =nullptr) noexcept
Append a "null" value.
Definition: Zakero_MessagePack.h:1991
Parameters
indexThe index

◆ deserialize()

void zakero::MessagePack::deserialize ( const std::vector< uint8_t > &  data)
noexcept

The packed vector of data will be converted into data objects that can be easily accessed and used.

Example
std::vector<uint8_t> command_result = get_reply(command_id);
zakero::MessagePack message_pack;
message_pack.deserialize(command_result);
constexpr size_t error_index = 1;
constexpr size_t error_code_index = 2;
if(message_pack.object(error_index).boolean == true)
{
writeError(message_pack.object(error_code_index).int64_);
}
Object & object(const size_t index) noexcept
Access a data object.
Definition: Zakero_MessagePack.h:2081
void deserialize(const std::vector< uint8_t > &) noexcept
Deserialize MessagePack data.
Definition: Zakero_MessagePack.h:2148
Todo:
Add an index parameter so that last parsed data location will be available.
Todo:
Add error codes.
  • Error_None
  • Error_Bad_Format_Type
  • Error_Incomplete
Todo:
Can the contents of the for-loop be rewritten as a switch-statement?
Parameters
dataThe packed data

◆ object()

MessagePack::Object & zakero::MessagePack::object ( const size_t  index)
noexcept

After data has been added to the MessagePack, that data can still be access by using its index value. The data object's type will be the C++ datatype and not the MessagePack format type.

The data-type of the object can be changed as needed. However, be sure to update the type to match the new value.

Example
zakero::MessagePack message_pack;
size_t mp_foo;
message_pack.append(int64_t(0), &mp_foo);
size_t mp_bar;
message_pack.append(int64_t(0), &mp_bar);
int64_t val = rand();
message_pack.object(mp_foo).int64_ = val;
if(val & 1)
{
// Change to string
message_pack.object(mp_bar).type = zakero::MessagePack::DataType::String
message_pack.object(mp_bar).string = "That's odd...";
}
else
{
// Change to boolean
message_pack.object(mp_bar).type = zakero::MessagePack::DataType::Bool
message_pack.object(mp_bar).bool_ = true;
}
Returns
The data object.
Parameters
indexThe index of the data object.

◆ serialize()

std::vector< uint8_t > zakero::MessagePack::serialize ( )
noexcept

The contents of the MessagePack will be packed into the returned std::vector.

Example
zakero::MessagePack message_pack;
message_pack.append(command_id);
message_pack.append(true);
message_pack.append(error_code);
std::vector<uint8_t> result = message_pack.serialize();
reply(host_ip, result);
std::vector< uint8_t > serialize() noexcept
Serialize MessagePack data.
Definition: Zakero_MessagePack.h:2387
Returns
The packed data.

◆ size()

size_t zakero::MessagePack::size ( )
noexcept

Using this method will provide a count of all the data objects that the MessagePack contains. Arrays and Maps each count as one data object. So, if the MessagePack contains Arrays or Maps, the contents of those objects is not counted.

Returns
The number of data objects.

The documentation for this class was generated from the following file: