diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2015-06-04 11:40:03 +0200 |
---|---|---|
committer | Jonas Schnelli <jonas.schnelli@include7.ch> | 2015-06-04 13:18:57 +0200 |
commit | c02309204b8195476945f7066e8d96c60246db08 (patch) | |
tree | 3a550d0ff86d1bd557c793441b23df0d7725c68f /src/univalue/univalue.cpp | |
parent | 7e98a3c642222edc0813ced945d4b6e548cb8ca8 (diff) |
univalue: add strict type checking
Diffstat (limited to 'src/univalue/univalue.cpp')
-rw-r--r-- | src/univalue/univalue.cpp | 79 |
1 files changed, 78 insertions, 1 deletions
diff --git a/src/univalue/univalue.cpp b/src/univalue/univalue.cpp index e8fc513433..6920c44c96 100644 --- a/src/univalue/univalue.cpp +++ b/src/univalue/univalue.cpp @@ -6,8 +6,12 @@ #include <ctype.h> #include <iomanip> #include <sstream> +#include <stdexcept> // std::runtime_error + #include "univalue.h" +#include "utilstrencodings.h" // ParseXX + using namespace std; const UniValue NullUniValue; @@ -224,4 +228,77 @@ const UniValue& find_value( const UniValue& obj, const std::string& name) } return NullUniValue; -}
\ No newline at end of file +} + +std::vector<std::string> UniValue::getKeys() const +{ + if (typ != VOBJ) + throw std::runtime_error("JSON value is not an object as expected"); + return keys; +} + +std::vector<UniValue> UniValue::getValues() const +{ + if (typ != VOBJ && typ != VARR) + throw std::runtime_error("JSON value is not an object or array as expected"); + return values; +} + +bool UniValue::get_bool() const +{ + if (typ != VBOOL) + throw std::runtime_error("JSON value is not a boolean as expected"); + return getBool(); +} + +std::string UniValue::get_str() const +{ + if (typ != VSTR) + throw std::runtime_error("JSON value is not a string as expected"); + return getValStr(); +} + +int UniValue::get_int() const +{ + if (typ != VNUM) + throw std::runtime_error("JSON value is not an integer as expected"); + int32_t retval; + if (!ParseInt32(getValStr(), &retval)) + throw std::runtime_error("JSON integer out of range"); + return retval; +} + +int64_t UniValue::get_int64() const +{ + if (typ != VNUM) + throw std::runtime_error("JSON value is not an integer as expected"); + int64_t retval; + if (!ParseInt64(getValStr(), &retval)) + throw std::runtime_error("JSON integer out of range"); + return retval; +} + +double UniValue::get_real() const +{ + if (typ != VREAL && typ != VNUM) + throw std::runtime_error("JSON value is not a number as expected"); + double retval; + if (!ParseDouble(getValStr(), &retval)) + throw std::runtime_error("JSON double out of range"); + return retval; +} + +const UniValue& UniValue::get_obj() const +{ + if (typ != VOBJ) + throw std::runtime_error("JSON value is not an object as expected"); + return *this; +} + +const UniValue& UniValue::get_array() const +{ + if (typ != VARR) + throw std::runtime_error("JSON value is not an array as expected"); + return *this; +} + |