implemented missing operator[]
This commit is contained in:
parent
36f14d21da
commit
807de40463
4 changed files with 54 additions and 10 deletions
|
@ -192,11 +192,14 @@ class json
|
||||||
@brief create an array object
|
@brief create an array object
|
||||||
@param v any type of container whose elements can be use to construct
|
@param v any type of container whose elements can be use to construct
|
||||||
JSON objects (e.g., std::vector, std::set, std::array)
|
JSON objects (e.g., std::vector, std::set, std::array)
|
||||||
|
@note For some reason, we need to explicitly forbid JSON iterator types.
|
||||||
*/
|
*/
|
||||||
template <class V, typename
|
template <class V, typename
|
||||||
std::enable_if<
|
std::enable_if<
|
||||||
not std::is_same<V, json::const_iterator>::value and
|
|
||||||
not std::is_same<V, json::iterator>::value and
|
not std::is_same<V, json::iterator>::value and
|
||||||
|
not std::is_same<V, json::const_iterator>::value and
|
||||||
|
not std::is_same<V, json::reverse_iterator>::value and
|
||||||
|
not std::is_same<V, json::const_reverse_iterator>::value and
|
||||||
std::is_constructible<json, typename V::value_type>::value, int>::type
|
std::is_constructible<json, typename V::value_type>::value, int>::type
|
||||||
= 0>
|
= 0>
|
||||||
json(const V& v) : json(array_t(v.begin(), v.end()))
|
json(const V& v) : json(array_t(v.begin(), v.end()))
|
||||||
|
@ -325,9 +328,9 @@ class json
|
||||||
void push_back(list_init_t);
|
void push_back(list_init_t);
|
||||||
|
|
||||||
/// operator to set an element in an array
|
/// operator to set an element in an array
|
||||||
json& operator[](const int);
|
reference operator[](const int);
|
||||||
/// operator to get an element in an array
|
/// operator to get an element in an array
|
||||||
const json& operator[](const int) const;
|
const_reference operator[](const int) const;
|
||||||
/// operator to get an element in an array
|
/// operator to get an element in an array
|
||||||
reference at(const int);
|
reference at(const int);
|
||||||
/// operator to get an element in an array
|
/// operator to get an element in an array
|
||||||
|
@ -339,6 +342,8 @@ class json
|
||||||
reference operator[](const char*);
|
reference operator[](const char*);
|
||||||
/// operator to get an element in an object
|
/// operator to get an element in an object
|
||||||
const_reference operator[](const std::string&) const;
|
const_reference operator[](const std::string&) const;
|
||||||
|
/// operator to get an element in an object
|
||||||
|
const_reference operator[](const char*) const;
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
reference at(const std::string&);
|
reference at(const std::string&);
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
|
@ -1546,6 +1551,14 @@ json::reference json::operator[](const char* key)
|
||||||
return (*value_.object)[key];
|
return (*value_.object)[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@copydoc json::operator[](const char* key)
|
||||||
|
*/
|
||||||
|
json::const_reference json::operator[](const std::string& key) const
|
||||||
|
{
|
||||||
|
return operator[](key.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
This operator realizes read-only access to object elements given a string
|
This operator realizes read-only access to object elements given a string
|
||||||
key.
|
key.
|
||||||
|
@ -1557,7 +1570,7 @@ key.
|
||||||
@exception std::domain_error if object is not an object
|
@exception std::domain_error if object is not an object
|
||||||
@exception std::out_of_range if key is not found in object
|
@exception std::out_of_range if key is not found in object
|
||||||
*/
|
*/
|
||||||
json::const_reference json::operator[](const std::string& key) const
|
json::const_reference json::operator[](const char* key) const
|
||||||
{
|
{
|
||||||
// this [] operator only works for objects
|
// this [] operator only works for objects
|
||||||
if (type_ != value_t::object)
|
if (type_ != value_t::object)
|
||||||
|
@ -1572,7 +1585,7 @@ json::const_reference json::operator[](const std::string& key) const
|
||||||
// make sure the key exists in the object
|
// make sure the key exists in the object
|
||||||
if (it == value_.object->end())
|
if (it == value_.object->end())
|
||||||
{
|
{
|
||||||
throw std::out_of_range("key " + key + " not found");
|
throw std::out_of_range("key " + std::string(key) + " not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// return element from array at given key
|
// return element from array at given key
|
||||||
|
|
12
src/json.cc
12
src/json.cc
|
@ -981,6 +981,14 @@ json::reference json::operator[](const char* key)
|
||||||
return (*value_.object)[key];
|
return (*value_.object)[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@copydoc json::operator[](const char* key)
|
||||||
|
*/
|
||||||
|
json::const_reference json::operator[](const std::string& key) const
|
||||||
|
{
|
||||||
|
return operator[](key.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
This operator realizes read-only access to object elements given a string
|
This operator realizes read-only access to object elements given a string
|
||||||
key.
|
key.
|
||||||
|
@ -992,7 +1000,7 @@ key.
|
||||||
@exception std::domain_error if object is not an object
|
@exception std::domain_error if object is not an object
|
||||||
@exception std::out_of_range if key is not found in object
|
@exception std::out_of_range if key is not found in object
|
||||||
*/
|
*/
|
||||||
json::const_reference json::operator[](const std::string& key) const
|
json::const_reference json::operator[](const char* key) const
|
||||||
{
|
{
|
||||||
// this [] operator only works for objects
|
// this [] operator only works for objects
|
||||||
if (type_ != value_t::object)
|
if (type_ != value_t::object)
|
||||||
|
@ -1007,7 +1015,7 @@ json::const_reference json::operator[](const std::string& key) const
|
||||||
// make sure the key exists in the object
|
// make sure the key exists in the object
|
||||||
if (it == value_.object->end())
|
if (it == value_.object->end())
|
||||||
{
|
{
|
||||||
throw std::out_of_range("key " + key + " not found");
|
throw std::out_of_range("key " + std::string(key) + " not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// return element from array at given key
|
// return element from array at given key
|
||||||
|
|
11
src/json.h
11
src/json.h
|
@ -192,11 +192,14 @@ class json
|
||||||
@brief create an array object
|
@brief create an array object
|
||||||
@param v any type of container whose elements can be use to construct
|
@param v any type of container whose elements can be use to construct
|
||||||
JSON objects (e.g., std::vector, std::set, std::array)
|
JSON objects (e.g., std::vector, std::set, std::array)
|
||||||
|
@note For some reason, we need to explicitly forbid JSON iterator types.
|
||||||
*/
|
*/
|
||||||
template <class V, typename
|
template <class V, typename
|
||||||
std::enable_if<
|
std::enable_if<
|
||||||
not std::is_same<V, json::const_iterator>::value and
|
|
||||||
not std::is_same<V, json::iterator>::value and
|
not std::is_same<V, json::iterator>::value and
|
||||||
|
not std::is_same<V, json::const_iterator>::value and
|
||||||
|
not std::is_same<V, json::reverse_iterator>::value and
|
||||||
|
not std::is_same<V, json::const_reverse_iterator>::value and
|
||||||
std::is_constructible<json, typename V::value_type>::value, int>::type
|
std::is_constructible<json, typename V::value_type>::value, int>::type
|
||||||
= 0>
|
= 0>
|
||||||
json(const V& v) : json(array_t(v.begin(), v.end()))
|
json(const V& v) : json(array_t(v.begin(), v.end()))
|
||||||
|
@ -325,9 +328,9 @@ class json
|
||||||
void push_back(list_init_t);
|
void push_back(list_init_t);
|
||||||
|
|
||||||
/// operator to set an element in an array
|
/// operator to set an element in an array
|
||||||
json& operator[](const int);
|
reference operator[](const int);
|
||||||
/// operator to get an element in an array
|
/// operator to get an element in an array
|
||||||
const json& operator[](const int) const;
|
const_reference operator[](const int) const;
|
||||||
/// operator to get an element in an array
|
/// operator to get an element in an array
|
||||||
reference at(const int);
|
reference at(const int);
|
||||||
/// operator to get an element in an array
|
/// operator to get an element in an array
|
||||||
|
@ -339,6 +342,8 @@ class json
|
||||||
reference operator[](const char*);
|
reference operator[](const char*);
|
||||||
/// operator to get an element in an object
|
/// operator to get an element in an object
|
||||||
const_reference operator[](const std::string&) const;
|
const_reference operator[](const std::string&) const;
|
||||||
|
/// operator to get an element in an object
|
||||||
|
const_reference operator[](const char*) const;
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
reference at(const std::string&);
|
reference at(const std::string&);
|
||||||
/// operator to set an element in an object
|
/// operator to set an element in an object
|
||||||
|
|
|
@ -504,6 +504,18 @@ TEST_CASE("object")
|
||||||
bool v4 = j[std::string("k4")];
|
bool v4 = j[std::string("k4")];
|
||||||
CHECK(v4 == true);
|
CHECK(v4 == true);
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
const std::string v0 = k["k0"];
|
||||||
|
CHECK(v0 == "v0");
|
||||||
|
auto v1 = k["k1"];
|
||||||
|
CHECK(v1 == nullptr);
|
||||||
|
int v2 = k["k2"];
|
||||||
|
CHECK(v2 == 42);
|
||||||
|
double v3 = k["k3"];
|
||||||
|
CHECK(v3 == 3.141);
|
||||||
|
bool v4 = k["k4"];
|
||||||
|
CHECK(v4 == true);
|
||||||
|
}
|
||||||
{
|
{
|
||||||
const std::string v0 = k[std::string("k0")];
|
const std::string v0 = k[std::string("k0")];
|
||||||
CHECK(v0 == "v0");
|
CHECK(v0 == "v0");
|
||||||
|
@ -925,6 +937,12 @@ TEST_CASE("null")
|
||||||
|
|
||||||
CHECK(n1 == json());
|
CHECK(n1 == json());
|
||||||
CHECK(n2 == json());
|
CHECK(n2 == json());
|
||||||
|
|
||||||
|
json::reverse_iterator ri = n1.rbegin();
|
||||||
|
ri--;
|
||||||
|
|
||||||
|
json::const_reverse_iterator rci = n1.crbegin();
|
||||||
|
rci--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue