get__ValueType_const.cpp
Go to the documentation of this file.
00001 #include <json.hpp>
00002 #include <unordered_map>
00003 
00004 using json = nlohmann::json;
00005 
00006 int main()
00007 {
00008     // create a JSON value with different types
00009     json json_types =
00010     {
00011         {"boolean", true},
00012         {
00013             "number", {
00014                 {"integer", 42},
00015                 {"floating-point", 17.23}
00016             }
00017         },
00018         {"string", "Hello, world!"},
00019         {"array", {1, 2, 3, 4, 5}},
00020         {"null", nullptr}
00021     };
00022 
00023     // use explicit conversions
00024     auto v1 = json_types["boolean"].get<bool>();
00025     auto v2 = json_types["number"]["integer"].get<int>();
00026     auto v3 = json_types["number"]["integer"].get<short>();
00027     auto v4 = json_types["number"]["floating-point"].get<float>();
00028     auto v5 = json_types["number"]["floating-point"].get<int>();
00029     auto v6 = json_types["string"].get<std::string>();
00030     auto v7 = json_types["array"].get<std::vector<short>>();
00031     auto v8 = json_types.get<std::unordered_map<std::string, json>>();
00032 
00033     // print the conversion results
00034     std::cout << v1 << '\n';
00035     std::cout << v2 << ' ' << v3 << '\n';
00036     std::cout << v4 << ' ' << v5 << '\n';
00037     std::cout << v6 << '\n';
00038 
00039     for (auto i : v7)
00040     {
00041         std::cout << i << ' ';
00042     }
00043     std::cout << "\n\n";
00044 
00045     for (auto i : v8)
00046     {
00047         std::cout << i.first << ": " << i.second << '\n';
00048     }
00049 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02