From 08e4c20d04f7e48e0b0e8f05d582cf0f9d3c3161 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 27 Oct 2023 10:09:40 +0800 Subject: [PATCH] feat: to_vector & to_map --- README.md | 6 ++++++ README_en.md | 6 ++++++ include/json.hpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ sample/sample.cpp | 6 ++++++ test/main.cpp | 4 ++++ 5 files changed, 67 insertions(+) diff --git a/README.md b/README.md index f57839e..7aa00cb 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,12 @@ void parsing() auto opt_v = value.find("not_exists"); std::cout << "Did we find the \"not_exists\"? " << opt_v.has_value() << std::endl; + // Output: 1, 2, 3 + std::vector vec = value["list"].to_vector(); + for (auto&& i : vec) { + std::cout << i << std::endl; + } + // Output: "literals" using namespace json::literals; auto val = "{\"hi\":\"literals\"}"_json; diff --git a/README_en.md b/README_en.md index f90b5be..ca9b602 100644 --- a/README_en.md +++ b/README_en.md @@ -111,6 +111,12 @@ void parsing() auto opt_v = value.find("not_exists"); std::cout << "Did we find the \"not_exists\"? " << opt_v.has_value() << std::endl; + // Output: 1, 2, 3 + std::vector vec = value["list"].to_vector(); + for (auto&& i : vec) { + std::cout << i << std::endl; + } + // Output: "literals" using namespace json::literals; auto val = "{\"hi\":\"literals\"}"_json; diff --git a/include/json.hpp b/include/json.hpp index a19b3e1..25172ae 100644 --- a/include/json.hpp +++ b/include/json.hpp @@ -194,6 +194,10 @@ class basic_value { return format(indent, 0); } + template + std::vector to_vector() const; + template + std::map to_map() const; basic_value& operator=(const basic_value& rhs); basic_value& operator=(basic_value&&) noexcept; @@ -309,6 +313,8 @@ class basic_array { return format(indent, 0); } + template + std::vector to_vector() const; // Usage: get(key_1, key_2, ..., default_value); template @@ -419,6 +425,8 @@ class basic_object { return format(indent, 0); } + template + std::map to_map() const; // Usage: get(key_1, key_2, ..., default_value); template @@ -1146,6 +1154,20 @@ MEOJSON_INLINE string_t basic_value::format(size_t indent, size_t inde } } +template +template +MEOJSON_INLINE std::vector basic_value::to_vector() const +{ + return as_array().template to_vector(); +} + +template +template +MEOJSON_INLINE std::map basic_value::to_map() const +{ + return as_object().template to_map(); +} + template MEOJSON_INLINE basic_value& basic_value::operator=(const basic_value& rhs) { @@ -1413,6 +1435,17 @@ MEOJSON_INLINE string_t basic_array::format(size_t indent, size_t inde return str; } +template +template +MEOJSON_INLINE std::vector basic_array::to_vector() const +{ + std::vector result; + for (const auto& elem : _array_data) { + result.emplace_back(elem.template as()); + } + return result; +} + template template MEOJSON_INLINE auto basic_array::get_helper(const value_t& default_value, size_t pos, @@ -1720,6 +1753,18 @@ MEOJSON_INLINE string_t basic_object::format(size_t indent, size_t ind return str; } +template +template +MEOJSON_INLINE std::map basic_object::to_map() const +{ + std::map result; + for (const auto& [key, val] : _object_data) { + result.emplace(key, val.template as()); + } + return result; +} + + template template MEOJSON_INLINE auto basic_object::get_helper(const value_t& default_value, const string_t& key, diff --git a/sample/sample.cpp b/sample/sample.cpp index 2efab5b..814df00 100644 --- a/sample/sample.cpp +++ b/sample/sample.cpp @@ -102,6 +102,12 @@ bool parsing() auto opt_v = value.find("not_exists"); std::cout << "Did we find the \"not_exists\"? " << opt_v.has_value() << std::endl; + // Output: 1, 2, 3 + std::vector vec = value["list"].to_vector(); + for (auto&& i : vec) { + std::cout << i << std::endl; + } + using namespace json::literals; json::value val = "{\"hi\":\"literals\"}"_json; std::cout << val["hi"] << std::endl; diff --git a/test/main.cpp b/test/main.cpp index d94c9c9..462c872 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -57,6 +57,8 @@ bool serializing() root["arr"].emplace(5); root["arr"] += json::array { 6, 7 }; + auto to_arr = root["arr"].to_vector(); + std::vector vec = { 1, 2, 3, 4, 5 }; root["arr from vec"] = vec; @@ -69,6 +71,8 @@ bool serializing() }; root["obj from map"] = map; + auto to_map = root["obj from map"].to_map(); + std::vector>> complex { { { 1, 2, 3 }, { 4, 5 } }, { { 6 }, { 7, 8 } } }; root["complex"] = json::serialize(complex);