mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-19 04:52:59 +01:00
Merge pull request #23 from ulikoehler/master
Add README syntax highlighting
This commit is contained in:
commit
e8ffe7ef96
46
README.md
46
README.md
@ -20,35 +20,37 @@ To use the library in your program, include msgpack.hpp header and link "msgpack
|
|||||||
|
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
```CPP
|
||||||
|
#include <msgpack.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <msgpack.hpp>
|
int main(void) {
|
||||||
#include <vector>
|
// This is target object.
|
||||||
|
std::vector<std::string> target;
|
||||||
|
target.push_back("Hello,");
|
||||||
|
target.push_back("World!");
|
||||||
|
|
||||||
int main(void) {
|
// Serialize it.
|
||||||
// This is target object.
|
msgpack::sbuffer sbuf; // simple buffer
|
||||||
std::vector<std::string> target;
|
msgpack::pack(&sbuf, target);
|
||||||
target.push_back("Hello,");
|
|
||||||
target.push_back("World!");
|
|
||||||
|
|
||||||
// Serialize it.
|
// Deserialize the serialized data.
|
||||||
msgpack::sbuffer sbuf; // simple buffer
|
msgpack::unpacked msg; // includes memory pool and deserialized object
|
||||||
msgpack::pack(&sbuf, target);
|
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
|
||||||
|
msgpack::object obj = msg.get();
|
||||||
|
|
||||||
// Deserialize the serialized data.
|
// Print the deserialized object to stdout.
|
||||||
msgpack::unpacked msg; // includes memory pool and deserialized object
|
std::cout << obj << std::endl; // ["Hello," "World!"]
|
||||||
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
|
|
||||||
msgpack::object obj = msg.get();
|
|
||||||
|
|
||||||
// Print the deserialized object to stdout.
|
// Convert the deserialized object to staticaly typed object.
|
||||||
std::cout << obj << std::endl; // ["Hello," "World!"]
|
std::vector<std::string> result;
|
||||||
|
obj.convert(&result);
|
||||||
|
|
||||||
// Convert the deserialized object to staticaly typed object.
|
// If the type is mismatched, it throws msgpack::type_error.
|
||||||
std::vector<std::string> result;
|
obj.as<int>(); // type is mismatched, msgpack::type_error is thrown
|
||||||
obj.convert(&result);
|
}
|
||||||
|
```
|
||||||
|
|
||||||
// If the type is mismatched, it throws msgpack::type_error.
|
|
||||||
obj.as<int>(); // type is mismatched, msgpack::type_error is thrown
|
|
||||||
}
|
|
||||||
|
|
||||||
API documents and other example codes are available at the [wiki.](http://redmine.msgpack.org/projects/msgpack/wiki)
|
API documents and other example codes are available at the [wiki.](http://redmine.msgpack.org/projects/msgpack/wiki)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user