22
22
23
23
#include < dpp/export.h>
24
24
#include < dpp/json_fwd.h>
25
- #include < dpp/json.h>
26
25
#include < dpp/json_interface.h>
27
26
28
27
namespace dpp {
@@ -48,21 +47,25 @@ void DPP_EXPORT set_snowflake_not_null(const nlohmann::json* j, const char *keyn
48
47
*/
49
48
void DPP_EXPORT set_snowflake_array_not_null (const nlohmann::json* j, const char *keyname, std::vector<class snowflake > &v);
50
49
50
+ /* *
51
+ * @brief Applies a function to each element of a json array.
52
+ * @param j nlohmann::json instance to retrieve value from
53
+ * @param key key name to check for the values
54
+ * @param fn function to apply to each element
55
+ */
56
+ void DPP_EXPORT for_each_json (nlohmann::json* parent, std::string_view key, std::function<void (const nlohmann::json*)> fn);
57
+
51
58
/* * @brief Sets an array of objects from a json field value, if defined, else does nothing
52
59
* @tparam T The class of which the array consists of. Must be derived from dpp::json_interface
53
60
* @param j nlohmann::json instance to retrieve value from
54
61
* @param keyname key name to check for the values
55
62
* @param v Value to change
56
63
*/
57
- template <class T > std:: enable_if_t <std::is_base_of_v<json_interface<T>, T>, void > set_object_array_not_null (nlohmann::json* j, const char *keyname , std::vector<T> & v) {
64
+ template <class T > void set_object_array_not_null (nlohmann::json* j, std::string_view key , std::vector<T>& v) {
58
65
v.clear ();
59
- auto k = j->find (keyname);
60
- if (k != j->end () && !k->is_null ()) {
61
- v.reserve (j->at (keyname).size ());
62
- for (auto &obj : j->at (keyname)) {
63
- v.emplace_back (T ().fill_from_json (&obj));
64
- }
65
- }
66
+ for_each_json (j, key, [&v](nlohmann::json* elem) {
67
+ v.push_back (T{}.fill_from_json (elem));
68
+ });
66
69
}
67
70
68
71
/* * @brief Returns a string from a json field value, if defined, else returns an empty string.
0 commit comments