summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYehuda Sadeh <yehuda@inktank.com>2012-10-30 15:52:25 -0700
committerYehuda Sadeh <yehuda@inktank.com>2012-10-30 15:52:25 -0700
commit9df953cdd5086bafb14ec2e7559daf7f157c8587 (patch)
treec6f827b816acb8562b6907832a8893acbcfbf9b0
parent6d3cafcc5044b5d3ac7ae4cee31d17110a72ebcc (diff)
downloadceph-9df953cdd5086bafb14ec2e7559daf7f157c8587.tar.gz
rgw: extend rgw json parser api
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
-rw-r--r--src/rgw/rgw_json.cc39
-rw-r--r--src/rgw/rgw_json.h6
2 files changed, 36 insertions, 9 deletions
diff --git a/src/rgw/rgw_json.cc b/src/rgw/rgw_json.cc
index 1a6fc32dea9..8e1aafe097c 100644
--- a/src/rgw/rgw_json.cc
+++ b/src/rgw/rgw_json.cc
@@ -2,6 +2,7 @@
#include <include/types.h>
#include "rgw_json.h"
+#include "rgw_common.h"
// for testing DELETE ME
#include <fstream>
@@ -9,6 +10,8 @@
using namespace std;
using namespace json_spirit;
+#define dout_subsys ceph_subsys_rgw
+
JSONObjIter::JSONObjIter()
{
}
@@ -52,7 +55,7 @@ JSONObj::~JSONObj()
void JSONObj::add_child(string el, JSONObj *obj)
{
- cout << "add_child: " << name << " <- " << el << endl;
+ cout << "add_child: " << name << " <- " << el << std::endl;
children.insert(pair<string, JSONObj *>(el, obj));
}
@@ -65,14 +68,16 @@ bool JSONObj::get_attr(string name, string& attr)
return true;
}
-JSONObjIter JSONObj::find(string name)
+JSONObjIter JSONObj::find(const string& name)
{
JSONObjIter iter;
map<string, JSONObj *>::iterator first;
map<string, JSONObj *>::iterator last;
first = children.find(name);
- last = children.upper_bound(name);
- iter.set(first, last);
+ if (first != children.end()) {
+ last = children.upper_bound(name);
+ iter.set(first, last);
+ }
return iter;
}
@@ -80,14 +85,14 @@ JSONObjIter JSONObj::find_first()
{
JSONObjIter iter;
iter.set(children.begin(), children.end());
- cout << "count=" << children.size() << endl;
+ cout << "count=" << children.size() << std::endl;
for (map<string, JSONObj *>:: iterator i = children.begin(); i != children.end(); ++i) {
- cout << "child: " << i->first << endl;
+ cout << "child: " << i->first << std::endl;
}
return iter;
}
-JSONObjIter JSONObj::find_first(string name)
+JSONObjIter JSONObj::find_first(const string& name)
{
JSONObjIter iter;
map<string, JSONObj *>::iterator first;
@@ -96,6 +101,26 @@ JSONObjIter JSONObj::find_first(string name)
return iter;
}
+JSONObj *JSONObj::find_obj(const string& name)
+{
+ JSONObjIter iter = find(name);
+ if (iter.end())
+ return NULL;
+
+ return *iter;
+}
+
+bool JSONObj::get_data(const string& key, string *dest)
+{
+ JSONObj *obj = find_obj(key);
+ if (!obj)
+ return false;
+
+ *dest = obj->get_data();
+
+ return true;
+}
+
/* accepts a JSON Array or JSON Object contained in
* a JSON Spirit Value, v, and creates a JSONObj for each
* child contained in v
diff --git a/src/rgw/rgw_json.h b/src/rgw/rgw_json.h
index e11ab25b405..d0dec397c28 100644
--- a/src/rgw/rgw_json.h
+++ b/src/rgw/rgw_json.h
@@ -55,12 +55,14 @@ public:
string& get_name() { return name; }
string& get_data() { return data_string; }
+ bool get_data(const string& key, string *dest);
JSONObj *get_parent();
void add_child(string el, JSONObj *child);
bool get_attr(string name, string& attr);
- JSONObjIter find(string name);
+ JSONObjIter find(const string& name);
JSONObjIter find_first();
- JSONObjIter find_first(string name);
+ JSONObjIter find_first(const string& name);
+ JSONObj *find_obj(const string& name);
friend ostream& operator<<(ostream& out, JSONObj& obj); // does not work, FIXME