Skip to content

Commit 10a28db

Browse files
committed
[libsolutil] Add new JSON helper functions.
1 parent 2201526 commit 10a28db

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

libsolutil/JSON.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,67 @@ std::string jsonPrint(Json::Value const& _input, JsonFormat const& _format);
6767
/// \return \c true if the document was successfully parsed, \c false if an error occurred.
6868
bool jsonParseStrict(std::string const& _input, Json::Value& _json, std::string* _errs = nullptr);
6969

70+
namespace detail
71+
{
72+
73+
template<typename T>
74+
struct helper;
75+
76+
#define DEFINE_HELPER(TYPE, CHECK_TYPE, CONVERT_TYPE) \
77+
template<> \
78+
struct helper<TYPE> \
79+
{ \
80+
static bool ofType(Json::Value const& _input, std::string const& _name) \
81+
{ \
82+
return _input[_name].CHECK_TYPE(); \
83+
} \
84+
static TYPE get(Json::Value const& _input, std::string const& _name) \
85+
{ \
86+
return _input[_name].CONVERT_TYPE(); \
87+
} \
88+
static TYPE getOrDefault(Json::Value const& _input, std::string const& _name, TYPE _default = {}) \
89+
{ \
90+
TYPE result = _default; \
91+
if (helper::ofType(_input, _name)) \
92+
result = _input[_name].CONVERT_TYPE(); \
93+
return result; \
94+
} \
95+
};
96+
97+
DEFINE_HELPER(float, isDouble, asFloat)
98+
DEFINE_HELPER(double, isDouble, asDouble)
99+
DEFINE_HELPER(std::string, isString, asString)
100+
DEFINE_HELPER(Json::Int, isInt, asInt)
101+
DEFINE_HELPER(Json::Int64, isInt64, asInt64)
102+
DEFINE_HELPER(Json::UInt, isUInt, asUInt)
103+
DEFINE_HELPER(Json::UInt64, isUInt64, asUInt64)
104+
105+
} // namespace detail
106+
107+
template<typename T>
108+
bool ofType(Json::Value const& _input, std::string const& _name)
109+
{
110+
return detail::helper<T>::ofType(_input, _name);
70111
}
112+
113+
template<typename T>
114+
bool ofTypeIfExists(Json::Value const& _input, std::string const& _name)
115+
{
116+
if (_input.isMember(_name))
117+
return ofType<T>(_input, _name);
118+
return true;
119+
}
120+
121+
template<typename T>
122+
T get(Json::Value const& _input, std::string const& _name)
123+
{
124+
return detail::helper<T>::get(_input, _name);
125+
}
126+
127+
template<typename T>
128+
T getOrDefault(Json::Value const& _input, std::string const& _name, T _default = {})
129+
{
130+
return detail::helper<T>::getOrDefault(_input, _name, _default);
131+
}
132+
133+
} // namespace solidity::util

test/libsolutil/JSON.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,131 @@ BOOST_AUTO_TEST_CASE(parse_json_strict)
184184
BOOST_CHECK(json[0] == "😊");
185185
}
186186

