README 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. `msgpack` for C/C++
  2. ===================
  3. Version 3.1.1 [![Build Status](https://travis-ci.org/msgpack/msgpack-c.svg?branch=master)](https://travis-ci.org/msgpack/msgpack-c) [![Build status](https://ci.appveyor.com/api/projects/status/8kstcgt79qj123mw/branch/master?svg=true)](https://ci.appveyor.com/project/redboltz/msgpack-c/branch/master)
  4. It's like JSON but smaller and faster.
  5. Overview
  6. --------
  7. [MessagePack](http://msgpack.org/) is an efficient binary serialization
  8. format, which lets you exchange data among multiple languages like JSON,
  9. except that it's faster and smaller. Small integers are encoded into a
  10. single byte and short strings require only one extra byte in
  11. addition to the strings themselves.
  12. Example
  13. -------
  14. In C:
  15. ```c
  16. #include <msgpack.h>
  17. #include <stdio.h>
  18. int main(void)
  19. {
  20. /* msgpack::sbuffer is a simple buffer implementation. */
  21. msgpack_sbuffer sbuf;
  22. msgpack_sbuffer_init(&sbuf);
  23. /* serialize values into the buffer using msgpack_sbuffer_write callback function. */
  24. msgpack_packer pk;
  25. msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  26. msgpack_pack_array(&pk, 3);
  27. msgpack_pack_int(&pk, 1);
  28. msgpack_pack_true(&pk);
  29. msgpack_pack_str(&pk, 7);
  30. msgpack_pack_str_body(&pk, "example", 7);
  31. /* deserialize the buffer into msgpack_object instance. */
  32. /* deserialized object is valid during the msgpack_zone instance alive. */
  33. msgpack_zone mempool;
  34. msgpack_zone_init(&mempool, 2048);
  35. msgpack_object deserialized;
  36. msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);
  37. /* print the deserialized object. */
  38. msgpack_object_print(stdout, deserialized);
  39. puts("");
  40. msgpack_zone_destroy(&mempool);
  41. msgpack_sbuffer_destroy(&sbuf);
  42. return 0;
  43. }
  44. ```
  45. See [`QUICKSTART-C.md`](./QUICKSTART-C.md) for more details.
  46. In C++:
  47. ```c++
  48. #include <msgpack.hpp>
  49. #include <string>
  50. #include <iostream>
  51. #include <sstream>
  52. int main(void)
  53. {
  54. msgpack::type::tuple<int, bool, std::string> src(1, true, "example");
  55. // serialize the object into the buffer.
  56. // any classes that implements write(const char*,size_t) can be a buffer.
  57. std::stringstream buffer;
  58. msgpack::pack(buffer, src);
  59. // send the buffer ...
  60. buffer.seekg(0);
  61. // deserialize the buffer into msgpack::object instance.
  62. std::string str(buffer.str());
  63. msgpack::object_handle oh =
  64. msgpack::unpack(str.data(), str.size());
  65. // deserialized object is valid during the msgpack::object_handle instance is alive.
  66. msgpack::object deserialized = oh.get();
  67. // msgpack::object supports ostream.
  68. std::cout << deserialized << std::endl;
  69. // convert msgpack::object instance into the original type.
  70. // if the type is mismatched, it throws msgpack::type_error exception.
  71. msgpack::type::tuple<int, bool, std::string> dst;
  72. deserialized.convert(dst);
  73. // or create the new instance
  74. msgpack::type::tuple<int, bool, std::string> dst2 =
  75. deserialized.as<msgpack::type::tuple<int, bool, std::string> >();
  76. return 0;
  77. }
  78. ```
  79. See [`QUICKSTART-CPP.md`](./QUICKSTART-CPP.md) for more details.
  80. Usage
  81. -----
  82. ### C++ Header Only Library
  83. When you use msgpack on C++, you can just add
  84. msgpack-c/include to your include path:
  85. g++ -I msgpack-c/include your_source_file.cpp
  86. If you want to use C version of msgpack, you need to build it. You can
  87. also install the C and C++ versions of msgpack.
  88. ### Building and Installing
  89. #### Install from git repository
  90. ##### Using the Terminal (CLI)
  91. You will need:
  92. - `gcc >= 4.1.0`
  93. - `cmake >= 2.8.0`
  94. C and C++03:
  95. $ git clone https://github.com/msgpack/msgpack-c.git
  96. $ cd msgpack-c
  97. $ cmake .
  98. $ make
  99. $ sudo make install
  100. If you want to setup C++11 or C++17 version of msgpack instead,
  101. execute the following commands:
  102. $ git clone https://github.com/msgpack/msgpack-c.git
  103. $ cd msgpack-c
  104. $ cmake -DMSGPACK_CXX[11|17]=ON .
  105. $ sudo make install
  106. `MSGPACK_CXX[11|17]` flags are not affected to installing files. Just switching test cases. All files are installed in every settings.
  107. When you use the C part of `msgpack-c`, you need to build and link the library. By default, both static/shared libraries are built. If you want to build only static library, set `BUILD_SHARED_LIBS=OFF` to cmake. If you want to build only shared library, set `BUILD_SHARED_L
  108. #### GUI on Windows
  109. Clone msgpack-c git repository.
  110. $ git clone https://github.com/msgpack/msgpack-c.git
  111. or using GUI git client.
  112. e.g.) tortoise git https://code.google.com/p/tortoisegit/
  113. 1. Launch [cmake GUI client](http://www.cmake.org/cmake/resources/software.html).
  114. 2. Set 'Where is the source code:' text box and 'Where to build
  115. the binaries:' text box.
  116. 3. Click 'Configure' button.
  117. 4. Choose your Visual Studio version.
  118. 5. Click 'Generate' button.
  119. 6. Open the created msgpack.sln on Visual Studio.
  120. 7. Build all.
  121. ### Documentation
  122. You can get additional information including the tutorial on the
  123. [wiki](https://github.com/msgpack/msgpack-c/wiki).
  124. Contributing
  125. ------------
  126. `msgpack-c` is developed on GitHub at [msgpack/msgpack-c](https://github.com/msgpack/msgpack-c).
  127. To report an issue or send a pull request, use the
  128. [issue tracker](https://github.com/msgpack/msgpack-c/issues).
  129. Here's the list of [great contributors](https://github.com/msgpack/msgpack-c/graphs/contributors).
  130. License
  131. -------
  132. `msgpack-c` is licensed under the Boost Software License, Version 1.0. See
  133. the [`LICENSE_1_0.txt`](./LICENSE_1_0.txt) file for details.