mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-09 05:27:26 +02:00
c: msgpack_pack_object
This commit is contained in:
@@ -5,27 +5,33 @@
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// this is target object
|
||||
msgpack::type::tuple<int, bool, std::string> src(1, true, "example");
|
||||
|
||||
// any classes that implements write(const char*,size_t) can be a buffer
|
||||
// serialize the object into the buffer.
|
||||
// any classes that implements write(const char*,size_t) can be a buffer.
|
||||
std::stringstream buffer;
|
||||
msgpack::pack(buffer, src);
|
||||
|
||||
// send the buffer ...
|
||||
buffer.seekg(0);
|
||||
|
||||
// deserialize the buffer into msgpack::object type
|
||||
msgpack::zone mempool;
|
||||
// deserialize the buffer into msgpack::object instance.
|
||||
std::string str(buffer.str());
|
||||
msgpack::object deserialized =
|
||||
msgpack::unpack(str.data(), str.size(), mempool);
|
||||
|
||||
// msgpack::object supports ostream
|
||||
// deserialized object is valid during the msgpack::zone instance alive.
|
||||
msgpack::zone mempool;
|
||||
|
||||
msgpack::object deserialized;
|
||||
msgpack::unpack(str.data(), str.size(), NULL, &mempool, &deserialized);
|
||||
|
||||
// msgpack::object supports ostream.
|
||||
std::cout << deserialized << std::endl;
|
||||
|
||||
// convert msgpack::object type into the original type
|
||||
// convert msgpack::object instance into the original type.
|
||||
// if the type is mismatched, it throws msgpack::type_error exception.
|
||||
msgpack::type::tuple<int, bool, std::string> dst;
|
||||
msgpack::convert(dst, deserialized);
|
||||
deserialized.convert(&dst);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -84,7 +84,6 @@ struct fwriter {
|
||||
void write(const char* buf, size_t buflen)
|
||||
{
|
||||
size_t count = fwrite(buf, buflen, 1, m_fp);
|
||||
//if(fwrite(buf, buflen, 1, m_fp) < 1) {
|
||||
if(count < 1) {
|
||||
std::cout << buflen << std::endl;
|
||||
std::cout << count << std::endl;
|
||||
|
Reference in New Issue
Block a user