Skip to content

Commit

Permalink
feat: to_vector & to_map
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Oct 27, 2023
1 parent 17cb823 commit 08e4c20
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> vec = value["list"].to_vector<int>();
for (auto&& i : vec) {
std::cout << i << std::endl;
}

// Output: "literals"
using namespace json::literals;
auto val = "{\"hi\":\"literals\"}"_json;
Expand Down
6 changes: 6 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> vec = value["list"].to_vector<int>();
for (auto&& i : vec) {
std::cout << i << std::endl;
}

// Output: "literals"
using namespace json::literals;
auto val = "{\"hi\":\"literals\"}"_json;
Expand Down
45 changes: 45 additions & 0 deletions include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ class basic_value
{
return format(indent, 0);
}
template <typename value_t>
std::vector<value_t> to_vector() const;
template <typename value_t>
std::map<string_t, value_t> to_map() const;

basic_value<string_t>& operator=(const basic_value<string_t>& rhs);
basic_value<string_t>& operator=(basic_value<string_t>&&) noexcept;
Expand Down Expand Up @@ -309,6 +313,8 @@ class basic_array
{
return format(indent, 0);
}
template <typename value_t>
std::vector<value_t> to_vector() const;

// Usage: get(key_1, key_2, ..., default_value);
template <typename... key_then_default_value_t>
Expand Down Expand Up @@ -419,6 +425,8 @@ class basic_object
{
return format(indent, 0);
}
template <typename value_t>
std::map<string_t, value_t> to_map() const;

// Usage: get(key_1, key_2, ..., default_value);
template <typename... key_then_default_value_t>
Expand Down Expand Up @@ -1146,6 +1154,20 @@ MEOJSON_INLINE string_t basic_value<string_t>::format(size_t indent, size_t inde
}
}

template <typename string_t>
template <typename value_t>
MEOJSON_INLINE std::vector<value_t> basic_value<string_t>::to_vector() const
{
return as_array().template to_vector<value_t>();
}

template <typename string_t>
template <typename value_t>
MEOJSON_INLINE std::map<string_t, value_t> basic_value<string_t>::to_map() const
{
return as_object().template to_map<value_t>();
}

template <typename string_t>
MEOJSON_INLINE basic_value<string_t>& basic_value<string_t>::operator=(const basic_value<string_t>& rhs)
{
Expand Down Expand Up @@ -1413,6 +1435,17 @@ MEOJSON_INLINE string_t basic_array<string_t>::format(size_t indent, size_t inde
return str;
}

template <typename string_t>
template <typename value_t>
MEOJSON_INLINE std::vector<value_t> basic_array<string_t>::to_vector() const
{
std::vector<value_t> result;
for (const auto& elem : _array_data) {
result.emplace_back(elem.template as<value_t>());
}
return result;
}

template <typename string_t>
template <typename value_t, typename... rest_keys_t>
MEOJSON_INLINE auto basic_array<string_t>::get_helper(const value_t& default_value, size_t pos,
Expand Down Expand Up @@ -1720,6 +1753,18 @@ MEOJSON_INLINE string_t basic_object<string_t>::format(size_t indent, size_t ind
return str;
}

template <typename string_t>
template <typename value_t>
MEOJSON_INLINE std::map<string_t, value_t> basic_object<string_t>::to_map() const
{
std::map<string_t, value_t> result;
for (const auto& [key, val] : _object_data) {
result.emplace(key, val.template as<value_t>());
}
return result;
}


template <typename string_t>
template <typename value_t, typename... rest_keys_t>
MEOJSON_INLINE auto basic_object<string_t>::get_helper(const value_t& default_value, const string_t& key,
Expand Down
6 changes: 6 additions & 0 deletions sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> vec = value["list"].to_vector<int>();
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;
Expand Down
4 changes: 4 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ bool serializing()
root["arr"].emplace(5);
root["arr"] += json::array { 6, 7 };

auto to_arr = root["arr"].to_vector<int>();

std::vector<int> vec = { 1, 2, 3, 4, 5 };
root["arr from vec"] = vec;

Expand All @@ -69,6 +71,8 @@ bool serializing()
};
root["obj from map"] = map;

auto to_map = root["obj from map"].to_map<int>();

std::vector<std::list<std::set<int>>> complex { { { 1, 2, 3 }, { 4, 5 } }, { { 6 }, { 7, 8 } } };
root["complex"] = json::serialize<false>(complex);

Expand Down

0 comments on commit 08e4c20

Please sign in to comment.