Skip to content

Commit b8c021a

Browse files
authored
Minor features to crow::json::wvalue (#1143)
* add function "has(key):bool" to crow::json::wvalue * crow::json::wvalue : add functions operator cast to std::string
1 parent 7830f45 commit b8c021a

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

include/crow/json.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,26 @@ namespace crow // NOTE: Already documented in "crow/app.h"
17401740
return const_cast<wvalue*>(this)->operator[](index);
17411741
}
17421742

1743+
/// Check if the object contains the given key.
1744+
bool has(const char* key) const
1745+
{
1746+
return has(std::string(key));
1747+
}
1748+
1749+
/// Check if the object contains the given key.
1750+
bool has(const std::string& key) const
1751+
{
1752+
if (t_ != type::Object)
1753+
return false;
1754+
if (!o)
1755+
return false;
1756+
#if (__cplusplus>=202002L)
1757+
return o->contains(key);
1758+
#else
1759+
return o->count(key)>0;
1760+
#endif
1761+
}
1762+
17431763
int count(const std::string& str) const
17441764
{
17451765
if (t_ != type::Object)
@@ -2042,6 +2062,12 @@ namespace crow // NOTE: Already documented in "crow/app.h"
20422062

20432063
return dump(DontIndent);
20442064
}
2065+
2066+
/// Return json string.
2067+
explicit operator std::string() const
2068+
{
2069+
return dump();
2070+
}
20452071
};
20462072

20472073
// Used for accessing the internals of a wvalue

0 commit comments

Comments
 (0)