27 rapidjson::Document m_document;
34 explicit json(
bool root_type_array =
false)
37 m_document.SetArray();
39 m_document.SetObject();
50 m_document.Parse(json_str.c_str(), json_str.size());
58 : m_document(std::move(rhs.m_document))
68 m_document = std::move(rhs.m_document);
79 m_document.CopyFrom(rhs.m_document, m_document.GetAllocator());
128 auto member = m_document.FindMember(name.c_str());
129 return member != m_document.MemberEnd()
130 && member->value.IsInt()
131 ? member->value.GetInt() : def_val;
143 auto member = m_document.FindMember(name.c_str());
144 return member != m_document.MemberEnd()
145 && member->value.IsInt64()
146 ? member->value.GetInt64() : def_val;
158 auto member = m_document.FindMember(name.c_str());
159 return member != m_document.MemberEnd()
160 && member->value.IsString()
161 ? member->value.GetString() : def_val;
173 auto member = m_document.FindMember(name.c_str());
174 return member != m_document.MemberEnd()
175 && member->value.IsDouble()
176 ? member->value.GetDouble() : def_val;
188 auto member = m_document.FindMember(name.c_str());
189 return member != m_document.MemberEnd()
190 && member->value.IsBool()
191 ? member->value.GetBool() : def_val;
203 auto member = m_document.FindMember(name.c_str());
204 return member != m_document.MemberEnd()
205 && member->value.IsUint()
206 ? member->value.GetUint() : def_val;
218 auto member = m_document.FindMember(name.c_str());
219 return member != m_document.MemberEnd()
220 && member->value.IsUint64()
221 ? member->value.GetUint64() : def_val;
230 bool try_parse(
const std::string &json_str, std::string &error)
noexcept
234 m_document.Parse(json_str.c_str(), json_str.size());
237 catch (
const std::exception &e)
253 rapidjson::StringBuffer sb;
256 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
257 m_document.Accept(writer);
262 rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
263 m_document.Accept(writer);
265 return sb.GetString();
276 json &
append(
const std::vector<std::map<std::string, T>> &arr_items)
278 if (m_document.IsArray() && !arr_items.empty())
280 for (
auto &arr_item: arr_items)
282 if (!arr_item.empty())
284 rapidjson::Value my_value(rapidjson::kObjectType);
285 for (
auto &obj_item: arr_item)
288 rapidjson::Value(obj_item.first.c_str(), m_document.GetAllocator()),
289 rapidjson::Value(obj_item.second),
290 m_document.GetAllocator()
293 m_document.PushBack(my_value.Move(), m_document.GetAllocator());
306 json &
append(
const std::vector<std::map<std::string, std::string>> &arr_items)
308 if (m_document.IsArray() && !arr_items.empty())
310 for (
auto &arr_item: arr_items)
312 if (arr_item.empty())
315 rapidjson::Value my_value(rapidjson::kObjectType);
316 for (
auto &obj_item: arr_item)
319 rapidjson::Value(obj_item.first.c_str(), m_document.GetAllocator()),
320 rapidjson::Value(obj_item.second.c_str(), m_document.GetAllocator()),
321 m_document.GetAllocator()
324 m_document.PushBack(my_value.Move(), m_document.GetAllocator());
338 json &
append(
const std::vector<std::map<std::string, std::vector<T>>> &arr_items)
340 if (m_document.IsArray() && !arr_items.empty())
342 for (
auto &arr_item: arr_items)
344 if (arr_item.empty())
347 rapidjson::Value my_object(rapidjson::kObjectType);
348 for (
auto &map_item: arr_item)
350 if (map_item.first.empty())
353 rapidjson::Value my_arr_value(rapidjson::kArrayType);
354 for (
auto &one_value: map_item.second)
356 my_arr_value.PushBack(rapidjson::Value(one_value).Move(), m_document.GetAllocator());
359 rapidjson::Value(map_item.first.c_str(), m_document.GetAllocator()),
361 m_document.GetAllocator()
364 m_document.PushBack(my_object, m_document.GetAllocator());
376 json &
append(
const std::vector<std::map<std::string, std::vector<std::string>>> &arr_items)
378 if (m_document.IsArray() && !arr_items.empty())
380 for (
auto &arr_item: arr_items)
382 if (arr_item.empty())
385 rapidjson::Value my_object(rapidjson::kObjectType);
386 for (
auto &map_item: arr_item)
388 if (map_item.first.empty())
391 rapidjson::Value my_arr_value(rapidjson::kArrayType);
392 for (
auto &one_value: map_item.second)
394 my_arr_value.PushBack(
395 rapidjson::Value(one_value.c_str(), m_document.GetAllocator()).Move(),
396 m_document.GetAllocator());
399 rapidjson::Value(map_item.first.c_str(), m_document.GetAllocator()),
401 m_document.GetAllocator()
404 m_document.PushBack(my_object, m_document.GetAllocator());
424 if (m_document.IsObject() && !m_document.HasMember(key.c_str()))
426 m_document.AddMember(
427 rapidjson::Value(key.c_str(), m_document.GetAllocator()),
428 rapidjson::Value(val),
429 m_document.GetAllocator());
431 else if (m_document.IsArray())
433 std::map<std::string, T> mp = {
434 std::make_pair(key, val)
436 std::vector<std::map<std::string, T>> vec = {mp};
455 if (m_document.IsObject() && !m_document.HasMember(key.c_str()))
457 m_document.AddMember(
458 rapidjson::Value(key.c_str(), m_document.GetAllocator()),
459 rapidjson::Value(val.c_str(), m_document.GetAllocator()),
460 m_document.GetAllocator()
463 else if (m_document.IsArray())
465 std::map<std::string, std::string> mp = {
466 std::make_pair(key, val)
484 json &
append(
const std::string &key,
const std::vector<T> &val)
488 if (m_document.IsObject() && !m_document.HasMember(key.c_str()))
490 rapidjson::Value value(rapidjson::kArrayType);
493 for (
auto &arr_item: val)
494 value.PushBack(rapidjson::Value(arr_item).Move(), m_document.GetAllocator());
496 m_document.AddMember(
497 rapidjson::Value(key.c_str(), m_document.GetAllocator()),
499 m_document.GetAllocator());
501 else if (m_document.IsArray())
503 std::map<std::string, std::vector<T>> mp = {
504 std::make_pair(key, val)
506 std::vector<
decltype(mp)> vec = {mp};
521 json &
append(
const std::string &key,
const std::vector<std::string> &val)
525 if (m_document.IsObject() && !m_document.HasMember(key.c_str()))
527 rapidjson::Value value(rapidjson::kArrayType);
530 for (
auto &arr_item: val)
532 rapidjson::Value(arr_item.c_str(), m_document.GetAllocator()),
533 m_document.GetAllocator()
536 m_document.AddMember(
537 rapidjson::Value(key.c_str(), m_document.GetAllocator()),
539 m_document.GetAllocator());
541 else if (m_document.IsArray())
543 std::map<std::string, std::vector<std::string>> mp = {
544 std::make_pair(key, val)
546 std::vector<
decltype(mp)> vec = {mp};
564 if (!kv_items.empty())
566 if (m_document.IsObject())
568 for (
auto &item: kv_items)
570 m_document.AddMember(rapidjson::Value(item.first.c_str(), m_document.GetAllocator()),
571 rapidjson::Value(item.second),
572 m_document.GetAllocator());
575 else if (m_document.IsArray())
592 json &
append(
const std::map<std::string, std::string> &kv_items)
594 if (!kv_items.empty())
596 if (m_document.IsObject())
598 for (
auto &item: kv_items)
600 m_document.AddMember(
601 rapidjson::Value(item.first.c_str(), m_document.GetAllocator()),
602 rapidjson::Value(item.second.c_str(), m_document.GetAllocator()),
603 m_document.GetAllocator()
607 else if (m_document.IsArray())
609 std::vector<std::map<std::string, std::string>> v{kv_items};
625 json &
append(
const std::map<std::string, std::vector<T>> &kv_items)
627 if (!kv_items.empty())
629 if (m_document.IsObject())
631 for (
auto &item: kv_items)
633 rapidjson::Value value(rapidjson::kArrayType);
634 for (
const T &arr_item: item.second)
636 value.PushBack(rapidjson::Value(arr_item).Move(), m_document.GetAllocator());
639 m_document.AddMember(rapidjson::Value(item.first.c_str(), m_document.GetAllocator()), value,
640 m_document.GetAllocator());
643 else if (m_document.IsArray())
658 json &
append(
const std::map<std::string, std::vector<std::string>> &kv_items)
660 if (!kv_items.empty())
662 if (m_document.IsObject())
664 for (
auto &item: kv_items)
666 rapidjson::Value value(rapidjson::kArrayType);
667 if (item.second.empty())
669 for (
auto &arr_item: item.second)
671 value.PushBack(rapidjson::Value(
673 m_document.GetAllocator()
675 m_document.GetAllocator());
678 m_document.AddMember(rapidjson::Value(item.first.c_str(), m_document.GetAllocator()), value,
679 m_document.GetAllocator());
682 else if (m_document.IsArray())
684 std::vector<std::map<std::string, std::vector<std::string>>> v{kv_items};
697 CORELINK_CPP_ATTR_MAYBE_UNUSED
700 if (!key.empty() && m_document.IsObject())
702 if (m_document.HasMember(key.c_str()))
703 m_document.RemoveMember(key.c_str());
714 if (!m_document.Empty())
provides a wrapper over JSON libraries. Corelink supports a wrapper over RapidJSON,...
Definition json.hpp:23
CORELINK_CPP_ATTR_MAYBE_UNUSED json & remove(const std::string &key)
Definition json.hpp:698
json & append(const std::string &key, const std::vector< T > &val)
Append a key value pair to the root JSON object where the value type is an array of native type value...
Definition json.hpp:484
CORELINK_CPP_ATTR_MAYBE_UNUSED json & clear()
Definition json.hpp:712
json & append(const std::vector< std::map< std::string, std::string > > &arr_items)
Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair...
Definition json.hpp:306
json & append(const std::string &key, const std::string &val)
Append a key value pair to the root JSON object where the value type is a string.
Definition json.hpp:451
std::string get_str(in< std::string > name, in< std::string > def_val="") const
get the value associated with the key as std::string. Note that this function can throw if the underl...
Definition json.hpp:156
json & append(const std::vector< std::map< std::string, std::vector< T > > > &arr_items)
Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair...
Definition json.hpp:338
int64_t get_int64(in< std::string > name, int64_t def_val=0) const
get the value associated with the key as an integer. Note that this function can throw if the underly...
Definition json.hpp:141
json & append(const std::map< std::string, T > &kv_items)
Append multiple key value pair to the root JSON object where the value type is a native type.
Definition json.hpp:562
json & append(const std::map< std::string, std::vector< T > > &kv_items)
Append multiple key value pair to the root JSON object where the value type is an array of native typ...
Definition json.hpp:625
double get_double(in< std::string > name, double def_val=0.0) const
get the value associated with the key as double. Note that this function can throw if the underlying ...
Definition json.hpp:171
json & append(const std::string &key, const std::vector< std::string > &val)
Append a key value pair to the root JSON object where the value type is an array of strings.
Definition json.hpp:521
uint32_t get_uint(in< std::string > name, uint32_t def_val=false) const
get the value associated with the key as an unsigned integer. Note that this function can throw if th...
Definition json.hpp:201
json & operator=(rvref< json > rhs) noexcept
Move assignment operator.
Definition json.hpp:66
json(rvref< json > rhs) noexcept
Move constructor.
Definition json.hpp:57
bool try_parse(const std::string &json_str, std::string &error) noexcept
Definition json.hpp:230
json & append(const std::vector< std::map< std::string, std::vector< std::string > > > &arr_items)
Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair...
Definition json.hpp:376
json & operator=(clvref< json > rhs)
Copy assignment operator.
Definition json.hpp:77
uint64_t get_uint64(in< std::string > name, uint64_t def_val=false) const
get the value associated with the key as an unsigned integer. Note that this function can throw if th...
Definition json.hpp:216
json & append(const std::map< std::string, std::string > &kv_items)
Append multiple key value pair to the root JSON object where the value type is value of string type.
Definition json.hpp:592
rapidjson::Document & operator()()
() operator overload.
Definition json.hpp:102
std::string to_string(bool pretty_print=false) const
Definition json.hpp:251
clvref< rapidjson::Document > operator()() const
() operator overload for const objects
Definition json.hpp:114
json(in< std::string > json_str)
Definition json.hpp:48
bool get_bool(in< std::string > name, bool def_val=false) const
get the value associated with the key as bool. Note that this function can throw if the underlying va...
Definition json.hpp:186
json(clvref< json > rhs)
Copy constructor.
Definition json.hpp:87
json & append(const std::map< std::string, std::vector< std::string > > &kv_items)
Append multiple key value pair to the root JSON object where the value type is an array of string typ...
Definition json.hpp:658
json(bool root_type_array=false)
Definition json.hpp:34
json & append(const std::vector< std::map< std::string, T > > &arr_items)
Append multiple key-value pairs as an object to the root JSON array, where value type of each KV pair...
Definition json.hpp:276
int32_t get_int(in< std::string > name, int32_t def_val=0) const
get the value associated with the key as an integer. Note that this function can throw if the underly...
Definition json.hpp:126
json & append(const std::string &key, const T &val)
Append a key value pair to the root JSON object where the value type is a native type.
Definition json.hpp:420
Definition reader_writer_lock_shim.hpp:7
fref< t && > rvref
R-value reference typedef Example.
Definition typedefs.hpp:89
clvref< t > in
Wraps a type and declares a value as an in parameter, which is a clvref.
Definition typedefs.hpp:139
fref< const t & > clvref
const l-value reference typedef.
Definition typedefs.hpp:123