lintel 0.2.0
Loading...
Searching...
No Matches
JsonUtils.h
Go to the documentation of this file.
1#ifndef LINTEL_JSONUTILS_H
2#define LINTEL_JSONUTILS_H
3
4#include <cstdint>
5#include <string>
6
7#include <rapidjson/document.h>
8
9namespace lintel::json {
21namespace detail {
22 inline const rapidjson::Value *findMember(const rapidjson::Value &obj, const char *key)
23 {
24 if (!obj.IsObject()) { return nullptr; }
25 const auto iter = obj.FindMember(key);
26 if (iter == obj.MemberEnd()) { return nullptr; }
27 return &iter->value;
28 }
29}// namespace detail
30
32inline const rapidjson::Value *member(const rapidjson::Value &obj, const char *key)
33{
34 return detail::findMember(obj, key);
35}
36
38inline bool getString(const rapidjson::Value &obj, const char *key, std::string &out)
39{
40 const rapidjson::Value *value = detail::findMember(obj, key);
41 if (value == nullptr || !value->IsString()) { return false; }
42 out = std::string(value->GetString(), value->GetStringLength());
43 return true;
44}
45
47inline bool getBool(const rapidjson::Value &obj, const char *key, bool &out)
48{
49 const rapidjson::Value *value = detail::findMember(obj, key);
50 if (value == nullptr || !value->IsBool()) { return false; }
51 out = value->GetBool();
52 return true;
53}
54
56inline bool getInt(const rapidjson::Value &obj, const char *key, std::int32_t &out)
57{
58 const rapidjson::Value *value = detail::findMember(obj, key);
59 if (value == nullptr || !value->IsInt()) { return false; }
60 out = value->GetInt();
61 return true;
62}
63
65inline bool getInt64(const rapidjson::Value &obj, const char *key, std::int64_t &out)
66{
67 const rapidjson::Value *value = detail::findMember(obj, key);
68 if (value == nullptr) { return false; }
69 if (value->IsInt64()) {
70 out = value->GetInt64();
71 } else if (value->IsInt()) {
72 out = value->GetInt();
73 } else if (value->IsUint()) {
74 out = static_cast<std::int64_t>(value->GetUint());
75 } else if (value->IsUint64()) {
76 out = static_cast<std::int64_t>(value->GetUint64());
77 } else {
78 return false;
79 }
80 return true;
81}
82
84inline bool getUint64(const rapidjson::Value &obj, const char *key, std::uint64_t &out)
85{
86 const rapidjson::Value *value = detail::findMember(obj, key);
87 if (value == nullptr) { return false; }
88 if (value->IsUint64()) {
89 out = value->GetUint64();
90 } else if (value->IsUint()) {
91 out = value->GetUint();
92 } else if (value->IsInt64()) {
93 const std::int64_t signedValue = value->GetInt64();
94 if (signedValue < 0) { return false; }
95 out = static_cast<std::uint64_t>(signedValue);
96 } else if (value->IsInt()) {
97 if (value->GetInt() < 0) { return false; }
98 out = static_cast<std::uint64_t>(value->GetInt());
99 } else {
100 return false;
101 }
102 return true;
103}
104
106inline bool getDouble(const rapidjson::Value &obj, const char *key, double &out)
107{
108 const rapidjson::Value *value = detail::findMember(obj, key);
109 if (value == nullptr || !value->IsNumber()) { return false; }
110 out = value->GetDouble();
111 return true;
112}
113}// namespace lintel::json
114
115#endif// LINTEL_JSONUTILS_H
const rapidjson::Value * findMember(const rapidjson::Value &obj, const char *key)
Definition JsonUtils.h:22
Definition JsonUtils.h:9
bool getInt64(const rapidjson::Value &obj, const char *key, std::int64_t &out)
Definition JsonUtils.h:65
const rapidjson::Value * member(const rapidjson::Value &obj, const char *key)
Definition JsonUtils.h:32
bool getInt(const rapidjson::Value &obj, const char *key, std::int32_t &out)
Definition JsonUtils.h:56
bool getBool(const rapidjson::Value &obj, const char *key, bool &out)
Definition JsonUtils.h:47
bool getUint64(const rapidjson::Value &obj, const char *key, std::uint64_t &out)
Definition JsonUtils.h:84
bool getString(const rapidjson::Value &obj, const char *key, std::string &out)
Definition JsonUtils.h:38
bool getDouble(const rapidjson::Value &obj, const char *key, double &out)
Definition JsonUtils.h:106
constexpr std::string_view type_name()
Definition TypeName.h:9