mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[libsolutil] JSON: Add get function.
This commit is contained in:
parent
555d774f64
commit
145152038c
@ -109,9 +109,9 @@ AssemblyItem Assembly::createAssemblyItemFromJSON(Json::Value const& _json)
|
|||||||
std::string name = getOrDefault<std::string>(_json, "name", "");
|
std::string name = getOrDefault<std::string>(_json, "name", "");
|
||||||
solAssert(!name.empty());
|
solAssert(!name.empty());
|
||||||
|
|
||||||
int begin = getOrDefault<int>(_json, "begin", -1);
|
int begin = get<int>(_json, "begin");
|
||||||
int end = getOrDefault<int>(_json, "end", -1);
|
int end = get<int>(_json, "end");
|
||||||
int srcIndex = getOrDefault<int>(_json, "source", -1);
|
int srcIndex = get<int>(_json, "source");
|
||||||
size_t modifierDepth = static_cast<size_t>(getOrDefault<int>(_json, "modifierDepth", 0));
|
size_t modifierDepth = static_cast<size_t>(getOrDefault<int>(_json, "modifierDepth", 0));
|
||||||
std::string value = getOrDefault<std::string>(_json, "value", "");
|
std::string value = getOrDefault<std::string>(_json, "value", "");
|
||||||
std::string jumpType = getOrDefault<std::string>(_json, "jumpType", "");
|
std::string jumpType = getOrDefault<std::string>(_json, "jumpType", "");
|
||||||
|
@ -81,6 +81,10 @@ struct helper;
|
|||||||
{ \
|
{ \
|
||||||
return _input[_name].CHECK_TYPE(); \
|
return _input[_name].CHECK_TYPE(); \
|
||||||
} \
|
} \
|
||||||
|
static TYPE get(Json::Value const& _input, std::string const& _name) \
|
||||||
|
{ \
|
||||||
|
return _input[_name].CONVERT_TYPE(); \
|
||||||
|
} \
|
||||||
static TYPE getOrDefault(Json::Value const& _input, std::string const& _name, TYPE _default = {}) \
|
static TYPE getOrDefault(Json::Value const& _input, std::string const& _name, TYPE _default = {}) \
|
||||||
{ \
|
{ \
|
||||||
TYPE result = _default; \
|
TYPE result = _default; \
|
||||||
@ -114,6 +118,12 @@ bool ofTypeIfExists(Json::Value const& _input, std::string const& _name)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T get(Json::Value const& _input, std::string const& _name)
|
||||||
|
{
|
||||||
|
return detail::helper<T>::get(_input, _name);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T getOrDefault(Json::Value const& _input, std::string const& _name, T _default = {})
|
T getOrDefault(Json::Value const& _input, std::string const& _name, T _default = {})
|
||||||
{
|
{
|
||||||
|
@ -286,6 +286,29 @@ BOOST_AUTO_TEST_CASE(json_getOrDefault)
|
|||||||
BOOST_CHECK(getOrDefault<std::string>(json, "no_string", "ERROR") == "ERROR");
|
BOOST_CHECK(getOrDefault<std::string>(json, "no_string", "ERROR") == "ERROR");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(json_get)
|
||||||
|
{
|
||||||
|
Json::Value json;
|
||||||
|
|
||||||
|
json["float"] = 3.1f;
|
||||||
|
json["double"] = 3.1;
|
||||||
|
json["int"] = 2;
|
||||||
|
json["int64"] = Json::Int64{0x4000000000000000};
|
||||||
|
json["uint64"] = Json::UInt64{0x5000000000000000};
|
||||||
|
json["string"] = "Hello World!";
|
||||||
|
|
||||||
|
BOOST_CHECK(get<float>(json, "float") == 3.1f);
|
||||||
|
BOOST_CHECK(get<double>(json, "double") == 3.1);
|
||||||
|
BOOST_CHECK(get<int>(json, "int") == 2);
|
||||||
|
BOOST_CHECK(get<Json::Int>(json, "int") == 2);
|
||||||
|
BOOST_CHECK(get<Json::UInt>(json, "int") == 2);
|
||||||
|
BOOST_CHECK(get<Json::Int64>(json, "int") == 2);
|
||||||
|
BOOST_CHECK(get<Json::Int64>(json, "int64") == 0x4000000000000000);
|
||||||
|
BOOST_CHECK(get<Json::UInt64>(json, "int64") == 0x4000000000000000);
|
||||||
|
BOOST_CHECK(get<Json::UInt64>(json, "uint64") == 0x5000000000000000);
|
||||||
|
BOOST_CHECK(get<std::string>(json, "string") == "Hello World!");
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user