From a68b4cafa2b46b33e952baf818091f590ebea8ff Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 28 Dec 2014 14:57:21 +0100 Subject: [PATCH] + more test cases --- src/JSON.cc | 14 ++++++++++++-- src/JSON.h | 2 ++ test/JSON_unit.cc | 4 ++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/JSON.cc b/src/JSON.cc index 1efa65bb..3cf26c7e 100644 --- a/src/JSON.cc +++ b/src/JSON.cc @@ -1099,7 +1099,8 @@ const JSON& JSON::operator[](const std::string& key) const // this [] operator only works for objects if (_type != value_type::object) { - throw std::domain_error("cannot get entry with key " + std::string(key) + " from " + _typename()); + throw std::domain_error("cannot get entry with key " + + std::string(key) + " from " + _typename()); } // search for the key @@ -1151,6 +1152,14 @@ JSON& JSON::at(const char* key) return _value.object->at(key); } +/*! +@copydoc JSON::at(const char *key) const +*/ +const JSON& JSON::at(const std::string& key) const +{ + return at(key.c_str()); +} + /*! This operator realizes read-only access to object elements given a string key. @@ -1162,7 +1171,7 @@ key. @exception std::domain_error if object is not an object @exception std::out_of_range if key is not found (via std::map::at) */ -const JSON& JSON::at(const std::string& key) const +const JSON& JSON::at(const char* key) const { // this [] operator only works for objects if (_type != value_type::object) @@ -1175,6 +1184,7 @@ const JSON& JSON::at(const std::string& key) const return _value.object->at(key); } + /*! Returns the size of the JSON object. diff --git a/src/JSON.h b/src/JSON.h index aea34e13..e6693cc9 100644 --- a/src/JSON.h +++ b/src/JSON.h @@ -257,6 +257,8 @@ class JSON JSON& at(const char*); /// operator to get an element in an object const JSON& at(const std::string&) const; + /// operator to get an element in an object + const JSON& at(const char*) const; /// return the number of stored values size_t size() const noexcept; diff --git a/test/JSON_unit.cc b/test/JSON_unit.cc index 5721c306..d633b179 100644 --- a/test/JSON_unit.cc +++ b/test/JSON_unit.cc @@ -507,6 +507,10 @@ TEST_CASE("object") JSON nonarray = 1; CHECK_THROWS_AS(const int i = nonarray["v1"], std::domain_error); CHECK_THROWS_AS(nonarray["v1"] = 10, std::domain_error); + { + const JSON c = {{"foo", "bar"}}; + CHECK_THROWS_AS(c[std::string("baz")], std::out_of_range); + } // clear() JSON j7 = {{"k0", 0}, {"k1", 1}, {"k2", 2}, {"k3", 3}};