1#include <msgpack.h> 2#include <stdio.h> 3 4void print(char const* buf, unsigned int len) 5{ 6 size_t i = 0; 7 for(; i < len ; ++i) 8 printf("%02x ", 0xff & buf[i]); 9 printf("\n"); 10} 11 12int main(void) 13{ 14 msgpack_sbuffer sbuf; 15 msgpack_packer pk; 16 msgpack_zone mempool; 17 msgpack_object deserialized; 18 19 /* msgpack::sbuffer is a simple buffer implementation. */ 20 msgpack_sbuffer_init(&sbuf); 21 22 /* serialize values into the buffer using msgpack_sbuffer_write callback function. */ 23 msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); 24 25 msgpack_pack_array(&pk, 3); 26 msgpack_pack_int(&pk, 1); 27 msgpack_pack_true(&pk); 28 msgpack_pack_str(&pk, 7); 29 msgpack_pack_str_body(&pk, "example", 7); 30 31 print(sbuf.data, sbuf.size); 32 33 /* deserialize the buffer into msgpack_object instance. */ 34 /* deserialized object is valid during the msgpack_zone instance alive. */ 35 msgpack_zone_init(&mempool, 2048); 36 37 msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized); 38 39 /* print the deserialized object. */ 40 msgpack_object_print(stdout, deserialized); 41 puts(""); 42 43 msgpack_zone_destroy(&mempool); 44 msgpack_sbuffer_destroy(&sbuf); 45 46 return 0; 47} 48