forked from jprjr/turtlecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonHelper.h
203 lines (159 loc) · 4.76 KB
/
JsonHelper.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
// Copyright (c) 2018-2019, The TurtleCoin Developers
//
// Please see the included LICENSE file for more information.
#pragma once
#include "rapidjson/document.h"
/* Yikes! */
typedef rapidjson::GenericObject<
true,
rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>>
JSONObject;
typedef rapidjson::GenericValue<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>
JSONValue;
static const std::string kTypeNames[] = {"Null", "False", "True", "Object", "Array", "String", "Number", "Double"};
template<typename T> bool hasMember(const T &j, const std::string &key)
{
if (!j.IsObject())
{
return false;
}
auto val = j.FindMember(key);
return val != j.MemberEnd();
}
/**
* Gets a JSONValue from the JSON with a given keyname
*/
template<typename T> const rapidjson::Value &getJsonValue(const T &j, const std::string &key)
{
auto val = j.FindMember(key);
if (val == j.MemberEnd())
{
throw std::invalid_argument("Missing JSON parameter: '" + key + "'");
}
return val->value;
}
/**
* Gets a uint32_t from the JSON, with or without a given keyname
*/
template<typename T> uint32_t getUintFromJSON(const T &j)
{
if (!j.IsUint())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected uint64_t, got " + kTypeNames[j.GetType()]);
}
return j.GetUint();
}
template<typename T> uint32_t getUintFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
return getUintFromJSON(val);
}
/**
* Gets a double from the JSON, with or without a given keyname
*/
template<typename T> double getDoubleFromJSON(const T &j)
{
if (!j.IsDouble())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected double, got " + kTypeNames[j.GetType()]);
}
return j.GetDouble();
}
template<typename T> double getDoubleFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
return getDoubleFromJSON(val);
}
/**
* Gets a uint64_t from the JSON, with or without a given keyname
*/
template<typename T> uint64_t getUint64FromJSON(const T &j)
{
if (!j.IsUint64())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected uint64_t, got " + kTypeNames[j.GetType()]);
}
return j.GetUint64();
}
template<typename T> uint64_t getUint64FromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
return getUint64FromJSON(val);
}
/**
* Gets a int64_t from the JSON, with or without a given keyname
*/
template<typename T> uint64_t getInt64FromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
if (!val.IsInt64())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected int64_t, got " + kTypeNames[val.GetType()]);
}
return val.GetInt64();
}
/**
* Gets a string from the JSON, with or without a given keyname
*/
template<typename T> std::string getStringFromJSON(const T &j)
{
if (!j.IsString())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected String, got " + kTypeNames[j.GetType()]);
}
return j.GetString();
}
template<typename T> std::string getStringFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
return getStringFromJSON(val);
}
/**
* Gets an Array from JSON, with or without a given keyname
*/
template<typename T> auto getArrayFromJSON(const T &j)
{
if (!j.IsArray())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected Array, got " + kTypeNames[j.GetType()]);
}
return j.GetArray();
}
template<typename T> auto getArrayFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
return getArrayFromJSON(val);
return val.GetArray();
}
/**
* Gets a JSONObject from JSON, with our without a given keyname
*/
template<typename T> JSONObject getObjectFromJSON(const T &j)
{
if (!j.IsObject())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected Object, got " + kTypeNames[j.GetType()]);
}
return j.Get_Object();
}
template<typename T> JSONObject getObjectFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
return getObjectFromJSON(val);
}
/**
* Gets a boolean from JSON, with our without a given keyname
*/
template<typename T> bool getBoolFromJSON(const T &j)
{
if (!j.IsBool())
{
throw std::invalid_argument("JSON parameter is wrong type. Expected Bool, got " + kTypeNames[j.GetType()]);
}
return j.GetBool();
}
template<typename T> bool getBoolFromJSON(const T &j, const std::string &key)
{
auto &val = getJsonValue(j, key);
return getBoolFromJSON(val);
}