This repository has been archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonValueCaster.h
209 lines (165 loc) · 5.56 KB
/
JsonValueCaster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2017 Yangga
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
#pragma once
#include <json/value.h>
#ifdef USE_BOOST_STRING_REF
#include <boost/utility/string_ref.hpp>
#endif
#include <algorithm>
#include <stdint.h>
namespace Json {
namespace detail {
template <typename T> static T valueAs(Json::Value const& v) { static_assert(0 > sizeof(T), "invalid type"); }
template <> int8_t valueAs(Json::Value const& v) { return static_cast<int8_t>(v.asInt()); }
template <> uint8_t valueAs(Json::Value const& v) { return static_cast<uint8_t>(v.asInt()); }
template <> int16_t valueAs(Json::Value const& v) { return static_cast<int16_t>(v.asInt()); }
template <> uint16_t valueAs(Json::Value const& v) { return static_cast<uint16_t>(v.asInt()); }
template <> Value::Int valueAs(Json::Value const& v) { return v.asInt(); }
template <> Value::UInt valueAs(Json::Value const& v) { return v.asUInt(); }
template <> Value::LargestInt valueAs(Json::Value const& v) { return v.asLargestInt(); }
template <> long valueAs(Json::Value const& v) { return static_cast<long>(v.asLargestInt()); }
template <> Value::LargestUInt valueAs(Json::Value const& v) { return v.asLargestUInt(); }
template <> unsigned long valueAs(Json::Value const& v) { return static_cast<unsigned long>(v.asLargestUInt()); }
template <> float valueAs(Json::Value const& v) { return v.asFloat(); }
template <> double valueAs(Json::Value const& v) { return v.asDouble(); }
template <> bool valueAs(Json::Value const& v) { return v.asBool(); }
template <> std::string valueAs(Json::Value const& v) { return std::move(v.asString()); }
template <> const char* valueAs(Json::Value const& v) { return v.asCString(); }
#ifdef USE_BOOST_STRING_REF
template <> boost::string_ref valueAs(Json::Value const& v) {
const char* txt = v.asCString();
if (nullptr == txt)
return boost::string_ref();
return boost::string_ref(txt, strlen(txt));
}
#endif
} // namespace detail
class CasterStatic
{
public:
template <typename T>
inline static T Get(Json::Value const& node, const char* key) {
if (node.isNull()) {
throw std::invalid_argument("node is null");
}
if (!node.isObject()) {
throw std::invalid_argument("node is not an object");
}
auto& vNodeSub = node[key];
if (vNodeSub.isNull()) {
throw std::invalid_argument("node is null");
}
return detail::valueAs<T>(vNodeSub);
}
template <typename T>
inline static T Get(Json::Value const& node, Json::ArrayIndex const& idx) {
if (node.isNull()) {
throw std::invalid_argument("node is null");
}
if (!node.isArray()) {
throw std::invalid_argument("node is not an array");
}
if (node.size() <= idx) {
throw std::range_error("invalid range");
}
return detail::valueAs<T>(node[idx]);
}
template <typename T, typename... Args>
inline static T Get(Json::Value const& node, const char* key1, Args... args) {
if (node.isNull()) {
throw std::invalid_argument("node is null");
}
if (!node.isObject()) {
throw std::invalid_argument("node is not an object");
}
return Get<T>(node[key1], args...);
}
template <typename T, typename... Args>
inline static T Get(Json::Value const& node, Json::ArrayIndex const& idx, Args... args) {
if (node.isNull()) {
throw std::invalid_argument("node is null");
}
if (!node.isArray()) {
throw std::invalid_argument("node is not an array");
}
if (node.size() <= idx) {
throw std::range_error("invalid range");
}
return Get<T>(node[idx], args...);
}
};
class Caster
{
public:
explicit Caster(Json::Value const& node)
: node_(node) {}
template <typename T, typename KEY_TYPE = const char*, typename... KEY_TYPES>
inline T get(KEY_TYPE key, KEY_TYPES... elses) const {
return CasterStatic::Get<T>(node_, key, std::forward<KEY_TYPES>(elses)...);
}
private:
Json::Value const& node_;
};
class CasterCoverDef
{
public:
explicit CasterCoverDef(Json::Value const& node)
: node_(node) {}
template <typename T, typename KEY_TYPE = const char*, typename... KEY_TYPES>
inline T get(T const& defaultValue, KEY_TYPE key, KEY_TYPES... elses) const {
try {
return CasterStatic::Get<T>(node_, key, std::forward<KEY_TYPES>(elses)...);
}
catch (...) {
return defaultValue;
}
}
template <typename T, typename KEY_TYPE = const char*, typename... KEY_TYPES>
inline T get(T && defaultValue, KEY_TYPE key, KEY_TYPES... elses) const {
try {
return CasterStatic::Get<T>(node_, key, std::forward<KEY_TYPES>(elses)...);
}
catch (...) {
return defaultValue;
}
}
private:
Json::Value const& node_;
};
class CasterBoolean
{
public:
explicit CasterBoolean(Json::Value const& node)
: node_(node) {}
template <typename KEY_TYPE = const char*>
inline bool get(KEY_TYPE key, bool const defaultValue=false) const {
/// case - value is boolean type
try {
return CasterStatic::Get<bool>(node_, key);
}
catch (...) {}
/// case - value is string type
try {
auto v = CasterStatic::Get<std::string>(node_, key);
std::transform(v.begin(), v.end(), v.begin(), ::tolower);
if (v == "true")
return true;
return false;
}
catch (...) {}
/// case - value is number type
try {
auto const v = CasterStatic::Get<int>(node_, key);
if (0 == v)
return false;
return true;
}
catch (...) {}
return defaultValue;
}
private:
Json::Value const& node_;
};
} // namespace Json