msgpack-c
msgpack-c copied to clipboard
zbuffer example
Hello! I tried to use the zbuffer to compress my data, but I can't seem to make it work. I modified one of the example programs and replaced stringstream with zbuffer. What am I doing wrong? Also, maybe there could be an example somewhere showing how to do this (assuming I didn't overlook it)?
#include <msgpack.hpp>
#include <msgpack/zbuffer.hpp>
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <cassert>
using namespace std;
class base1 {
public:
int a;
MSGPACK_DEFINE(a);
};
class base2 {
public:
int b;
MSGPACK_DEFINE(b);
};
class test: public base1, public base2 {
public:
int c;
std::string d;
// You can choose any order. It is represented to the msgpack array elements order.
MSGPACK_DEFINE(d, MSGPACK_BASE(base2), c, MSGPACK_BASE(base1));
};
int main(){
test a = {1,3,5,"hello"};
printf("a: %d %d %d %s\n", a.a, a.b, a.c, a.d.c_str());
// stringstream ss;
msgpack::zbuffer ss;
msgpack::pack(ss, a);
size_t offset = 0;
cout << "offset: " << offset << endl;
{
// msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size(), offset);
msgpack::object_handle oh = msgpack::unpack(ss.data(), ss.size(), offset);
msgpack::object obj = oh.get();
cout << obj << endl;
test b;
obj.convert(b);
assert(a.a == b.a);
assert(a.b == b.b);
assert(a.c == b.c);
assert(a.d == b.d);
// assert(obj.as<decltype(a)>() == a);
}
return 0;
}
Output:
a: 1 3 5 hello
offset: 0
120
libc++abi.dylib: terminating with uncaught exception of type msgpack::v1::type_error: std::bad_cast
Abort trap: 6
I am using osx and my compiler is:
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
@walchko , msgpack-c unpack function cannot recognize the buffer is z-compressed or not. So you need to decompress the buffer by yourself before calling unpack.
// decompress the compressed buffer ss.data(), ss.size()
// std::string decompressed = /* deflate using zlib API */;
// msgpack::object_handle oh = msgpack::unpack(ss.data(), ss.size(), offset);
msgpack::object_handle oh = msgpack::unpack(decompressed.data(), decompressed.size(), offset);
I think that the following link is helpful.
https://stackoverflow.com/questions/27529570/simple-zlib-c-string-compression-and-decompression