mirror of
https://github.com/matrix-construct/construct
synced 2024-12-27 07:54:05 +01:00
ircd::json: Add integrated type-checking overloads to reduce call count.
This commit is contained in:
parent
9b4dd43cf2
commit
5958e73ff8
2 changed files with 63 additions and 2 deletions
|
@ -89,12 +89,14 @@ struct ircd::json::object
|
||||||
size_t count() const;
|
size_t count() const;
|
||||||
size_t size() const; // warns if used; use count()
|
size_t size() const; // warns if used; use count()
|
||||||
bool has(const string_view &key) const;
|
bool has(const string_view &key) const;
|
||||||
|
bool has(const string_view &key, const enum json::type &) const; // false if not type
|
||||||
|
|
||||||
// returns value or default
|
// returns value or default
|
||||||
template<class T> T get(const string_view &key, const T &def = T{}) const;
|
template<class T> T get(const string_view &key, const T &def = T{}) const;
|
||||||
string_view get(const string_view &key, const string_view &def = {}) const;
|
string_view get(const string_view &key, const string_view &def = {}) const;
|
||||||
|
|
||||||
// returns value or throws not_found
|
// returns value or throws not_found
|
||||||
|
template<class T = string_view> T at(const string_view &key, const enum json::type &) const;
|
||||||
template<class T = string_view> T at(const string_view &key) const;
|
template<class T = string_view> T at(const string_view &key) const;
|
||||||
|
|
||||||
// returns value or empty
|
// returns value or empty
|
||||||
|
@ -168,6 +170,44 @@ catch(const bad_lex_cast &e)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline T
|
||||||
|
ircd::json::object::at(const string_view &key,
|
||||||
|
const enum json::type &type)
|
||||||
|
const try
|
||||||
|
{
|
||||||
|
const auto it
|
||||||
|
{
|
||||||
|
find(key)
|
||||||
|
};
|
||||||
|
|
||||||
|
if(unlikely(it == end()))
|
||||||
|
throw not_found
|
||||||
|
{
|
||||||
|
"'%s'", key
|
||||||
|
};
|
||||||
|
|
||||||
|
if(unlikely(!json::type(it->second, type, strict)))
|
||||||
|
throw type_error
|
||||||
|
{
|
||||||
|
"'%s' expected %s; got %s instead",
|
||||||
|
key,
|
||||||
|
reflect(type),
|
||||||
|
reflect(json::type(it->second, std::nothrow)),
|
||||||
|
};
|
||||||
|
|
||||||
|
return lex_cast<T>(it->second);
|
||||||
|
}
|
||||||
|
catch(const bad_lex_cast &e)
|
||||||
|
{
|
||||||
|
throw type_error
|
||||||
|
{
|
||||||
|
"'%s' must cast to type %s",
|
||||||
|
key,
|
||||||
|
typeid(T).name()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
template<ircd::json::name_hash_t key,
|
template<ircd::json::name_hash_t key,
|
||||||
class T>
|
class T>
|
||||||
inline T
|
inline T
|
||||||
|
|
25
ircd/json.cc
25
ircd/json.cc
|
@ -2707,8 +2707,14 @@ ircd::string_view
|
||||||
ircd::json::object::operator[](const string_view &key)
|
ircd::json::object::operator[](const string_view &key)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
const auto it(find(key));
|
const auto it
|
||||||
return it != end()? it->second : string_view{};
|
{
|
||||||
|
find(key)
|
||||||
|
};
|
||||||
|
|
||||||
|
return it != end()?
|
||||||
|
it->second:
|
||||||
|
string_view{};
|
||||||
}
|
}
|
||||||
|
|
||||||
ircd::string_view
|
ircd::string_view
|
||||||
|
@ -2725,6 +2731,21 @@ const
|
||||||
return json::strung(*this);
|
return json::strung(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
ircd::json::object::has(const string_view &key,
|
||||||
|
const enum json::type &type)
|
||||||
|
const
|
||||||
|
{
|
||||||
|
const auto &it
|
||||||
|
{
|
||||||
|
find(key)
|
||||||
|
};
|
||||||
|
|
||||||
|
return it != end()?
|
||||||
|
json::type(it->second, type, strict):
|
||||||
|
false;
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
ircd::json::object::has(const string_view &key)
|
ircd::json::object::has(const string_view &key)
|
||||||
const
|
const
|
||||||
|
|
Loading…
Reference in a new issue