Skip to content

Commit 7b644c5

Browse files
committed
[libsolutil] JSON: Add get function.
1 parent 0ae37c8 commit 7b644c5

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

libevmasm/Assembly.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json::Value const& _json)
109109
std::string name = getOrDefault<std::string>(_json, "name", "");
110110
solAssert(!name.empty());
111111

112-
int begin = getOrDefault<int>(_json, "begin", -1);
113-
int end = getOrDefault<int>(_json, "end", -1);
114-
int srcIndex = getOrDefault<int>(_json, "source", -1);
112+
int begin = get<int>(_json, "begin");
113+
int end = get<int>(_json, "end");
114+
int srcIndex = get<int>(_json, "source");
115115
size_t modifierDepth = static_cast<size_t>(getOrDefault<int>(_json, "modifierDepth", 0));
116116
std::string value = getOrDefault<std::string>(_json, "value", "");
117117
std::string jumpType = getOrDefault<std::string>(_json, "jumpType", "");

libsolutil/JSON.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ struct helper;
8181
{ \
8282
return _input[_name].CHECK_TYPE(); \
8383
} \
84+
static TYPE get(Json::Value const& _input, std::string const& _name) \
85+
{ \
86+
return _input[_name].CONVERT_TYPE(); \
87+
} \
8488
static TYPE getOrDefault(Json::Value const& _input, std::string const& _name, TYPE _default = {}) \
8589
{ \
8690
TYPE result = _default; \
@@ -114,6 +118,12 @@ bool ofTypeIfExists(Json::Value const& _input, std::string const& _name)
114118
return true;
115119
}
116120

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+
117127
template<typename T>
118128
T getOrDefault(Json::Value const& _input, std::string const& _name, T _default = {})
119129
{

test/libsolutil/JSON.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,29 @@ BOOST_AUTO_TEST_CASE(json_getOrDefault)
286286
BOOST_CHECK(getOrDefault<std::string>(json, "no_string", "ERROR") == "ERROR");
287287
}
288288

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+
289312
BOOST_AUTO_TEST_SUITE_END()
290313

291314
}

0 commit comments

Comments
 (0)