C++ Cereal: serialize C-style array

8.2k Views Asked by At

Can/How do you serialize an array using the cereal library.

I.e.

void save(Archive & ar, const unsigned int version) const
{
    unsigned int l  = g1_size_bin(g,POINT_COMPRESS);
    uint8_t data[l];
    memset(data, 0, l);
    g1_write_bin(data, l, g,POINT_COMPRESS);
    ar(l);
    ar(data); // what should be here
}

That doesn't work (nor would I expect it too). Nor does

ar(cereal::binary_data(data,l)); 

(which I would think would work, since it looks like the boost code one would use), which produces a compilation error :

/usr/local/include/cereal/cereal.hpp:79:17: note: candidate template ignored: substitution failure : variably modified type 'unsigned char (&)[l]' cannot be used as a template argument BinaryData binary_data( T && data, size_t size )

Nor does

ar.saveBinaryValue(data,l);

Since that method only appears to be supported for XML/Json and I want a binary archive.

1

There are 1 best solutions below

5
On BEST ANSWER

cereal::binary_data is the correct construct to use in this case assuming you want a binary representation of the POD array. This will only work for archives that support binary_data (binary and portable_binary). binary_data doesn't work for the text based archives because it is seen as an optimization for a more generic serialization method - see how vector is serialized for an example of this.

Anyway, here's a working example of serializing a C style array:

#include <cereal/archives/binary.hpp>
#include <iostream>

int main()
{
  std::stringstream ss;

  {
    cereal::BinaryOutputArchive ar(ss);
    std::uint8_t data[] = {1, 2, 3};
    ar( cereal::binary_data( data, sizeof(std::uint8_t) * 3 ) );
  }

  {
    cereal::BinaryInputArchive ar(ss);
    std::uint8_t data[3];
    ar( cereal::binary_data( data, sizeof(std::uint8_t) * 3 ) );

    for( int i : data )
      std::cout << i << " ";
  }

  return 0;
}

If you wanted to serialize a C style array to a text based archive, or if your array wasn't over POD types, you would need to iterate over each object and serialize it individually.