187+
BOOST_AUTO_TEST_CASE(json_ofType)
188+
{
189+
Json::Value json;
190+
191+
json["float"] = 3.1f;
192+
json["double"] = 3.1;
193+
json["int"] = 2;
194+
json["int64"] = Json::Int64{0x4000000000000000};
195+
json["string"] = "Hello World!";
196+
197+
BOOST_CHECK(ofType<float>(json, "float"));
198+
BOOST_CHECK(ofType<double>(json, "double"));
199+
BOOST_CHECK(ofType<int>(json, "int"));
200+
BOOST_CHECK(ofType<Json::Int>(json, "int"));
201+
BOOST_CHECK(ofType<Json::UInt>(json, "int"));
202+
BOOST_CHECK(ofType<Json::Int64>(json, "int"));
203+
BOOST_CHECK(ofType<Json::Int64>(json, "int64"));
204+
BOOST_CHECK(ofType<Json::UInt64>(json, "int64"));
205+
BOOST_CHECK(ofType<std::string>(json, "string"));
206+
BOOST_CHECK(!ofType<Json::Int>(json, "int64"));
207+
BOOST_CHECK(!ofType<int>(json, "double"));
208+
BOOST_CHECK(!ofType<float>(json, "string"));
209+
BOOST_CHECK(!ofType<double>(json, "string"));
210+
BOOST_CHECK(!ofType<Json::Int>(json, "string"));
211+
BOOST_CHECK(!ofType<Json::Int64>(json, "string"));
212+
BOOST_CHECK(!ofType<Json::UInt>(json, "string"));
213+
BOOST_CHECK(!ofType<Json::UInt64>(json, "string"));
214+
}
215+
216+
BOOST_AUTO_TEST_CASE(json_ofTypeIfExists)
217+
{
218+
Json::Value json;
219+
220+
json["float"] = 3.1f;
221+
json["double"] = 3.1;
222+
json["int"] = 2;
223+
json["int64"] = Json::Int64{0x4000000000000000};
224+
json["string"] = "Hello World!";
225+
226+
BOOST_CHECK(ofTypeIfExists<float>(json, "float"));
227+
BOOST_CHECK(ofTypeIfExists<double>(json, "double"));
228+
BOOST_CHECK(ofTypeIfExists<int>(json, "int"));
229+
BOOST_CHECK(ofTypeIfExists<Json::Int>(json, "int"));
230+
BOOST_CHECK(ofTypeIfExists<Json::UInt>(json, "int"));
231+
BOOST_CHECK(ofTypeIfExists<Json::Int64>(json, "int"));
232+
BOOST_CHECK(ofTypeIfExists<Json::Int64>(json, "int64"));
233+
BOOST_CHECK(ofTypeIfExists<Json::UInt64>(json, "int64"));
234+
BOOST_CHECK(ofTypeIfExists<std::string>(json, "string"));
235+
BOOST_CHECK(!ofTypeIfExists<Json::Int>(json, "int64"));
236+
BOOST_CHECK(!ofTypeIfExists<int>(json, "double"));
237+
BOOST_CHECK(!ofTypeIfExists<float>(json, "string"));
238+
BOOST_CHECK(!ofTypeIfExists<double>(json, "string"));
239+
BOOST_CHECK(!ofTypeIfExists<Json::Int>(json, "string"));
240+
BOOST_CHECK(!ofTypeIfExists<Json::Int64>(json, "string"));
241+
BOOST_CHECK(!ofTypeIfExists<Json::UInt>(json, "string"));
242+
BOOST_CHECK(!ofTypeIfExists<Json::UInt64>(json, "string"));
243+
BOOST_CHECK(ofTypeIfExists<Json::UInt64>(json, "NOT_EXISTING"));
244+
}
245+
246+
BOOST_AUTO_TEST_CASE(json_getOrDefault)
247+
{
248+
Json::Value json;
249+
250+
json["float"] = 3.1f;
251+
json["double"] = 3.1;
252+
json["int"] = 2;
253+
json["int64"] = Json::Int64{0x4000000000000000};
254+
json["uint64"] = Json::UInt64{0x5000000000000000};
255+
json["string"] = "Hello World!";
256+
257+
BOOST_CHECK(getOrDefault<float>(json, "float") == 3.1f);
258+
BOOST_CHECK(getOrDefault<float>(json, "float", -1.1f) == 3.1f);
259+
BOOST_CHECK(getOrDefault<float>(json, "no_float", -1.1f) == -1.1f);
260+
BOOST_CHECK(getOrDefault<double>(json, "double") == 3.1);
261+
BOOST_CHECK(getOrDefault<double>(json, "double", -1) == 3.1);
262+
BOOST_CHECK(getOrDefault<double>(json, "no_double", -1.1) == -1.1);
263+
BOOST_CHECK(getOrDefault<int>(json, "int") == 2);
264+
BOOST_CHECK(getOrDefault<int>(json, "int", -1) == 2);
265+
BOOST_CHECK(getOrDefault<int>(json, "no_int", -1) == -1);
266+
BOOST_CHECK(getOrDefault<Json::Int>(json, "int") == 2);
267+
BOOST_CHECK(getOrDefault<Json::Int>(json, "int", -1) == 2);
268+
BOOST_CHECK(getOrDefault<Json::Int>(json, "no_int", -1) == -1);
269+
BOOST_CHECK(getOrDefault<Json::UInt>(json, "int") == 2);
270+
BOOST_CHECK(getOrDefault<Json::UInt>(json, "int", 1) == 2);
271+
BOOST_CHECK(getOrDefault<Json::UInt>(json, "no_int", 1) == 1);
272+
BOOST_CHECK(getOrDefault<Json::Int64>(json, "int") == 2);
273+
BOOST_CHECK(getOrDefault<Json::Int64>(json, "int", -1) == 2);
274+
BOOST_CHECK(getOrDefault<Json::Int64>(json, "no_int", -1) == -1);
275+
BOOST_CHECK(getOrDefault<Json::Int64>(json, "int64") == 0x4000000000000000);
276+
BOOST_CHECK(getOrDefault<Json::Int64>(json, "int64", -1) == 0x4000000000000000);
277+
BOOST_CHECK(getOrDefault<Json::Int64>(json, "no_int64", -1) == -1);
278+
BOOST_CHECK(getOrDefault<Json::UInt64>(json, "int64") == 0x4000000000000000);
279+
BOOST_CHECK(getOrDefault<Json::UInt64>(json, "int64", 1) == 0x4000000000000000);
280+
BOOST_CHECK(getOrDefault<Json::UInt64>(json, "no_int64", 1) == 1);
281+
BOOST_CHECK(getOrDefault<Json::UInt64>(json, "uint64") == 0x5000000000000000);
282+
BOOST_CHECK(getOrDefault<Json::UInt64>(json, "uint64", 1) == 0x5000000000000000);
283+
BOOST_CHECK(getOrDefault<Json::UInt64>(json, "no_uint64", 1) == 1);
284+
BOOST_CHECK(getOrDefault<std::string>(json, "string", "ERROR") == "Hello World!");
285+
BOOST_CHECK(getOrDefault<std::string>(json, "no_string").empty());
286+
BOOST_CHECK(getOrDefault<std::string>(json, "no_string", "ERROR") == "ERROR");
287+
}
288+
289+
BOOST_AUTO_TEST_CASE(json_get)
290+
{
291+
Json::Value json;
292+
293+
json["float"] = 3.1f;
294+
json["double"] = 3.1;
295+
json["int"] = 2;
296+
json["int64"] = Json::Int64{0x4000000000000000};
297+
json["uint64"] = Json::UInt64{0x5000000000000000};
298+
json["string"] = "Hello World!";
299+
300+
BOOST_CHECK(get<float>(json, "float") == 3.1f);
301+
BOOST_CHECK(get<double>(json, "double") == 3.1);
302+
BOOST_CHECK(get<int>(json, "int") == 2);
303+
BOOST_CHECK(get<Json::Int>(json, "int") == 2);
304+
BOOST_CHECK(get<Json::UInt>(json, "int") == 2);
305+
BOOST_CHECK(get<Json::Int64>(json, "int") == 2);
306+
BOOST_CHECK(get<Json::Int64>(json, "int64") == 0x4000000000000000);
307+
BOOST_CHECK(get<Json::UInt64>(json, "int64") == 0x4000000000000000);
308+
BOOST_CHECK(get<Json::UInt64>(json, "uint64") == 0x5000000000000000);
309+
BOOST_CHECK(get<std::string>(json, "string") == "Hello World!");
310+
}
311+
187312
BOOST_AUTO_TEST_SUITE_END()
188313

189314
}

0 commit comments

Comments
 (0)