diff --git a/include/common/array.hpp b/include/common/array.hpp index d7a5479..c82f636 100644 --- a/include/common/array.hpp +++ b/include/common/array.hpp @@ -89,6 +89,8 @@ class basic_array bool all() const; template typename collection_t = std::vector> collection_t as_collection() const; + template + std::array as_collection() const; // Usage: get(key_1, key_2, ..., default_value); template @@ -155,6 +157,12 @@ class basic_array return as_collection(); } + template + explicit operator std::array() const + { + return as_collection(); + } + template < typename jsonization_t, std::enable_if_t<_utils::has_from_json_in_member::value, bool> = @@ -339,6 +347,21 @@ inline collection_t basic_array::as_collection() const return result; } +template +template +inline std::array basic_array::as_collection() const +{ + if (size() != Size) { + throw exception("Wrong array size"); + } + + std::array result; + for (size_t i = 0; i < Size; ++i) { + result.at(i) = _array_data.at(i).template as(); + } + return result; +} + template template inline auto basic_array::get(key_then_default_value_t&&... keys_then_default_value) const diff --git a/include/common/utils.hpp b/include/common/utils.hpp index 9939b50..fa90c31 100644 --- a/include/common/utils.hpp +++ b/include/common/utils.hpp @@ -69,6 +69,16 @@ constexpr bool is_collection = false; template constexpr bool is_collection = is_container && !is_map; +template +constexpr bool is_std_array = false; +template +constexpr bool is_std_array> = true; + +template +constexpr size_t std_array_size = 0; +template +constexpr size_t std_array_size> = Size; + template class has_to_json_in_member { diff --git a/include/common/value.hpp b/include/common/value.hpp index 9d7665a..a482b18 100644 --- a/include/common/value.hpp +++ b/include/common/value.hpp @@ -181,6 +181,8 @@ class basic_value template typename collection_t = std::vector> collection_t as_collection() const; + template + std::array as_collection() const; template typename map_t = std::map> map_t as_map() const; @@ -276,6 +278,12 @@ class basic_value return as_collection(); } + template + explicit operator std::array() const + { + return as_collection(); + } + template < typename value_t, template typename map_t = std::map, @@ -491,6 +499,10 @@ inline bool basic_value::is() const noexcept else if constexpr (std::is_same_v, value_t>) { return is_array(); } + else if constexpr (_utils::is_std_array) { + return is_array() && all() + && as_array().size() == _utils::std_array_size; + } else if constexpr (_utils::is_collection) { return is_array() && all(); } @@ -922,6 +934,13 @@ inline collection_t basic_value::as_collection() const return as_array().template as_collection(); } +template +template +inline std::array basic_value::as_collection() const +{ + return as_array().template as_collection(); +} + template template typename map_t> inline map_t basic_value::as_map() const diff --git a/test/serializing_test.cpp b/test/serializing_test.cpp index 0c92c40..0ec9b77 100644 --- a/test/serializing_test.cpp +++ b/test/serializing_test.cpp @@ -262,8 +262,9 @@ bool jsonizing() std::string str3; std::vector vec; std::unordered_map>>> map; + std::array arr; - MEO_JSONIZATION(str1, str2, str3, vec, map); + MEO_JSONIZATION(str1, str2, str3, vec, map, arr); }; MyStruct mine; @@ -313,5 +314,27 @@ bool jsonizing() return false; } + std::array stdarr { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + json::value jarr = stdarr; + std::array new_std_arr; + if (jarr.is>()) { + new_std_arr = jarr.as_collection(); + } + else { + std::cerr << "error std::array" << std::endl; + return false; + } + + if (new_std_arr.back() != 10) { + std::cerr << "error std::array value" << std::endl; + return false; + } + + if (jarr.is>()) { + std::cerr << "error std::array size" << std::endl; + return false; + + } + return true; }