mirror of
https://github.com/nlohmann/json.git
synced 2024-11-23 22:19:02 +08:00
- a find() function
This commit is contained in:
parent
ac5ff65856
commit
0ee8d53a28
42
src/JSON.cc
42
src/JSON.cc
@ -484,6 +484,48 @@ JSON::json_t JSON::type() const {
|
||||
return _type;
|
||||
}
|
||||
|
||||
JSON::iterator JSON::find(const std::string& key) {
|
||||
return find(key.c_str());
|
||||
}
|
||||
|
||||
JSON::const_iterator JSON::find(const std::string& key) const {
|
||||
return find(key.c_str());
|
||||
}
|
||||
|
||||
JSON::iterator JSON::find(const char* key) {
|
||||
if (_type != object) {
|
||||
return end();
|
||||
} else {
|
||||
std::map<std::string, JSON>* o = static_cast<std::map<std::string, JSON>*>(_payload);
|
||||
const std::map<std::string, JSON>::iterator i = o->find(key);
|
||||
if (i != o->end()) {
|
||||
JSON::iterator result;
|
||||
result._object = this;
|
||||
result._oi = new std::map<std::string, JSON>::iterator(i);
|
||||
return result;
|
||||
} else {
|
||||
return end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JSON::const_iterator JSON::find(const char* key) const {
|
||||
if (_type != object) {
|
||||
return end();
|
||||
} else {
|
||||
std::map<std::string, JSON>* o = static_cast<std::map<std::string, JSON>*>(_payload);
|
||||
const std::map<std::string, JSON>::const_iterator i = o->find(key);
|
||||
if (i != o->end()) {
|
||||
JSON::const_iterator result;
|
||||
result._object = this;
|
||||
result._oi = new std::map<std::string, JSON>::const_iterator(i);
|
||||
return result;
|
||||
} else {
|
||||
return end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// direct access to the underlying payload
|
||||
void* JSON::data() {
|
||||
return _payload;
|
||||
|
19
src/JSON.h
19
src/JSON.h
@ -22,6 +22,11 @@
|
||||
#endif
|
||||
|
||||
class JSON {
|
||||
// forward declaration to friend this class
|
||||
public:
|
||||
class iterator;
|
||||
class const_iterator;
|
||||
|
||||
private:
|
||||
#ifdef __cplusplus11
|
||||
/// mutex to guard payload
|
||||
@ -158,6 +163,12 @@ class JSON {
|
||||
/// return the type of the object
|
||||
json_t type() const;
|
||||
|
||||
/// find an element in an object (returns end() iterator otherwise)
|
||||
iterator find(const std::string&);
|
||||
const_iterator find(const std::string&) const;
|
||||
iterator find(const char *);
|
||||
const_iterator find(const char *) const;
|
||||
|
||||
/// direct access to the underlying payload
|
||||
void* data();
|
||||
/// direct access to the underlying payload
|
||||
@ -170,14 +181,11 @@ class JSON {
|
||||
/// return the type as string
|
||||
std::string _typename() const;
|
||||
|
||||
// forward declaration to friend this class
|
||||
public:
|
||||
class const_iterator;
|
||||
|
||||
public:
|
||||
/// an iterator
|
||||
class iterator {
|
||||
friend class JSON::const_iterator;
|
||||
friend class JSON;
|
||||
friend class JSON::const_iterator;
|
||||
public:
|
||||
iterator();
|
||||
iterator(JSON*);
|
||||
@ -207,6 +215,7 @@ class JSON {
|
||||
|
||||
/// a const iterator
|
||||
class const_iterator {
|
||||
friend class JSON;
|
||||
public:
|
||||
const_iterator();
|
||||
const_iterator(const JSON*);
|
||||
|
Loading…
Reference in New Issue
Block a user