diff options
author | Yehuda Sadeh <yehuda@inktank.com> | 2013-02-12 09:40:12 -0800 |
---|---|---|
committer | Yehuda Sadeh <yehuda@inktank.com> | 2013-02-12 09:40:12 -0800 |
commit | 8b12a186b645a6c6bc71e84be5cfc66a3a90dd73 (patch) | |
tree | 2970a515f7a59732bfa269b021138dc8adc7e08f | |
parent | adca757223b4118ffcf1810264e320d7bd263053 (diff) | |
download | ceph-8b12a186b645a6c6bc71e84be5cfc66a3a90dd73.tar.gz |
rgw: add encode_jsonwip-json-decode
dump() just dumps the internal content of an object, encode_json()
create the object inside its own section. Note that there are cases
where we don't want an object to be surrounded by a section, e.g.,
when an object represents an array. In such a case we'd need to
override the encode_json() for this object type.
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
-rw-r--r-- | src/common/ceph_json.h | 36 | ||||
-rw-r--r-- | src/rgw/rgw_common.cc | 12 | ||||
-rw-r--r-- | src/rgw/rgw_common.h | 2 | ||||
-rw-r--r-- | src/rgw/rgw_json_enc.cc | 16 |
4 files changed, 49 insertions, 17 deletions
diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h index bfbd3fa6c5b..ce91ec45ae0 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -3,8 +3,10 @@ #include <iostream> #include <include/types.h> +#include <list> #include "json_spirit/json_spirit.h" +#include "Formatter.h" using namespace json_spirit; @@ -105,10 +107,10 @@ public: } template<class T> - static bool decode_json(const string& name, T& val, JSONObj *obj, bool mandatory = false); + static bool decode_json(const char *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); + static void decode_json(const char *name, T& val, T& default_val, JSONObj *obj); }; template<class T> @@ -142,12 +144,12 @@ void decode_json_obj(list<T>& l, JSONObj *obj) } template<class T> -bool JSONDecoder::decode_json(const string& name, T& val, JSONObj *obj, bool mandatory) +bool JSONDecoder::decode_json(const char *name, T& val, JSONObj *obj, bool mandatory) { JSONObjIter iter = obj->find_first(name); if (iter.end()) { if (mandatory) { - string s = "missing mandatory field " + name; + string s = "missing mandatory field " + string(name); throw err(s); } return false; @@ -156,7 +158,7 @@ bool JSONDecoder::decode_json(const string& name, T& val, JSONObj *obj, bool man try { decode_json_obj(val, *iter); } catch (err& e) { - string s = name + ": "; + string s = string(name) + ": "; s.append(e.message); throw err(s); } @@ -165,7 +167,7 @@ bool JSONDecoder::decode_json(const string& name, T& val, JSONObj *obj, bool man } template<class T> -void JSONDecoder::decode_json(const string& name, T& val, T& default_val, JSONObj *obj) +void JSONDecoder::decode_json(const char *name, T& val, T& default_val, JSONObj *obj) { JSONObjIter iter = obj->find_first(name); if (iter.end()) { @@ -177,10 +179,30 @@ void JSONDecoder::decode_json(const string& name, T& val, T& default_val, JSONOb decode_json_obj(val, *iter); } catch (err& e) { val = default_val; - string s = name + ": "; + string s = string(name) + ": "; s.append(e.message); throw err(s); } } +template<class T> +static void encode_json(const char *name, const T& val, Formatter *f) +{ + f->open_object_section(name); + val.dump(f); + f->close_section(); +} + +template<class T> +static void encode_json(const char *name, const std::list<T>& l, Formatter *f) +{ + f->open_array_section(name); + for (typename std::list<T>::const_iterator iter = l.begin(); iter != l.end(); ++iter) { + f->open_object_section("obj"); + encode_json(name, *iter, f); + f->close_section(); + } + f->close_section(); +} + #endif diff --git a/src/rgw/rgw_common.cc b/src/rgw/rgw_common.cc index 0fcc941a4a5..9bc1866ca55 100644 --- a/src/rgw/rgw_common.cc +++ b/src/rgw/rgw_common.cc @@ -707,7 +707,12 @@ int RGWUserCaps::remove_from_string(const string& str) void RGWUserCaps::dump(Formatter *f) const { - f->open_array_section("caps"); + dump(f, "caps"); +} + +void RGWUserCaps::dump(Formatter *f, const char *name) const +{ + f->open_array_section(name); map<string, uint32_t>::const_iterator iter; for (iter = caps.begin(); iter != caps.end(); ++iter) { @@ -748,6 +753,11 @@ struct RGWUserCap { } }; +void encode_json(const char *name, const RGWUserCaps& val, Formatter *f) +{ + val.dump(f, name); +} + void RGWUserCaps::decode_json(JSONObj *obj) { list<RGWUserCap> caps_list; diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index 3a01e2ab49c..a5aa97cce47 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -353,11 +353,13 @@ public: } int check_cap(const string& cap, uint32_t perm); void dump(Formatter *f) const; + void dump(Formatter *f, const char *name) const; void decode_json(JSONObj *obj); }; WRITE_CLASS_ENCODER(RGWUserCaps); +void encode_json(const char *name, const RGWUserCaps& val, Formatter *f); struct RGWUserInfo { diff --git a/src/rgw/rgw_json_enc.cc b/src/rgw/rgw_json_enc.cc index c6465a29e85..cc3d33eae46 100644 --- a/src/rgw/rgw_json_enc.cc +++ b/src/rgw/rgw_json_enc.cc @@ -195,16 +195,13 @@ void RGWCacheNotifyInfo::dump(Formatter *f) const void RGWAccessKey::dump(Formatter *f) const { - f->open_object_section("key"); f->dump_string("access_key", id); f->dump_string("secret_key", key); f->dump_string("subuser", subuser); - f->close_section(); } void RGWAccessKey::dump(Formatter *f, const string& user, bool swift) const { - f->open_object_section("key"); string u = user; if (!subuser.empty()) { u.append(":"); @@ -215,7 +212,6 @@ void RGWAccessKey::dump(Formatter *f, const string& user, bool swift) const f->dump_string("access_key", id); } f->dump_string("secret_key", key); - f->close_section(); } void RGWAccessKey::decode_json(JSONObj *obj) { @@ -292,17 +288,14 @@ static void perm_to_str(uint32_t mask, char *buf, int len) void RGWSubUser::dump(Formatter *f) const { - f->open_object_section("subuser"); f->dump_string("id", name); char buf[256]; perm_to_str(perm_mask, buf, sizeof(buf)); f->dump_string("permissions", buf); - f->close_section(); } void RGWSubUser::dump(Formatter *f, const string& user) const { - f->open_object_section("subuser"); string s = user; s.append(":"); s.append(name); @@ -310,7 +303,6 @@ void RGWSubUser::dump(Formatter *f, const string& user) const char buf[256]; perm_to_str(perm_mask, buf, sizeof(buf)); f->dump_string("permissions", buf); - f->close_section(); } static uint32_t str_to_perm(const string& s) @@ -353,25 +345,31 @@ void RGWUserInfo::dump(Formatter *f) const map<string, RGWSubUser>::const_iterator siter = subusers.begin(); f->open_array_section("subusers"); for (; siter != subusers.end(); ++siter) { + f->open_object_section("subuser"); siter->second.dump(f, user_id); + f->close_section(); } f->close_section(); map<string, RGWAccessKey>::const_iterator aiter = access_keys.begin(); f->open_array_section("keys"); for (; aiter != access_keys.end(); ++aiter) { + f->open_object_section("key"); aiter->second.dump(f, user_id, false); + f->close_section(); } f->close_section(); aiter = swift_keys.begin(); f->open_array_section("swift_keys"); for (; aiter != swift_keys.end(); ++aiter) { + f->open_object_section("key"); aiter->second.dump(f, user_id, true); + f->close_section(); } f->close_section(); - caps.dump(f); + encode_json("caps", caps, f); f->close_section(); } |