Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,37 @@ To use the library in your program, include msgpack.hpp header and link "msgpack


## Example

#include <msgpack.hpp>
#include <vector>
```CPP
#include <msgpack.hpp>
#include <vector>

int main(void) {
// This is target object.
std::vector<std::string> target;
target.push_back("Hello,");
target.push_back("World!");
int main(void) {
// This is target object.
std::vector<std::string> target;
target.push_back("Hello,");
target.push_back("World!");

// Serialize it.
msgpack::sbuffer sbuf; // simple buffer
msgpack::pack(&sbuf, target);
// Serialize it.
msgpack::sbuffer sbuf; // simple buffer
msgpack::pack(&sbuf, target);

// Deserialize the serialized data.
msgpack::unpacked msg; // includes memory pool and deserialized object
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();
// Deserialize the serialized data.
msgpack::unpacked msg; // includes memory pool and deserialized object
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();

// Print the deserialized object to stdout.
std::cout << obj << std::endl; // ["Hello," "World!"]
// Print the deserialized object to stdout.
std::cout << obj << std::endl; // ["Hello," "World!"]

// Convert the deserialized object to staticaly typed object.
std::vector<std::string> result;
obj.convert(&result);
// Convert the deserialized object to staticaly typed object.
std::vector<std::string> result;
obj.convert(&result);

// If the type is mismatched, it throws msgpack::type_error.
obj.as<int>(); // type is mismatched, msgpack::type_error is thrown
}
// 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)

Expand Down