From: Yehuda Sadeh Date: Wed, 6 Feb 2013 20:56:54 +0000 (-0800) Subject: moved rgw/rgw_json.* common/ceph_json.* X-Git-Tag: v0.62~159^2~5 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=96ad9b149f22c08ad1510833f45d9afcb90d3289;p=ceph.git moved rgw/rgw_json.* common/ceph_json.* Signed-off-by: Yehuda Sadeh --- diff --git a/src/Makefile.am b/src/Makefile.am index 81015e7fcd61..2cc40622e644 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 000000000000..536618aa7d83 --- /dev/null +++ b/src/common/ceph_json.cc @@ -0,0 +1,368 @@ +#include +#include +#include + +#include "common/ceph_json.h" + +// for testing DELETE ME +#include + +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::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(el, obj)); +} + +bool JSONObj::get_attr(string name, string& attr) +{ + map::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::iterator first; + map::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::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(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 JSONObj::get_array_elements() +{ + vector 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 index 000000000000..bfbd3fa6c5b9 --- /dev/null +++ b/src/common/ceph_json.h @@ -0,0 +1,186 @@ +#ifndef CEPH_JSON_H +#define CEPH_JSON_H + +#include +#include + +#include "json_spirit/json_spirit.h" + + +using namespace json_spirit; + + +class JSONObj; + +class JSONObjIter { + typedef map::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 children; + map 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 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 + static bool decode_json(const string& name, T& val, JSONObj *obj, bool mandatory = false); + + template + static void decode_json(const string& name, T& val, T& default_val, JSONObj *obj); +}; + +template +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 +void decode_json_obj(list& 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 +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 +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 diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index fdd48bc7cf78..3609992750fc 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -1,11 +1,11 @@ #include #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 index 3cff30aa37bd..000000000000 --- a/src/rgw/rgw_json.cc +++ /dev/null @@ -1,368 +0,0 @@ -#include -#include - -#include "rgw_json.h" -#include "rgw_common.h" - -// for testing DELETE ME -#include - -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::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(el, obj)); -} - -bool JSONObj::get_attr(string name, string& attr) -{ - map::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::iterator first; - map::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::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(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 JSONObj::get_array_elements() -{ - vector 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 index 4b6f00d92863..000000000000 --- a/src/rgw/rgw_json.h +++ /dev/null @@ -1,186 +0,0 @@ -#ifndef RGW_JSON_H -#define RGW_JSON_H - -#include -#include - -#include "json_spirit/json_spirit.h" - - -using namespace json_spirit; - - -class JSONObj; - -class JSONObjIter { - typedef map::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 children; - map 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 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 - static bool decode_json(const string& name, T& val, JSONObj *obj, bool mandatory = false); - - template - static void decode_json(const string& name, T& val, T& default_val, JSONObj *obj); -}; - -template -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 -void decode_json_obj(list& 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 -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 -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 diff --git a/src/rgw/rgw_jsonparser.cc b/src/rgw/rgw_jsonparser.cc index 820cc6216b64..ec1c4ce23557 100644 --- a/src/rgw/rgw_jsonparser.cc +++ b/src/rgw/rgw_jsonparser.cc @@ -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; diff --git a/src/rgw/rgw_policy_s3.cc b/src/rgw/rgw_policy_s3.cc index c0a88b485b2e..fac05f18884b 100644 --- a/src/rgw/rgw_policy_s3.cc +++ b/src/rgw/rgw_policy_s3.cc @@ -1,8 +1,8 @@ #include +#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"; diff --git a/src/rgw/rgw_swift.cc b/src/rgw/rgw_swift.cc index 58d93fcead49..59117dc876a9 100644 --- a/src/rgw/rgw_swift.cc +++ b/src/rgw/rgw_swift.cc @@ -2,7 +2,7 @@ #include #include -#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;