{"id":379580,"date":"2024-06-25T03:02:46","date_gmt":"2024-06-25T03:02:46","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=379580"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=379580","title":{"rendered":"<span>\u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 C++ \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 Cereal<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/eef\/d81\/e79\/eefd81e79771424ffbadc03cfe0107e1.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eef\/d81\/e79\/eefd81e79771424ffbadc03cfe0107e1.png\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Cereal \u0432 C++, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043d\u0435 \u0442\u0435\u0440\u044f\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p><strong>Cereal<\/strong> \u2014 \u044d\u0442\u043e <strong>\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u0430\u044f<\/strong> <strong>\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430<\/strong> \u0434\u043b\u044f C++, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f <strong>\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/strong> <strong>\u0434\u0430\u043d\u043d\u044b\u0445<\/strong>. \u041e\u043d\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 XML \u0438 JSON. \u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0441\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 C++ \u0438 \u0438\u043c\u0435\u0435\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438. \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a <a href=\"https:\/\/www.boost.org\/\">Boost<\/a>, Cereal \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u0438 \u0438\u043c\u0435\u0435\u0442 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441, \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u044e\u0437\u0435\u0440\u0430\u043c Boost.<\/p>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c<\/h4>\n<p>C\u043a\u0430\u0447\u0430\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0441 <a href=\"https:\/\/github.com\/USCiLab\/cereal\">GitHub<\/a>:<\/p>\n<pre><code>git clone https:\/\/github.com\/USCiLab\/cereal.git<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 <code>include\/cereal<\/code> \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u0443 \u043f\u0430\u043f\u043a\u0443 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0443\u044e \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430. <\/p>\n<p><em>Cereal \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f!<\/em><\/p>\n<p>Cereal \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C++11. \u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>GCC 4.7.3 \u0438\u043b\u0438 \u043d\u043e\u0432\u0435\u0435<\/p>\n<\/li>\n<li>\n<p>Clang 3.3 \u0438\u043b\u0438 \u043d\u043e\u0432\u0435\u0435<\/p>\n<\/li>\n<li>\n<p>MSVC 2013 \u0438\u043b\u0438 \u043d\u043e\u0432\u0435\u0435<\/p>\n<\/li>\n<\/ul>\n<h3>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441<\/h3>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 serialize<\/strong><\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>serialize<\/code> &#8212; \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u043a\u0430\u043a\u0438\u0435 \u0447\u043b\u0435\u043d\u044b \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b. \u041e\u0431\u044b\u0447\u043d\u043e \u0435\u0451 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"cpp\">struct MyRecord {     uint8_t x, y;     float z;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(x, y, z);     } };<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>serialize<\/code> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0430\u0440\u0445\u0438\u0432\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0435\u043c\u0443 \u0447\u043b\u0435\u043d\u044b \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 save \u0438 load<\/strong><\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435, \u043c\u043e\u0436\u043d\u043e \u044e\u0437\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>save<\/code> \u0438 <code>load<\/code>. \u041c\u0430\u0441\u0442\u0445\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u043e\u043f. \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0438\u043b\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"cpp\">struct SomeData {     int32_t id;     std::shared_ptr&lt;std::unordered_map&lt;uint32_t, MyRecord>> data;      template &lt;class Archive>     void save(Archive&amp; ar) const {         ar(data);     }      template &lt;class Archive>     void load(Archive&amp; ar) {         static int32_t idGen = 0;         id = idGen++;         ar(data);     } };<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>save<\/code> \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c <code>const<\/code>, \u0442.\u043a \u043e\u043d\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 save_minimal \u0438 load_minimal<\/strong><\/p>\n<p>\u042d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0438. \u041f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u043e\u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0445 \u0430\u0440\u0445\u0438\u0432\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">struct MyData {     double d;      template &lt;class Archive>     double save_minimal(Archive const&amp;) const {         return d;     }      template &lt;class Archive>     void load_minimal(Archive const&amp;, double const&amp; value) {         d = value;     } };<\/code><\/pre>\n<p><strong>\u0423\u043c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438<\/strong><\/p>\n<p>Cereal \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0443\u043c\u043d\u044b\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 <code>std::shared_ptr<\/code> \u0438 <code>std::unique_ptr<\/code>. \u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u0443\u043c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438, \u0431\u0435\u0437 \u043b\u0448\u043d\u0438\u0445 \u0443\u0441\u0438\u043b\u0438\u0439:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/types\/memory.hpp>  struct DataHolder {     std::shared_ptr&lt;MyRecord> record;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(record);     } };<\/code><\/pre>\n<p><strong>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0415\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>cereal::base_class<\/code> \u0438 <code>cereal::virtual_base_class<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/types\/base_class.hpp>  struct Base {     int x;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(x);     } };  struct Derived : public Base {     int y;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(cereal::base_class&lt;Base>(this), y);     } };<\/code><\/pre>\n<p><strong>\u0410\u0440\u0445\u0438\u0432\u044b \u0438 \u0438\u0445 \u0442\u0438\u043f\u044b<\/strong><\/p>\n<p>Cereal \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 \u0430\u0440\u0445\u0438\u0432\u043e\u0432: \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435, XML \u0438 JSON \u0430\u0440\u0445\u0438\u0432\u044b. \u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0444\u043e\u0440\u043c\u0430\u0442\u0430\u0445.<\/p>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u0430\u0440\u0445\u0438\u0432\u044b<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/binary.hpp>  std::ofstream os(\"data.cereal\", std::ios::binary); cereal::BinaryOutputArchive archive(os); archive(someData);<\/code><\/pre>\n<p>XML \u0430\u0440\u0445\u0438\u0432\u044b<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/xml.hpp>  std::ofstream os(\"data.xml\"); cereal::XMLOutputArchive archive(os); archive(someData);<\/code><\/pre>\n<p>JSON \u0430\u0440\u0445\u0438\u0432\u044b<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/json.hpp>  std::ofstream os(\"data.json\"); cereal::JSONOutputArchive archive(os); archive(someData);<\/code><\/pre>\n<p><strong>\u0412\u0435\u0440\u0441\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432<\/strong><\/p>\n<p>\u0414\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0441\u0438\u044f\u043c\u0438 \u0442\u0438\u043f\u043e\u0432 \u0435\u0441\u0442\u044c \u043c\u0430\u043a\u0440\u043e\u0441 <code>CEREAL_CLASS_VERSION<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u0441\u0438\u044e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/types\/base_class.hpp> #include &lt;cereal\/types\/polymorphic.hpp>  struct MyType {     int x;      template &lt;class Archive>     void serialize(Archive&amp; ar, const std::uint32_t version) {         ar(x);     } };  CEREAL_CLASS_VERSION(MyType, 1);<\/code><\/pre>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/h3>\n<h4>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h4>\n<p>\u0427\u0430\u0441\u0442\u043e Cereal \u044e\u0437\u0430\u044e\u0442 \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440, \u0433\u0434\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 JSON \u0444\u0430\u0439\u043b\u0435, \u0438 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0435\u0451 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/json.hpp> #include &lt;cereal\/types\/map.hpp> #include &lt;cereal\/types\/string.hpp> #include &lt;fstream> #include &lt;iostream>  struct AppConfig {     std::map&lt;std::string, std::string> settings;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(settings);     } };  void saveConfig(const AppConfig&amp; config, const std::string&amp; filename) {     std::ofstream os(filename);     cereal::JSONOutputArchive archive(os);     archive(config); }  AppConfig loadConfig(const std::string&amp; filename) {     std::ifstream is(filename);     cereal::JSONInputArchive archive(is);     AppConfig config;     archive(config);     return config; }  int main() {     AppConfig config;     config.settings[\"username\"] = \"admin\";     config.settings[\"theme\"] = \"dark\";      saveConfig(config, \"config.json\");      AppConfig loadedConfig = loadConfig(\"config.json\");     std::cout &lt;&lt; \"Username: \" &lt;&lt; loadedConfig.settings[\"username\"] &lt;&lt; \"\\n\";     std::cout &lt;&lt; \"Theme: \" &lt;&lt; loadedConfig.settings[\"theme\"] &lt;&lt; \"\\n\";      return 0; }<\/code><\/pre>\n<h4>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438\u0433\u0440\u044b<\/h4>\n<p>\u0412 \u0438\u0433\u0440\u0443\u0448\u043a\u0430\u0445 \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0433\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u0438\u0433\u0440\u043e\u043a \u043c\u043e\u0433 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0441 \u0442\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430, \u0433\u0434\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f. \u041c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0433\u0440\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Cereal:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/binary.hpp> #include &lt;cereal\/types\/vector.hpp> #include &lt;fstream>  struct GameState {     int level;     int score;     std::vector&lt;int> inventory;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(level, score, inventory);     } };  void saveGameState(const GameState&amp; state, const std::string&amp; filename) {     std::ofstream os(filename, std::ios::binary);     cereal::BinaryOutputArchive archive(os);     archive(state); }  GameState loadGameState(const std::string&amp; filename) {     std::ifstream is(filename, std::ios::binary);     cereal::BinaryInputArchive archive(is);     GameState state;     archive(state);     return state; }  int main() {     GameState state{3, 4500, {1, 2, 3}};     saveGameState(state, \"game.sav\");      GameState loadedState = loadGameState(\"game.sav\");     std::cout &lt;&lt; \"Level: \" &lt;&lt; loadedState.level &lt;&lt; \"\\n\";     std::cout &lt;&lt; \"Score: \" &lt;&lt; loadedState.score &lt;&lt; \"\\n\";     std::cout &lt;&lt; \"Inventory: \";     for (int item : loadedState.inventory) {         std::cout &lt;&lt; item &lt;&lt; \" \";     }     std::cout &lt;&lt; \"\\n\";      return 0; }<\/code><\/pre>\n<h4> \u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430  <\/h4>\n<p>\u0412 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445 \u0438 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u043e \u0441\u0435\u0442\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/binary.hpp> #include &lt;cereal\/types\/string.hpp> #include &lt;cereal\/types\/vector.hpp> #include &lt;sstream> #include &lt;iostream>  struct Message {     std::string sender;     std::string content;     std::vector&lt;int> attachments;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(sender, content, attachments);     } };  std::string serializeMessage(const Message&amp; message) {     std::ostringstream oss;     cereal::BinaryOutputArchive archive(oss);     archive(message);     return oss.str(); }  Message deserializeMessage(const std::string&amp; data) {     std::istringstream iss(data);     cereal::BinaryInputArchive archive(iss);     Message message;     archive(message);     return message; }  int main() {     Message msg = {\"Alice\", \"Hello, Bob!\", {1, 2, 3}};     std::string serializedData = serializeMessage(msg);      Message deserializedMsg = deserializeMessage(serializedData);     std::cout &lt;&lt; \"Sender: \" &lt;&lt; deserializedMsg.sender &lt;&lt; \", Content: \" &lt;&lt; deserializedMsg.content &lt;&lt; std::endl;      return 0; }<\/code><\/pre>\n<hr\/>\n<p><em>\u041a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u043d\u0430 \u0421++ \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u0443\u044e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443? \u041e\u0431 \u044d\u0442\u043e\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0435\u0442 \u0410\u0440\u0441\u0435\u043d\u0438\u0439 \u0427\u0435\u0440\u0435\u043d\u043a\u043e\u0432. <\/em><a href=\"https:\/\/otus.pw\/2b3A\/\"><em>\u0412\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0443\u0440\u043e\u043a\u0435 \u00ab\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 Conan \u0434\u043b\u044f \u0421++\u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432\u00bb \u043e\u0442 OTUS<\/em><\/a><em>.<\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/821195\/\"> https:\/\/habr.com\/ru\/articles\/821195\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Cereal \u0432 C++, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043d\u0435 \u0442\u0435\u0440\u044f\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p><strong>Cereal<\/strong> \u2014 \u044d\u0442\u043e <strong>\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u0430\u044f<\/strong> <strong>\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430<\/strong> \u0434\u043b\u044f C++, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f <strong>\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/strong> <strong>\u0434\u0430\u043d\u043d\u044b\u0445<\/strong>. \u041e\u043d\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 XML \u0438 JSON. \u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0441\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 C++ \u0438 \u0438\u043c\u0435\u0435\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438. \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a <a href=\"https:\/\/www.boost.org\/\">Boost<\/a>, Cereal \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u0438 \u0438\u043c\u0435\u0435\u0442 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441, \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u044e\u0437\u0435\u0440\u0430\u043c Boost.<\/p>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c<\/h4>\n<p>C\u043a\u0430\u0447\u0430\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0441 <a href=\"https:\/\/github.com\/USCiLab\/cereal\">GitHub<\/a>:<\/p>\n<pre><code>git clone https:\/\/github.com\/USCiLab\/cereal.git<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 <code>include\/cereal<\/code> \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u0443 \u043f\u0430\u043f\u043a\u0443 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0443\u044e \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430. <\/p>\n<p><em>Cereal \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f!<\/em><\/p>\n<p>Cereal \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 C++11. \u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u0432:<\/p>\n<ul>\n<li>\n<p>GCC 4.7.3 \u0438\u043b\u0438 \u043d\u043e\u0432\u0435\u0435<\/p>\n<\/li>\n<li>\n<p>Clang 3.3 \u0438\u043b\u0438 \u043d\u043e\u0432\u0435\u0435<\/p>\n<\/li>\n<li>\n<p>MSVC 2013 \u0438\u043b\u0438 \u043d\u043e\u0432\u0435\u0435<\/p>\n<\/li>\n<\/ul>\n<h3>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441<\/h3>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 serialize<\/strong><\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>serialize<\/code> &#8212; \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u043a\u0430\u043a\u0438\u0435 \u0447\u043b\u0435\u043d\u044b \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b. \u041e\u0431\u044b\u0447\u043d\u043e \u0435\u0451 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"cpp\">struct MyRecord {     uint8_t x, y;     float z;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(x, y, z);     } };<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>serialize<\/code> \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0430\u0440\u0445\u0438\u0432\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0435\u043c\u0443 \u0447\u043b\u0435\u043d\u044b \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 save \u0438 load<\/strong><\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435, \u043c\u043e\u0436\u043d\u043e \u044e\u0437\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>save<\/code> \u0438 <code>load<\/code>. \u041c\u0430\u0441\u0442\u0445\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u043e\u043f. \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0438\u043b\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"cpp\">struct SomeData {     int32_t id;     std::shared_ptr&lt;std::unordered_map&lt;uint32_t, MyRecord>> data;      template &lt;class Archive>     void save(Archive&amp; ar) const {         ar(data);     }      template &lt;class Archive>     void load(Archive&amp; ar) {         static int32_t idGen = 0;         id = idGen++;         ar(data);     } };<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>save<\/code> \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c <code>const<\/code>, \u0442.\u043a \u043e\u043d\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p><strong>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 save_minimal \u0438 load_minimal<\/strong><\/p>\n<p>\u042d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044f \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0438. \u041f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u043e\u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0445 \u0430\u0440\u0445\u0438\u0432\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">struct MyData {     double d;      template &lt;class Archive>     double save_minimal(Archive const&amp;) const {         return d;     }      template &lt;class Archive>     void load_minimal(Archive const&amp;, double const&amp; value) {         d = value;     } };<\/code><\/pre>\n<p><strong>\u0423\u043c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438<\/strong><\/p>\n<p>Cereal \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0443\u043c\u043d\u044b\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 <code>std::shared_ptr<\/code> \u0438 <code>std::unique_ptr<\/code>. \u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u0443\u043c\u043d\u044b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438, \u0431\u0435\u0437 \u043b\u0448\u043d\u0438\u0445 \u0443\u0441\u0438\u043b\u0438\u0439:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/types\/memory.hpp>  struct DataHolder {     std::shared_ptr&lt;MyRecord> record;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(record);     } };<\/code><\/pre>\n<p><strong>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0415\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>cereal::base_class<\/code> \u0438 <code>cereal::virtual_base_class<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/types\/base_class.hpp>  struct Base {     int x;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(x);     } };  struct Derived : public Base {     int y;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(cereal::base_class&lt;Base>(this), y);     } };<\/code><\/pre>\n<p><strong>\u0410\u0440\u0445\u0438\u0432\u044b \u0438 \u0438\u0445 \u0442\u0438\u043f\u044b<\/strong><\/p>\n<p>Cereal \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u043e\u0432 \u0430\u0440\u0445\u0438\u0432\u043e\u0432: \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0435, XML \u0438 JSON \u0430\u0440\u0445\u0438\u0432\u044b. \u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0444\u043e\u0440\u043c\u0430\u0442\u0430\u0445.<\/p>\n<p>\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u0430\u0440\u0445\u0438\u0432\u044b<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/binary.hpp>  std::ofstream os(\"data.cereal\", std::ios::binary); cereal::BinaryOutputArchive archive(os); archive(someData);<\/code><\/pre>\n<p>XML \u0430\u0440\u0445\u0438\u0432\u044b<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/xml.hpp>  std::ofstream os(\"data.xml\"); cereal::XMLOutputArchive archive(os); archive(someData);<\/code><\/pre>\n<p>JSON \u0430\u0440\u0445\u0438\u0432\u044b<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/json.hpp>  std::ofstream os(\"data.json\"); cereal::JSONOutputArchive archive(os); archive(someData);<\/code><\/pre>\n<p><strong>\u0412\u0435\u0440\u0441\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432<\/strong><\/p>\n<p>\u0414\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0441\u0438\u044f\u043c\u0438 \u0442\u0438\u043f\u043e\u0432 \u0435\u0441\u0442\u044c \u043c\u0430\u043a\u0440\u043e\u0441 <code>CEREAL_CLASS_VERSION<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u0441\u0438\u044e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/types\/base_class.hpp> #include &lt;cereal\/types\/polymorphic.hpp>  struct MyType {     int x;      template &lt;class Archive>     void serialize(Archive&amp; ar, const std::uint32_t version) {         ar(x);     } };  CEREAL_CLASS_VERSION(MyType, 1);<\/code><\/pre>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/h3>\n<h4>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h4>\n<p>\u0427\u0430\u0441\u0442\u043e Cereal \u044e\u0437\u0430\u044e\u0442 \u0434\u043b\u044f \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440, \u0433\u0434\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 JSON \u0444\u0430\u0439\u043b\u0435, \u0438 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0435\u0451 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/json.hpp> #include &lt;cereal\/types\/map.hpp> #include &lt;cereal\/types\/string.hpp> #include &lt;fstream> #include &lt;iostream>  struct AppConfig {     std::map&lt;std::string, std::string> settings;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(settings);     } };  void saveConfig(const AppConfig&amp; config, const std::string&amp; filename) {     std::ofstream os(filename);     cereal::JSONOutputArchive archive(os);     archive(config); }  AppConfig loadConfig(const std::string&amp; filename) {     std::ifstream is(filename);     cereal::JSONInputArchive archive(is);     AppConfig config;     archive(config);     return config; }  int main() {     AppConfig config;     config.settings[\"username\"] = \"admin\";     config.settings[\"theme\"] = \"dark\";      saveConfig(config, \"config.json\");      AppConfig loadedConfig = loadConfig(\"config.json\");     std::cout &lt;&lt; \"Username: \" &lt;&lt; loadedConfig.settings[\"username\"] &lt;&lt; \"\\n\";     std::cout &lt;&lt; \"Theme: \" &lt;&lt; loadedConfig.settings[\"theme\"] &lt;&lt; \"\\n\";      return 0; }<\/code><\/pre>\n<h4>\u0421\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438\u0433\u0440\u044b<\/h4>\n<p>\u0412 \u0438\u0433\u0440\u0443\u0448\u043a\u0430\u0445 \u0431\u044b\u043b\u043e \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0433\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u0438\u0433\u0440\u043e\u043a \u043c\u043e\u0433 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0441 \u0442\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430, \u0433\u0434\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f. \u041c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0433\u0440\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Cereal:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/binary.hpp> #include &lt;cereal\/types\/vector.hpp> #include &lt;fstream>  struct GameState {     int level;     int score;     std::vector&lt;int> inventory;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(level, score, inventory);     } };  void saveGameState(const GameState&amp; state, const std::string&amp; filename) {     std::ofstream os(filename, std::ios::binary);     cereal::BinaryOutputArchive archive(os);     archive(state); }  GameState loadGameState(const std::string&amp; filename) {     std::ifstream is(filename, std::ios::binary);     cereal::BinaryInputArchive archive(is);     GameState state;     archive(state);     return state; }  int main() {     GameState state{3, 4500, {1, 2, 3}};     saveGameState(state, \"game.sav\");      GameState loadedState = loadGameState(\"game.sav\");     std::cout &lt;&lt; \"Level: \" &lt;&lt; loadedState.level &lt;&lt; \"\\n\";     std::cout &lt;&lt; \"Score: \" &lt;&lt; loadedState.score &lt;&lt; \"\\n\";     std::cout &lt;&lt; \"Inventory: \";     for (int item : loadedState.inventory) {         std::cout &lt;&lt; item &lt;&lt; \" \";     }     std::cout &lt;&lt; \"\\n\";      return 0; }<\/code><\/pre>\n<h4> \u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430  <\/h4>\n<p>\u0412 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445 \u0438 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u043e \u0441\u0435\u0442\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">#include &lt;cereal\/archives\/binary.hpp> #include &lt;cereal\/types\/string.hpp> #include &lt;cereal\/types\/vector.hpp> #include &lt;sstream> #include &lt;iostream>  struct Message {     std::string sender;     std::string content;     std::vector&lt;int> attachments;      template &lt;class Archive>     void serialize(Archive&amp; ar) {         ar(sender, content, attachments);     } };  std::string serializeMessage(const Message&amp; message) {     std::ostringstream oss;     cereal::BinaryOutputArchive archive(oss);     archive(message);     return oss.str(); }  Message deserializeMessage(const std::string&amp; data) {     std::istringstream iss(data);     cereal::BinaryInputArchive archive(iss);     Message message;     archive(message);     return message; }  int main() {     Message msg = {\"Alice\", \"Hello, Bob!\", {1, 2, 3}};     std::string serializedData = serializeMessage(msg);      Message deserializedMsg = deserializeMessage(serializedData);     std::cout &lt;&lt; \"Sender: \" &lt;&lt; deserializedMsg.sender &lt;&lt; \", Content: \" &lt;&lt; deserializedMsg.content &lt;&lt; std::endl;      return 0; }<\/code><\/pre>\n<hr\/>\n<p><em>\u041a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u043d\u0430 \u0421++ \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u0443\u044e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443? \u041e\u0431 \u044d\u0442\u043e\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0435\u0442 \u0410\u0440\u0441\u0435\u043d\u0438\u0439 \u0427\u0435\u0440\u0435\u043d\u043a\u043e\u0432. <\/em><a href=\"https:\/\/otus.pw\/2b3A\/\"><em>\u0412\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c\u0441\u044f \u043d\u0430 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0443\u0440\u043e\u043a\u0435 \u00ab\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 Conan \u0434\u043b\u044f \u0421++\u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432\u00bb \u043e\u0442 OTUS<\/em><\/a><em>.<\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/821195\/\"> https:\/\/habr.com\/ru\/articles\/821195\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-379580","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/379580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=379580"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/379580\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=379580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=379580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=379580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}