]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: rgw_jsonparser changes
authorYehuda Sadeh <yehuda@inktank.com>
Wed, 6 Feb 2013 20:36:56 +0000 (12:36 -0800)
committerYehuda Sadeh <yehuda@inktank.com>
Fri, 22 Mar 2013 18:23:07 +0000 (11:23 -0700)
Now testing json_decode functionality.

Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/rgw/rgw_jsonparser.cc

index 7a1f11c325efe076131abbaaf41a6a5c1c773f5f..820cc6216b640603a8686d70a988dad36e0f25ab 100644 (file)
@@ -1,3 +1,4 @@
+#include <errno.h>
 #include <string.h>
 
 #include <iostream>
@@ -5,7 +6,10 @@
 
 #include "include/types.h"
 
+#include "common/Formatter.h"
+
 #include "rgw_json.h"
+#include "rgw_common.h"
 
 #define dout_subsys ceph_subsys_rgw
 
@@ -18,15 +22,43 @@ void dump_array(JSONObj *obj)
 
   for (; !iter.end(); ++iter) { 
     JSONObj *o = *iter;
-    cout << "data=" << o->get_data() << endl;
+    cout << "data=" << o->get_data() << std::endl;
   }
 
 }
                                   
+struct Key {
+  string user;
+  string access_key;
+  string secret_key;
+
+  void decode_json(JSONObj *obj) {
+    JSONDecoder::decode_json("user", user, obj);
+    JSONDecoder::decode_json("access_key", access_key, obj);
+    JSONDecoder::decode_json("secret_key", secret_key, obj);
+  }
+};
+
+struct UserInfo {
+  string uid;
+  string display_name;
+  int max_buckets;
+  list<Key> keys;
+
+  void decode_json(JSONObj *obj) {
+    JSONDecoder::decode_json("user_id", uid, obj);
+    JSONDecoder::decode_json("display_name", display_name, obj);
+    JSONDecoder::decode_json("max_buckets", max_buckets, obj);
+    JSONDecoder::decode_json("keys", keys, obj);
+  }
+};
+
+
 int main(int argc, char **argv) {
   RGWJSONParser parser;
 
   char buf[1024];
+  bufferlist bl;
 
   for (;;) {
     int done;
@@ -43,18 +75,20 @@ int main(int argc, char **argv) {
     if (!ret)
       cerr << "parse error" << std::endl;
 
-    if (done)
+    if (done) {
+      bl.append(buf, len);
       break;
+    }
   }
 
   JSONObjIter iter = parser.find_first();
 
   for (; !iter.end(); ++iter) { 
     JSONObj *obj = *iter;
-    cout << "is_object=" << obj->is_object() << endl;
-    cout << "is_array=" << obj->is_array() << endl;
-    cout << "name=" << obj->get_name() << endl;
-    cout << "data=" << obj->get_data() << endl;
+    cout << "is_object=" << obj->is_object() << std::endl;
+    cout << "is_array=" << obj->is_array() << std::endl;
+    cout << "name=" << obj->get_name() << std::endl;
+    cout << "data=" << obj->get_data() << std::endl;
   }
 
   iter = parser.find_first("conditions");
@@ -64,17 +98,33 @@ int main(int argc, char **argv) {
     JSONObjIter iter2 = obj->find_first();
     for (; !iter2.end(); ++iter2) {
       JSONObj *child = *iter2;
-      cout << "is_object=" << child->is_object() << endl;
-      cout << "is_array=" << child->is_array() << endl;
+      cout << "is_object=" << child->is_object() << std::endl;
+      cout << "is_array=" << child->is_array() << std::endl;
       if (child->is_array()) {
         dump_array(child);
       }
-      cout << "name=" << child->get_name() << endl;
-      cout << "data=" << child->get_data() << endl;
+      cout << "name=" << child->get_name() <<std::endl;
+      cout << "data=" << child->get_data() <<std::endl;
     }
   }
 
+  RGWUserInfo ui;
+
+  try {
+    ui.decode_json(&parser);
+  } catch (JSONDecoder::err& e) {
+    cout << "failed to decode JSON input: " << e.message << std::endl;
+    exit(1);
+  }
+
+  JSONFormatter formatter(true);
+
+  formatter.open_object_section("user_info");
+  ui.dump(&formatter);
+  formatter.close_section();
+
+  formatter.flush(std::cout);
 
-  exit(0);
+  std::cout << std::endl;
 }