template<class T>
static bool decode_json(const char *name, T& val, JSONObj *obj, bool mandatory = false);
+ template<class C>
+ static bool decode_json(const char *name, C& container, void (*cb)(C&, JSONObj *obj), JSONObj *obj, bool mandatory = false);
+
template<class T>
static void decode_json(const char *name, T& val, T& default_val, JSONObj *obj);
};
}
}
+template<class C>
+void decode_json_obj(C& container, void (*cb)(C&, JSONObj *obj), JSONObj *obj)
+{
+ JSONObjIter iter = obj->find_first();
+
+ for (; !iter.end(); ++iter) {
+ JSONObj *o = *iter;
+ cb(container, o);
+ }
+}
+
template<class T>
bool JSONDecoder::decode_json(const char *name, T& val, JSONObj *obj, bool mandatory)
{
return true;
}
+template<class C>
+bool JSONDecoder::decode_json(const char *name, C& container, void (*cb)(C&, JSONObj *), JSONObj *obj, bool mandatory)
+{
+ JSONObjIter iter = obj->find_first(name);
+ if (iter.end()) {
+ if (mandatory) {
+ string s = "missing mandatory field " + string(name);
+ throw err(s);
+ }
+ return false;
+ }
+
+ try {
+ decode_json_obj(container, cb, *iter);
+ } catch (err& e) {
+ string s = string(name) + ": ";
+ s.append(e.message);
+ throw err(s);
+ }
+
+ return true;
+}
+
template<class T>
void JSONDecoder::decode_json(const char *name, T& val, T& default_val, JSONObj *obj)
{
}
-struct SwiftKeyEntry {
- RGWAccessKey key;
+static void decode_access_keys(map<string, RGWAccessKey>& m, JSONObj *o)
+{
+ RGWAccessKey k;
+ k.decode_json(o);
+ m[k.id] = k;
+}
- void decode_json(JSONObj *obj) {
- key.decode_json(obj, true);
- }
-};
+static void decode_swift_keys(map<string, RGWAccessKey>& m, JSONObj *o)
+{
+ RGWAccessKey k;
+ k.decode_json(o, true);
+ m[k.subuser] = k;
+}
+
+static void decode_subusers(map<string, RGWSubUser>& m, JSONObj *o)
+{
+ RGWSubUser u;
+ u.decode_json(o);
+ m[u.name] = u;
+}
void RGWUserInfo::decode_json(JSONObj *obj)
{
JSONDecoder::decode_json("max_buckets", max_buckets, obj);
JSONDecoder::decode_json("auid", auid, obj);
- list<RGWAccessKey> akeys_list;
- JSONDecoder::decode_json("keys", akeys_list, obj);
-
- list<RGWAccessKey>::iterator iter;
- for (iter = akeys_list.begin(); iter != akeys_list.end(); ++iter) {
- RGWAccessKey& e = *iter;
- access_keys[e.id] = e;
- }
-
- list<SwiftKeyEntry> skeys_list;
- list<SwiftKeyEntry>::iterator skiter;
- JSONDecoder::decode_json("swift_keys", skeys_list, obj);
-
- for (skiter = skeys_list.begin(); skiter != skeys_list.end(); ++skiter) {
- SwiftKeyEntry& e = *skiter;
- swift_keys[e.key.subuser] = e.key;
- }
-
- list<RGWSubUser> susers_list;
- list<RGWSubUser>::iterator siter;
- JSONDecoder::decode_json("subusers", susers_list, obj);
-
- for (siter = susers_list.begin(); siter != susers_list.end(); ++siter) {
- RGWSubUser& e = *siter;
- subusers[e.name] = e;
- }
+ JSONDecoder::decode_json("keys", access_keys, decode_access_keys, obj);
+ JSONDecoder::decode_json("swift_keys", swift_keys, decode_swift_keys, obj);
+ JSONDecoder::decode_json("subusers", subusers, decode_subusers, obj);
JSONDecoder::decode_json("caps", caps, obj);
}