From 8b12a186b645a6c6bc71e84be5cfc66a3a90dd73 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Tue, 12 Feb 2013 09:40:12 -0800 Subject: rgw: add encode_json 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 --- src/common/ceph_json.h | 36 +++++++++++++++++++++++++++++------- src/rgw/rgw_common.cc | 12 +++++++++++- src/rgw/rgw_common.h | 2 ++ 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 #include +#include #include "json_spirit/json_spirit.h" +#include "Formatter.h" using namespace json_spirit; @@ -105,10 +107,10 @@ public: } template - 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 - 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 @@ -142,12 +144,12 @@ void decode_json_obj(list& l, JSONObj *obj) } template -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 -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 +static void encode_json(const char *name, const T& val, Formatter *f) +{ + f->open_object_section(name); + val.dump(f); + f->close_section(); +} + +template +static void encode_json(const char *name, const std::list& l, Formatter *f) +{ + f->open_array_section(name); + for (typename std::list::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::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 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::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::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(); } -- cgit v1.2.1