mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 13:02:13 +01:00
add example/simple.c
This commit is contained in:
parent
a0b82e39e1
commit
5d51129e65
37
example/simple.c
Normal file
37
example/simple.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <msgpack.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* msgpack::sbuffer is a simple buffer implementation. */
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
|
||||
/* serialize values into the buffer using msgpack_sbuffer_write callback function. */
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
|
||||
msgpack_pack_array(&pk, 3);
|
||||
msgpack_pack_int(&pk, 1);
|
||||
msgpack_pack_true(&pk);
|
||||
msgpack_pack_raw(&pk, 7);
|
||||
msgpack_pack_raw_body(&pk, "example", 7);
|
||||
|
||||
/* deserialize the buffer into msgpack_object instance. */
|
||||
/* deserialized object is valid during the msgpack_zone instance alive. */
|
||||
msgpack_zone mempool;
|
||||
msgpack_zone_init(&mempool, 2048);
|
||||
|
||||
msgpack_object deserialized;
|
||||
msgpack_unpack(sbuf.ptr, sbuf.size, NULL, &mempool, &deserialized);
|
||||
|
||||
/* print the deserialized object. */
|
||||
msgpack_object_print(stdout, deserialized);
|
||||
puts("");
|
||||
|
||||
msgpack_zone_destroy(&mempool);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user