]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
moved rgw/rgw_json.* common/ceph_json.*
authorYehuda Sadeh <yehuda@inktank.com>
Wed, 6 Feb 2013 20:56:54 +0000 (12:56 -0800)
committerYehuda Sadeh <yehuda@inktank.com>
Fri, 22 Mar 2013 18:23:08 +0000 (11:23 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/Makefile.am
src/common/ceph_json.cc [new file with mode: 0644]
src/common/ceph_json.h [new file with mode: 0644]
src/rgw/rgw_common.cc
src/rgw/rgw_json.cc [deleted file]
src/rgw/rgw_json.h [deleted file]
src/rgw/rgw_jsonparser.cc
src/rgw/rgw_policy_s3.cc
src/rgw/rgw_swift.cc

index 81015e7fcd617b5fbc80769441d4425d2330a050..2cc40622e644420c92a4ee91a64dafea02a844cd 100644 (file)
@@ -133,7 +133,6 @@ bin_PROGRAMS += monmaptool crushtool osdmaptool
 rgw_dencoder_src = rgw/rgw_dencoder.cc \
                    rgw/rgw_acl.cc \
                    rgw/rgw_common.cc \
-                   rgw/rgw_json.cc \
                    rgw/rgw_json_enc.cc
 
 ceph_dencoder_SOURCES = test/encoding/ceph_dencoder.cc ${rgw_dencoder_src} perfglue/disabled_heap_profiler.cc
@@ -358,7 +357,6 @@ librgw_a_SOURCES =  \
        rgw/rgw_fcgi.cc \
        rgw/rgw_xml.cc \
        rgw/rgw_usage.cc \
-       rgw/rgw_json.cc \
        rgw/rgw_json_enc.cc \
        rgw/rgw_user.cc \
        rgw/rgw_tools.cc \
@@ -1339,6 +1337,7 @@ libcommon_files = \
        common/entity_name.cc \
        common/ceph_crypto.cc \
        common/ceph_crypto_cms.cc \
+       common/ceph_json.cc \
        common/ipaddr.cc \
        common/pick_address.cc \
        include/addr_parsing.c \
@@ -1590,6 +1589,7 @@ noinst_HEADERS = \
        common/config_opts.h\
        common/ceph_crypto.h\
        common/ceph_crypto_cms.h\
+       common/ceph_json.h\
        common/utf8.h\
        common/mime.h\
        common/pick_address.h\
@@ -1932,7 +1932,6 @@ noinst_HEADERS = \
        rgw/rgw_client_io.h\
        rgw/rgw_fcgi.h\
        rgw/rgw_xml.h\
-       rgw/rgw_json.h\
        rgw/rgw_cache.h\
        rgw/rgw_common.h\
        rgw/rgw_string.h\
diff --git a/src/common/ceph_json.cc b/src/common/ceph_json.cc
new file mode 100644 (file)
index 0000000..536618a
--- /dev/null
@@ -0,0 +1,368 @@
+#include <iostream>
+#include <include/types.h>
+#include <errno.h>
+
+#include "common/ceph_json.h"
+
+// for testing DELETE ME
+#include <fstream>
+
+using namespace std;
+using namespace json_spirit;
+
+#define dout_subsys ceph_subsys_rgw
+
+JSONObjIter::JSONObjIter()
+{
+}
+
+JSONObjIter::~JSONObjIter()
+{
+}
+
+void JSONObjIter::set(const JSONObjIter::map_iter_t &_cur, const JSONObjIter::map_iter_t &_last)
+{
+  cur = _cur;
+  last = _last;
+}
+
+void JSONObjIter::operator++()
+{
+  if (cur != last)
+    ++cur;
+};
+
+JSONObj *JSONObjIter::operator*()
+{
+  return cur->second;
+};
+
+// does not work, FIXME
+ostream& operator<<(ostream& out, JSONObj& obj) {
+   out << obj.name << ": " << obj.data_string;
+   return out;
+}
+
+JSONObj::~JSONObj()
+{
+  multimap<string, JSONObj *>::iterator iter;
+  for (iter = children.begin(); iter != children.end(); ++iter) {
+    JSONObj *obj = iter->second;
+    delete obj;
+  }
+}
+
+
+void JSONObj::add_child(string el, JSONObj *obj)
+{
+  cout << "add_child: " << name << " <- " << el << std::endl;
+  children.insert(pair<string, JSONObj *>(el, obj));
+}
+
+bool JSONObj::get_attr(string name, string& attr)
+{
+  map<string, string>::iterator iter = attr_map.find(name);
+  if (iter == attr_map.end())
+    return false;
+  attr = iter->second;
+  return true;
+}
+
+JSONObjIter JSONObj::find(const string& name)
+{
+  JSONObjIter iter;
+  map<string, JSONObj *>::iterator first;
+  map<string, JSONObj *>::iterator last;
+  first = children.find(name);
+  if (first != children.end()) {
+    last = children.upper_bound(name);
+    iter.set(first, last);
+  }
+  return iter;
+}
+
+JSONObjIter JSONObj::find_first()
+{
+  JSONObjIter iter;
+  iter.set(children.begin(), children.end());
+  return iter;
+}
+
+JSONObjIter JSONObj::find_first(const string& name)
+{
+  JSONObjIter iter;
+  map<string, JSONObj *>::iterator first;
+  first = children.find(name);
+  iter.set(first, children.end());
+  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
+ */
+void JSONObj::handle_value(Value v)
+{
+  if (v.type() == obj_type) {
+    Object temp_obj = v.get_obj();
+    for (Object::size_type i = 0; i < temp_obj.size(); i++) {
+      Pair temp_pair = temp_obj[i];
+      string temp_name = temp_pair.name_;
+      Value temp_value = temp_pair.value_;
+      JSONObj *child = new JSONObj;
+      child->init(this, temp_value, temp_name);
+      add_child(temp_name, child);
+    }
+  } else if (v.type() == array_type) {
+    Array temp_array = v.get_array();
+    Value value;
+
+    for (unsigned j = 0; j < temp_array.size(); j++) {
+      Value cur = temp_array[j];
+      string temp_name;
+
+      JSONObj *child = new JSONObj;
+      child->init(this, cur, temp_name);
+      add_child(child->get_name(), child);
+    }
+  }
+}
+
+void JSONObj::init(JSONObj *p, Value v, string n)
+{
+  name = n;
+  parent = p;
+  data = v;
+
+  handle_value(v);
+  if (v.type() == str_type)
+    data_string =  v.get_str();
+  else
+    data_string =  write(v, raw_utf8);
+  attr_map.insert(pair<string,string>(name, data_string));
+}
+
+JSONObj *JSONObj::get_parent()
+{
+  return parent;
+}
+
+bool JSONObj::is_object()
+{
+  cout << data.type() << std::endl;
+  return (data.type() == obj_type);
+}
+
+bool JSONObj::is_array()
+{
+  return (data.type() == array_type);
+}
+
+vector<string> JSONObj::get_array_elements()
+{
+  vector<string> elements;
+  Array temp_array;
+
+  if (data.type() == array_type)
+    temp_array = data.get_array();
+
+  int array_size = temp_array.size();
+  if (array_size > 0)
+    for (int i = 0; i < array_size; i++) {
+      Value temp_value = temp_array[i];
+      string temp_string;
+      temp_string = write(temp_value, raw_utf8);
+      elements.push_back(temp_string);
+    }
+
+  return elements;
+}
+
+JSONParser::JSONParser() : buf_len(0), success(true)
+{
+}
+
+JSONParser::~JSONParser()
+{
+}
+
+
+
+void JSONParser::handle_data(const char *s, int len)
+{
+  json_buffer.append(s, len); // check for problems with null termination FIXME
+  buf_len += len;
+}
+
+// parse a supplied JSON fragment
+bool JSONParser::parse(const char *buf_, int len)
+{
+  string json_string = buf_;
+  // make a substring to len
+  json_string = json_string.substr(0, len);
+  success = read(json_string, data);
+  if (success)
+    handle_value(data);
+  else
+    set_failure();
+
+  return success;
+}
+
+// parse the internal json_buffer up to len
+bool JSONParser::parse(int len)
+{
+  string json_string = json_buffer.substr(0, len);
+  success = read(json_string, data);
+  if (success)
+    handle_value(data);
+  else
+    set_failure();
+
+  return success;
+}
+
+// parse the complete internal json_buffer
+bool JSONParser::parse()
+{
+  success = read(json_buffer, data);
+  if (success)
+    handle_value(data);
+  else
+    set_failure();
+
+  return success;
+}
+
+// parse a supplied ifstream, for testing. DELETE ME
+bool JSONParser::parse(const char *file_name)
+{
+  ifstream is(file_name);
+  success = read(is, data);
+  if (success)
+    handle_value(data);
+  else
+    set_failure();
+
+  return success;
+}
+
+
+void decode_json_obj(long& val, JSONObj *obj)
+{
+  string s = obj->get_data();
+  const char *start = s.c_str();
+  char *p;
+
+  errno = 0;
+  val = strtol(start, &p, 10);
+
+  /* Check for various possible errors */
+
+ if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
+     (errno != 0 && val == 0)) {
+   throw JSONDecoder::err("failed to parse number");
+ }
+
+ if (p == start) {
+   throw JSONDecoder::err("failed to parse number");
+ }
+
+ while (*p != '\0') {
+   if (!isspace(*p)) {
+     throw JSONDecoder::err("failed to parse number");
+   }
+   p++;
+ }
+}
+
+void decode_json_obj(unsigned long& val, JSONObj *obj)
+{
+  string s = obj->get_data();
+  const char *start = s.c_str();
+  char *p;
+
+  errno = 0;
+  val = strtoul(start, &p, 10);
+
+  /* Check for various possible errors */
+
+ if ((errno == ERANGE && val == ULONG_MAX) ||
+     (errno != 0 && val == 0)) {
+   throw JSONDecoder::err("failed to number");
+ }
+
+ if (p == start) {
+   throw JSONDecoder::err("failed to parse number");
+ }
+
+ while (*p != '\0') {
+   if (!isspace(*p)) {
+     throw JSONDecoder::err("failed to parse number");
+   }
+   p++;
+ }
+}
+
+void decode_json_obj(int& val, JSONObj *obj)
+{
+  long l;
+  decode_json_obj(l, obj);
+#if LONG_MAX > INT_MAX
+  if (l > INT_MAX || l < INT_MIN) {
+    throw JSONDecoder::err("integer out of range");
+  }
+#endif
+
+  val = (int)l;
+}
+
+void decode_json_obj(unsigned& val, JSONObj *obj)
+{
+  unsigned long l;
+  decode_json_obj(l, obj);
+#if ULONG_MAX > UINT_MAX
+  if (l > UINT_MAX) {
+    throw JSONDecoder::err("unsigned integer out of range");
+  }
+#endif
+
+  val = (unsigned)l;
+}
+
+void decode_json_obj(bool& val, JSONObj *obj)
+{
+  string s = obj->get_data();
+  if (strcasecmp(s.c_str(), "true") == 0) {
+    val = true;
+    return;
+  }
+  if (strcasecmp(s.c_str(), "false") == 0) {
+    val = true;
+    return;
+  }
+  int i;
+  decode_json_obj(i, obj);
+  val = (bool)i;
+}
+
diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h
new file mode 100644 (file)
index 0000000..bfbd3fa
--- /dev/null
@@ -0,0 +1,186 @@
+#ifndef CEPH_JSON_H
+#define CEPH_JSON_H
+
+#include <iostream>
+#include <include/types.h>
+
+#include "json_spirit/json_spirit.h"
+
+
+using namespace json_spirit;
+
+
+class JSONObj;
+
+class JSONObjIter {
+  typedef map<string, JSONObj *>::iterator map_iter_t;
+  map_iter_t cur;
+  map_iter_t last;
+
+public:
+  JSONObjIter();
+  ~JSONObjIter();
+  void set(const JSONObjIter::map_iter_t &_cur, const JSONObjIter::map_iter_t &_end);
+
+  void operator++();
+  JSONObj *operator*();
+
+  bool end() {
+    return (cur == last);
+  }
+};
+
+class JSONObj
+{
+  JSONObj *parent;
+protected:
+  string name; // corresponds to obj_type in XMLObj
+  Value data;
+  string data_string;
+  multimap<string, JSONObj *> children;
+  map<string, string> attr_map;
+  void handle_value(Value v);
+
+public:
+
+  JSONObj() : parent(NULL){};
+
+  virtual ~JSONObj();
+
+  void init(JSONObj *p, Value v, string n);
+
+  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(const string& name);
+  JSONObjIter find_first();
+  JSONObjIter find_first(const string& name);
+  JSONObj *find_obj(const string& name);
+
+  friend ostream& operator<<(ostream& out, JSONObj& obj); // does not work, FIXME
+
+  bool is_array();
+  bool is_object();
+  vector<string> get_array_elements();
+};
+
+class JSONParser : public JSONObj
+{
+  int buf_len;
+  string json_buffer;
+  bool success;
+public:
+  JSONParser();
+  virtual ~JSONParser();
+  void handle_data(const char *s, int len);
+
+  bool parse(const char *buf_, int len);
+  bool parse(int len);
+  bool parse();
+  bool parse(const char *file_name);
+
+  const char *get_json() { return json_buffer.c_str(); }
+  void set_failure() { success = false; }
+};
+
+
+class JSONDecoder {
+public:
+  struct err {
+    string message;
+
+    err(const string& m) : message(m) {}
+  };
+
+  JSONParser parser;
+
+  JSONDecoder(bufferlist& bl) {
+    if (!parser.parse(bl.c_str(), bl.length())) {
+      cout << "JSONDecoder::err()" << std::endl;
+      throw JSONDecoder::err("failed to parse JSON input");
+    }
+  }
+
+  template<class T>
+  static bool decode_json(const string& name, T& val, JSONObj *obj, bool mandatory = false);
+
+  template<class T>
+  static void decode_json(const string& name, T& val, T& default_val, JSONObj *obj);
+};
+
+template<class T>
+void decode_json_obj(T& val, JSONObj *obj)
+{
+  val.decode_json(obj);
+}
+
+static inline void decode_json_obj(string& val, JSONObj *obj)
+{
+  val = obj->get_data();
+}
+
+void decode_json_obj(unsigned long& val, JSONObj *obj);
+void decode_json_obj(long& val, JSONObj *obj);
+void decode_json_obj(unsigned& val, JSONObj *obj);
+void decode_json_obj(int& val, JSONObj *obj);
+void decode_json_obj(bool& val, JSONObj *obj);
+
+template<class T>
+void decode_json_obj(list<T>& l, JSONObj *obj)
+{
+  JSONObjIter iter = obj->find_first();
+
+  for (; !iter.end(); ++iter) {
+    T val;
+    JSONObj *o = *iter;
+    decode_json_obj(val, o);
+    l.push_back(val);
+  }
+}
+
+template<class T>
+bool JSONDecoder::decode_json(const string& name, T& val, JSONObj *obj, bool mandatory)
+{
+  JSONObjIter iter = obj->find_first(name);
+  if (iter.end()) {
+    if (mandatory) {
+      string s = "missing mandatory field " + name;
+      throw err(s);
+    }
+    return false;
+  }
+
+  try {
+    decode_json_obj(val, *iter);
+  } catch (err& e) {
+    string s = name + ": ";
+    s.append(e.message);
+    throw err(s);
+  }
+
+  return true;
+}
+
+template<class T>
+void JSONDecoder::decode_json(const string& name, T& val, T& default_val, JSONObj *obj)
+{
+  JSONObjIter iter = obj->find_first(name);
+  if (iter.end()) {
+    val = default_val;
+    return;
+  }
+
+  try {
+    decode_json_obj(val, *iter);
+  } catch (err& e) {
+    val = default_val;
+    string s = name + ": ";
+    s.append(e.message);
+    throw err(s);
+  }
+}
+
+#endif
index fdd48bc7cf7826e80db200d1e761f320318fc17d..3609992750fc9e11494b46775efb77d0054fb640 100644 (file)
@@ -1,11 +1,11 @@
 #include <errno.h>
 
 #include "json_spirit/json_spirit.h"
+#include "common/ceph_json.h"
 
 #include "rgw_common.h"
 #include "rgw_acl.h"
 #include "rgw_string.h"
-#include "rgw_json.h"
 
 #include "common/ceph_crypto.h"
 #include "common/armor.h"
diff --git a/src/rgw/rgw_json.cc b/src/rgw/rgw_json.cc
deleted file mode 100644 (file)
index 3cff30a..0000000
+++ /dev/null
@@ -1,368 +0,0 @@
-#include <iostream>
-#include <include/types.h>
-
-#include "rgw_json.h"
-#include "rgw_common.h"
-
-// for testing DELETE ME
-#include <fstream>
-
-using namespace std;
-using namespace json_spirit;
-
-#define dout_subsys ceph_subsys_rgw
-
-JSONObjIter::JSONObjIter()
-{
-}
-
-JSONObjIter::~JSONObjIter()
-{
-}
-
-void JSONObjIter::set(const JSONObjIter::map_iter_t &_cur, const JSONObjIter::map_iter_t &_last)
-{
-  cur = _cur;
-  last = _last;
-}
-
-void JSONObjIter::operator++()
-{
-  if (cur != last)
-    ++cur;
-};
-
-JSONObj *JSONObjIter::operator*()
-{
-  return cur->second;
-};
-
-// does not work, FIXME
-ostream& operator<<(ostream& out, JSONObj& obj) {
-   out << obj.name << ": " << obj.data_string;
-   return out;
-}
-
-JSONObj::~JSONObj()
-{
-  multimap<string, JSONObj *>::iterator iter;
-  for (iter = children.begin(); iter != children.end(); ++iter) {
-    JSONObj *obj = iter->second;
-    delete obj;
-  }
-}
-
-
-void JSONObj::add_child(string el, JSONObj *obj)
-{
-  cout << "add_child: " << name << " <- " << el << std::endl;
-  children.insert(pair<string, JSONObj *>(el, obj));
-}
-
-bool JSONObj::get_attr(string name, string& attr)
-{
-  map<string, string>::iterator iter = attr_map.find(name);
-  if (iter == attr_map.end())
-    return false;
-  attr = iter->second;
-  return true;
-}
-
-JSONObjIter JSONObj::find(const string& name)
-{
-  JSONObjIter iter;
-  map<string, JSONObj *>::iterator first;
-  map<string, JSONObj *>::iterator last;
-  first = children.find(name);
-  if (first != children.end()) {
-    last = children.upper_bound(name);
-    iter.set(first, last);
-  }
-  return iter;
-}
-
-JSONObjIter JSONObj::find_first()
-{
-  JSONObjIter iter;
-  iter.set(children.begin(), children.end());
-  return iter;
-}
-
-JSONObjIter JSONObj::find_first(const string& name)
-{
-  JSONObjIter iter;
-  map<string, JSONObj *>::iterator first;
-  first = children.find(name);
-  iter.set(first, children.end());
-  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
- */
-void JSONObj::handle_value(Value v)
-{
-  if (v.type() == obj_type) {
-    Object temp_obj = v.get_obj();
-    for (Object::size_type i = 0; i < temp_obj.size(); i++) {
-      Pair temp_pair = temp_obj[i];
-      string temp_name = temp_pair.name_;
-      Value temp_value = temp_pair.value_;
-      JSONObj *child = new JSONObj;
-      child->init(this, temp_value, temp_name);
-      add_child(temp_name, child);
-    }
-  } else if (v.type() == array_type) {
-    Array temp_array = v.get_array();
-    Value value;
-
-    for (unsigned j = 0; j < temp_array.size(); j++) {
-      Value cur = temp_array[j];
-      string temp_name;
-
-      JSONObj *child = new JSONObj;
-      child->init(this, cur, temp_name);
-      add_child(child->get_name(), child);
-    }
-  }
-}
-
-void JSONObj::init(JSONObj *p, Value v, string n)
-{
-  name = n;
-  parent = p;
-  data = v;
-
-  handle_value(v);
-  if (v.type() == str_type)
-    data_string =  v.get_str();
-  else
-    data_string =  write(v, raw_utf8);
-  attr_map.insert(pair<string,string>(name, data_string));
-}
-
-JSONObj *JSONObj::get_parent()
-{
-  return parent;
-}
-
-bool JSONObj::is_object()
-{
-  cout << data.type() << std::endl;
-  return (data.type() == obj_type);
-}
-
-bool JSONObj::is_array()
-{
-  return (data.type() == array_type);
-}
-
-vector<string> JSONObj::get_array_elements()
-{
-  vector<string> elements;
-  Array temp_array;
-
-  if (data.type() == array_type)
-    temp_array = data.get_array();
-
-  int array_size = temp_array.size();
-  if (array_size > 0)
-    for (int i = 0; i < array_size; i++) {
-      Value temp_value = temp_array[i];
-      string temp_string;
-      temp_string = write(temp_value, raw_utf8);
-      elements.push_back(temp_string);
-    }
-
-  return elements;
-}
-
-RGWJSONParser::RGWJSONParser() : buf_len(0), success(true)
-{
-}
-
-RGWJSONParser::~RGWJSONParser()
-{
-}
-
-
-
-void RGWJSONParser::handle_data(const char *s, int len)
-{
-  json_buffer.append(s, len); // check for problems with null termination FIXME
-  buf_len += len;
-}
-
-// parse a supplied JSON fragment
-bool RGWJSONParser::parse(const char *buf_, int len)
-{
-  string json_string = buf_;
-  // make a substring to len
-  json_string = json_string.substr(0, len);
-  success = read(json_string, data);
-  if (success)
-    handle_value(data);
-  else
-    set_failure();
-
-  return success;
-}
-
-// parse the internal json_buffer up to len
-bool RGWJSONParser::parse(int len)
-{
-  string json_string = json_buffer.substr(0, len);
-  success = read(json_string, data);
-  if (success)
-    handle_value(data);
-  else
-    set_failure();
-
-  return success;
-}
-
-// parse the complete internal json_buffer
-bool RGWJSONParser::parse()
-{
-  success = read(json_buffer, data);
-  if (success)
-    handle_value(data);
-  else
-    set_failure();
-
-  return success;
-}
-
-// parse a supplied ifstream, for testing. DELETE ME
-bool RGWJSONParser::parse(const char *file_name)
-{
-  ifstream is(file_name);
-  success = read(is, data);
-  if (success)
-    handle_value(data);
-  else
-    set_failure();
-
-  return success;
-}
-
-
-void decode_json_obj(long& val, JSONObj *obj)
-{
-  string s = obj->get_data();
-  const char *start = s.c_str();
-  char *p;
-
-  errno = 0;
-  val = strtol(start, &p, 10);
-
-  /* Check for various possible errors */
-
- if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
-     (errno != 0 && val == 0)) {
-   throw JSONDecoder::err("failed to parse number");
- }
-
- if (p == start) {
-   throw JSONDecoder::err("failed to parse number");
- }
-
- while (*p != '\0') {
-   if (!isspace(*p)) {
-     throw JSONDecoder::err("failed to parse number");
-   }
-   p++;
- }
-}
-
-void decode_json_obj(unsigned long& val, JSONObj *obj)
-{
-  string s = obj->get_data();
-  const char *start = s.c_str();
-  char *p;
-
-  errno = 0;
-  val = strtoul(start, &p, 10);
-
-  /* Check for various possible errors */
-
- if ((errno == ERANGE && val == ULONG_MAX) ||
-     (errno != 0 && val == 0)) {
-   throw JSONDecoder::err("failed to number");
- }
-
- if (p == start) {
-   throw JSONDecoder::err("failed to parse number");
- }
-
- while (*p != '\0') {
-   if (!isspace(*p)) {
-     throw JSONDecoder::err("failed to parse number");
-   }
-   p++;
- }
-}
-
-void decode_json_obj(int& val, JSONObj *obj)
-{
-  long l;
-  decode_json_obj(l, obj);
-#if LONG_MAX > INT_MAX
-  if (l > INT_MAX || l < INT_MIN) {
-    throw JSONDecoder::err("integer out of range");
-  }
-#endif
-
-  val = (int)l;
-}
-
-void decode_json_obj(unsigned& val, JSONObj *obj)
-{
-  unsigned long l;
-  decode_json_obj(l, obj);
-#if ULONG_MAX > UINT_MAX
-  if (l > UINT_MAX) {
-    throw JSONDecoder::err("unsigned integer out of range");
-  }
-#endif
-
-  val = (unsigned)l;
-}
-
-void decode_json_obj(bool& val, JSONObj *obj)
-{
-  string s = obj->get_data();
-  if (strcasecmp(s.c_str(), "true") == 0) {
-    val = true;
-    return;
-  }
-  if (strcasecmp(s.c_str(), "false") == 0) {
-    val = true;
-    return;
-  }
-  int i;
-  decode_json_obj(i, obj);
-  val = (bool)i;
-}
-
diff --git a/src/rgw/rgw_json.h b/src/rgw/rgw_json.h
deleted file mode 100644 (file)
index 4b6f00d..0000000
+++ /dev/null
@@ -1,186 +0,0 @@
-#ifndef RGW_JSON_H
-#define RGW_JSON_H
-
-#include <iostream>
-#include <include/types.h>
-
-#include "json_spirit/json_spirit.h"
-
-
-using namespace json_spirit;
-
-
-class JSONObj;
-
-class JSONObjIter {
-  typedef map<string, JSONObj *>::iterator map_iter_t;
-  map_iter_t cur;
-  map_iter_t last;
-
-public:
-  JSONObjIter();
-  ~JSONObjIter();
-  void set(const JSONObjIter::map_iter_t &_cur, const JSONObjIter::map_iter_t &_end);
-
-  void operator++();
-  JSONObj *operator*();
-
-  bool end() {
-    return (cur == last);
-  }
-};
-
-class JSONObj
-{
-  JSONObj *parent;
-protected:
-  string name; // corresponds to obj_type in XMLObj
-  Value data;
-  string data_string;
-  multimap<string, JSONObj *> children;
-  map<string, string> attr_map;
-  void handle_value(Value v);
-
-public:
-
-  JSONObj() : parent(NULL){};
-
-  virtual ~JSONObj();
-
-  void init(JSONObj *p, Value v, string n);
-
-  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(const string& name);
-  JSONObjIter find_first();
-  JSONObjIter find_first(const string& name);
-  JSONObj *find_obj(const string& name);
-
-  friend ostream& operator<<(ostream& out, JSONObj& obj); // does not work, FIXME
-
-  bool is_array();
-  bool is_object();
-  vector<string> get_array_elements();
-};
-
-class RGWJSONParser : public JSONObj
-{
-  int buf_len;
-  string json_buffer;
-  bool success;
-public:
-  RGWJSONParser();
-  virtual ~RGWJSONParser();
-  void handle_data(const char *s, int len);
-
-  bool parse(const char *buf_, int len);
-  bool parse(int len);
-  bool parse();
-  bool parse(const char *file_name);
-
-  const char *get_json() { return json_buffer.c_str(); }
-  void set_failure() { success = false; }
-};
-
-
-class JSONDecoder {
-public:
-  struct err {
-    string message;
-
-    err(const string& m) : message(m) {}
-  };
-
-  RGWJSONParser parser;
-
-  JSONDecoder(bufferlist& bl) {
-    if (!parser.parse(bl.c_str(), bl.length())) {
-      cout << "JSONDecoder::err()" << std::endl;
-      throw JSONDecoder::err("failed to parse JSON input");
-    }
-  }
-
-  template<class T>
-  static bool decode_json(const string& name, T& val, JSONObj *obj, bool mandatory = false);
-
-  template<class T>
-  static void decode_json(const string& name, T& val, T& default_val, JSONObj *obj);
-};
-
-template<class T>
-void decode_json_obj(T& val, JSONObj *obj)
-{
-  val.decode_json(obj);
-}
-
-static inline void decode_json_obj(string& val, JSONObj *obj)
-{
-  val = obj->get_data();
-}
-
-void decode_json_obj(unsigned long& val, JSONObj *obj);
-void decode_json_obj(long& val, JSONObj *obj);
-void decode_json_obj(unsigned& val, JSONObj *obj);
-void decode_json_obj(int& val, JSONObj *obj);
-void decode_json_obj(bool& val, JSONObj *obj);
-
-template<class T>
-void decode_json_obj(list<T>& l, JSONObj *obj)
-{
-  JSONObjIter iter = obj->find_first();
-
-  for (; !iter.end(); ++iter) {
-    T val;
-    JSONObj *o = *iter;
-    decode_json_obj(val, o);
-    l.push_back(val);
-  }
-}
-
-template<class T>
-bool JSONDecoder::decode_json(const string& name, T& val, JSONObj *obj, bool mandatory)
-{
-  JSONObjIter iter = obj->find_first(name);
-  if (iter.end()) {
-    if (mandatory) {
-      string s = "missing mandatory field " + name;
-      throw err(s);
-    }
-    return false;
-  }
-
-  try {
-    decode_json_obj(val, *iter);
-  } catch (err& e) {
-    string s = name + ": ";
-    s.append(e.message);
-    throw err(s);
-  }
-
-  return true;
-}
-
-template<class T>
-void JSONDecoder::decode_json(const string& name, T& val, T& default_val, JSONObj *obj)
-{
-  JSONObjIter iter = obj->find_first(name);
-  if (iter.end()) {
-    val = default_val;
-    return;
-  }
-
-  try {
-    decode_json_obj(val, *iter);
-  } catch (err& e) {
-    val = default_val;
-    string s = name + ": ";
-    s.append(e.message);
-    throw err(s);
-  }
-}
-
-#endif
index 820cc6216b640603a8686d70a988dad36e0f25ab..ec1c4ce2355740ed69cd2135d98d0f0fc37248f2 100644 (file)
@@ -7,8 +7,8 @@
 #include "include/types.h"
 
 #include "common/Formatter.h"
+#include "common/ceph_json.h"
 
-#include "rgw_json.h"
 #include "rgw_common.h"
 
 #define dout_subsys ceph_subsys_rgw
@@ -55,7 +55,7 @@ struct UserInfo {
 
 
 int main(int argc, char **argv) {
-  RGWJSONParser parser;
+  JSONParser parser;
 
   char buf[1024];
   bufferlist bl;
index c0a88b485b2e8391655a5e7a3a81dd2c0540fbe6..fac05f18884b6fc547d4eb3d11e46a8be46f221d 100644 (file)
@@ -1,8 +1,8 @@
 
 #include <errno.h>
 
+#include "common/ceph_json.h"
 #include "rgw_policy_s3.h"
-#include "rgw_json.h"
 #include "rgw_common.h"
 
 
@@ -229,7 +229,7 @@ int RGWPolicy::check(RGWPolicyEnv *env, string& err_msg)
 
 int RGWPolicy::from_json(bufferlist& bl, string& err_msg)
 {
-  RGWJSONParser parser;
+  JSONParser parser;
 
   if (!parser.parse(bl.c_str(), bl.length())) {
     err_msg = "Malformed JSON";
index 58d93fcead49819ac6f377333be9cde208036c52..59117dc876a969649b81ba69f3c597c117e40623 100644 (file)
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-#include "rgw_json.h"
+#include "common/ceph_json.h"
 #include "rgw_common.h"
 #include "rgw_swift.h"
 #include "rgw_swift_auth.h"
@@ -102,7 +102,7 @@ int RGWSwift::validate_token(const char *token, struct rgw_swift_auth_info *info
 
 int KeystoneToken::parse(CephContext *cct, bufferlist& bl)
 {
-  RGWJSONParser parser;
+  JSONParser parser;
 
   if (!parser.parse(bl.c_str(), bl.length())) {
     ldout(cct, 0) << "malformed json" << dendl;
@@ -404,7 +404,7 @@ int RGWSwift::check_revoked()
 
   ldout(cct, 10) << "request returned " << bl.c_str() << dendl;
 
-  RGWJSONParser parser;
+  JSONParser parser;
 
   if (!parser.parse(bl.c_str(), bl.length())) {
     ldout(cct, 0) << "malformed json" << dendl;
@@ -438,7 +438,7 @@ int RGWSwift::check_revoked()
 
   ldout(cct, 10) << "ceph_decode_cms: decoded: " << json.c_str() << dendl;
 
-  RGWJSONParser list_parser;
+  JSONParser list_parser;
   if (!list_parser.parse(json.c_str(), json.length())) {
     ldout(cct, 0) << "malformed json" << dendl;
     return -EINVAL;