Open dlandtaa opened 2 years ago
https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_object#conversion msgpack-c has 3 concepts. They are pack/unpack/convert. (Creating object from T is not related in this context)
Can I do something like this?
You can pack the only specific member variables but you need to call pack APIs directly.
See https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_packer#pack-manually
You can't unpack only specific element using msgpack::unpack() function. You need to unpack all elements.
If you really want to only specific element from MessagePack formatted byte stream, then you can use visitor APIs. https://github.com/msgpack/msgpack-c/wiki/v2_0_cpp_visitor It is like SAX api.
If msgpack::object contains MAP like {"a":1,"b":2}
, then you can get only "a":1
using direct access.
But no key serach are provided.
So first, convert to std::map<std::string, int> m
and then get m["a"]
is easy way.
Here's the way to somehow ignore certain fields:
struct C
{
int a;
int b;
int c;
MSGPACK_DEFINE_MAP(a, b, c);
};
msgpack::sbuffer buff;
msgpack::packer<msgpack::sbuffer> packer(buff);
struct C_fieldless
{
int a;
msgpack::object b, c; // the fields that are ignored
MSGPACK_DEFINE_MAP(a, b, c);
};
msgpack::object_handle handle;
msgpack::unpack(handle, buff.data(), buff.size());
auto fieldless = handle.as<C_fieldsless>();
Is there anyway to control which fields are packed or unpacked on a call by call basis?
Can I do something like this?