diff --git a/lib/Basics/JsonHelper.cpp b/lib/Basics/JsonHelper.cpp index 0b748b0a7d..e48583be2f 100644 --- a/lib/Basics/JsonHelper.cpp +++ b/lib/Basics/JsonHelper.cpp @@ -270,6 +270,23 @@ std::string JsonHelper::getStringValue (TRI_json_t const* json, return defaultValue; } +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns a string sub-element, or throws if does not exist +/// or it is not a string +//////////////////////////////////////////////////////////////////////////////// + +std::string JsonHelper::getStringValue (TRI_json_t const* json, + const char* name) { + TRI_json_t const* sub = getArrayElement(json, name); + + if ( sub == nullptr || !isString(sub)) { + std::string msg = "The attribute name " + std::string(name) + + " was not found or is not an string."; + THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, msg.c_str()); + } + return string(sub->_value._string.data, sub->_value._string.length - 1); +} + //////////////////////////////////////////////////////////////////////////////// /// @brief returns a boolean sub-element, or a default it is does not exist //////////////////////////////////////////////////////////////////////////////// @@ -286,6 +303,41 @@ bool JsonHelper::getBooleanValue (TRI_json_t const* json, return defaultValue; } +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns a boolean sub-element, or throws an exception if the +/// sub-element does not exist or if it is not boolean +//////////////////////////////////////////////////////////////////////////////// + +bool JsonHelper::getBooleanValue (TRI_json_t const* json, + const char* name) { + TRI_json_t const* sub = getArrayElement(json, name); + + if (sub == nullptr || !isBoolean(sub)) { + std::string msg = "The attribute name " + std::string(name) + + " was not found or is not a boolean."; + THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, msg.c_str()); + } + + return sub->_value._boolean; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns an array sub-element, or throws an exception if the +/// sub-element does not exist or if it is not boolean +//////////////////////////////////////////////////////////////////////////////// + +TRI_json_t const* JsonHelper::getArray (TRI_json_t const* json, + const char* name) { + TRI_json_t const* sub = getArrayElement(json, name); + + if (sub == nullptr || !isArray(sub)) { + std::string msg = "The attribute name " + std::string(name) + + " was not found or is not an array."; + THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL, msg.c_str()); + } + + return sub; +} // ----------------------------------------------------------------------------- // --SECTION-- END-OF-FILE // ----------------------------------------------------------------------------- diff --git a/lib/Basics/JsonHelper.h b/lib/Basics/JsonHelper.h index 607e80e204..456a2d981d 100644 --- a/lib/Basics/JsonHelper.h +++ b/lib/Basics/JsonHelper.h @@ -32,6 +32,7 @@ #include "Basics/Common.h" #include "BasicsC/json.h" +#include "arangod/Utils/Exception.h" namespace triagens { namespace basics { @@ -190,6 +191,14 @@ namespace triagens { const char*, const std::string&); +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns a string sub-element, or throws if does not exist +/// or it is not a string +//////////////////////////////////////////////////////////////////////////////// + + static std::string getStringValue (TRI_json_t const*, + const char* name); + //////////////////////////////////////////////////////////////////////////////// /// @brief returns a numeric value //////////////////////////////////////////////////////////////////////////////// @@ -226,7 +235,21 @@ namespace triagens { static bool getBooleanValue (TRI_json_t const*, const char*, bool); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns a boolean sub-element, or a throws an exception if the +/// sub-element does not exist or if it is not boolean +//////////////////////////////////////////////////////////////////////////////// + static bool getBooleanValue (TRI_json_t const*, + const char*); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns an array sub-element, or a throws an exception if the +/// sub-element does not exist or if it is not an array +//////////////////////////////////////////////////////////////////////////////// + + static TRI_json_t const* getArray (TRI_json_t const* json, const char* name); }; ////////////////////////////////////////////////////////////////////////////////