Created
March 14, 2016 09:31
-
-
Save usagi/fc51de19829336ae5a0e to your computer and use it in GitHub Desktop.
cereal と boost::variant で何でもほいほい入れてシリアライズする例 ref: http://qiita.com/usagi/items/497a07909069187cd045
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cereal/cereal.hpp> | |
| #include <cereal/archives/json.hpp> | |
| #include <cereal/types/vector.hpp> | |
| #include <cereal/types/unordered_map.hpp> | |
| #include <cereal/types/string.hpp> | |
| #include <cereal/types/boost_variant.hpp> | |
| using variant_data_type = boost::make_recursive_variant | |
| < std::string | |
| , std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t | |
| , std::int8_t, std::int16_t, std::int32_t, std::int64_t | |
| , float, double, long double | |
| , bool | |
| , std::vector< boost::recursive_variant_ > | |
| , std::unordered_map< std::string, boost::recursive_variant_ > | |
| >::type; | |
| using variant_array_type = std::vector< variant_data_type >; | |
| using variant_map_type = std::unordered_map< std::string, variant_data_type >; | |
| auto main() -> int | |
| { | |
| using namespace std::literals::string_literals; | |
| variant_array_type array{ "hoge"s, true, false, 0x12345678 }; | |
| variant_map_type map{ { "key-1"s, "value"s }, { "key-2"s, 3.14f }, { "key-3"s, array } }; | |
| variant_data_type data = map; | |
| cereal::JSONOutputArchive( std::cout )( data ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "value0": { | |
| "which": 14, | |
| "data": [ | |
| { | |
| "key": "key-3", | |
| "value": { | |
| "which": 13, | |
| "data": [ | |
| { | |
| "which": 0, | |
| "data": "hoge" | |
| }, | |
| { | |
| "which": 12, | |
| "data": true | |
| }, | |
| { | |
| "which": 12, | |
| "data": false | |
| }, | |
| { | |
| "which": 7, | |
| "data": 305419896 | |
| } | |
| ] | |
| } | |
| }, | |
| { | |
| "key": "key-1", | |
| "value": { | |
| "which": 0, | |
| "data": "value" | |
| } | |
| }, | |
| { | |
| "key": "key-2", | |
| "value": { | |
| "which": 9, | |
| "data": 3.1400001049041748 | |
| } | |
| } | |
| ] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment