]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/ceph_json: formattable encoding fixes
authorYehuda Sadeh <yehuda@redhat.com>
Wed, 25 Jul 2018 20:35:15 +0000 (13:35 -0700)
committerYehuda Sadeh <yehuda@redhat.com>
Tue, 11 Dec 2018 08:10:42 +0000 (00:10 -0800)
Signed-off-by: Yehuda Sadeh <yehuda@redhat.com>
src/common/ceph_json.cc
src/common/ceph_json.h

index 0203f51f988d7d3f37501a824968b60f4ae03d25..cc12a620211c20740529624032c8e00e4334685c 100644 (file)
@@ -610,6 +610,14 @@ int JSONFormattable::val_int() const {
   return atoi(value.str.c_str());
 }
 
+long JSONFormattable::val_long() const {
+  return atol(value.str.c_str());
+}
+
+long long JSONFormattable::val_long_long() const {
+  return atoll(value.str.c_str());
+}
+
 bool JSONFormattable::val_bool() const {
   return (boost::iequals(value.str, "true") ||
           boost::iequals(value.str, "on") ||
@@ -882,3 +890,48 @@ void JSONFormattable::encode_json(const char *name, Formatter *f) const
   }
 }
 
+bool JSONFormattable::handle_value(const char *name, std::string_view s, bool quoted) {
+  JSONFormattable *new_val;
+  if (cur_enc->is_array()) {
+    cur_enc->arr.push_back(JSONFormattable());
+    new_val = &cur_enc->arr.back();
+  } else {
+    cur_enc->set_type(JSONFormattable::FMT_OBJ);
+    new_val  = &cur_enc->obj[name];
+  }
+  new_val->set_type(JSONFormattable::FMT_VALUE);
+  new_val->value.set(s, quoted);
+
+  return false;
+}
+bool JSONFormattable::handle_open_section(const char *name, const char *ns, bool section_is_array) {
+  if (cur_enc->is_array()) {
+    cur_enc->arr.push_back(JSONFormattable());
+    cur_enc = &cur_enc->arr.back();
+  } else if (enc_stack.size() > 1) {
+      /* only open a new section if already nested,
+       * otherwise root is the container
+       */
+      cur_enc = &cur_enc->obj[name];
+  }
+  enc_stack.push_back(cur_enc);
+
+  if (section_is_array) {
+    cur_enc->set_type(JSONFormattable::FMT_ARRAY);
+  } else {
+    cur_enc->set_type(JSONFormattable::FMT_OBJ);
+  }
+
+  return false; /* continue processing */
+}
+
+bool JSONFormattable::handle_close_section() {
+  if (enc_stack.size() <= 1) {
+    return false;
+  }
+
+  enc_stack.pop_back();
+  cur_enc = enc_stack.back();
+  return false; /* continue processing */
+}
+
index 195284c60d3a3fdaae6e61329c42d7ef62fe815d..d83e6aae7216455915d6c0a6b51db899ce2e1757 100644 (file)
@@ -500,45 +500,9 @@ class JSONFormattable : public ceph::JSONFormatter {
   JSONFormattable *cur_enc;
 
 protected:
-  bool handle_value(const char *name, std::string_view s, bool quoted) override {
-    JSONFormattable *new_val;
-    if (cur_enc->is_array()) {
-      cur_enc->arr.push_back(JSONFormattable());
-      new_val = &cur_enc->arr.back();
-    } else {
-      new_val  = &cur_enc->obj[name];
-    }
-    new_val->set_type(JSONFormattable::FMT_VALUE);
-    new_val->value.set(s, quoted);
-
-    return false;
-  }
-  bool handle_open_section(const char *name, const char *ns, bool section_is_array) override {
-    if (cur_enc->is_array()) {
-      cur_enc->arr.push_back(JSONFormattable());
-      cur_enc = &cur_enc->arr.back();
-    } else {
-      cur_enc = &obj[name];
-    }
-    enc_stack.push_back(cur_enc);
-
-    if (section_is_array) {
-      cur_enc->set_type(JSONFormattable::FMT_ARRAY);
-    } else {
-      cur_enc->set_type(JSONFormattable::FMT_OBJ);
-    }
-
-    return false; /* continue processing */
-  }
-  bool handle_close_section() override {
-    if (enc_stack.size() <= 1) {
-      return false;
-    }
-
-    enc_stack.pop_back();
-    cur_enc = enc_stack.back();
-    return false; /* continue processing */
-  }
+  bool handle_value(const char *name, std::string_view s, bool quoted) override;
+  bool handle_open_section(const char *name, const char *ns, bool section_is_array) override;
+  bool handle_close_section() override;
 
 public:
   JSONFormattable(bool p = false) : JSONFormatter(p) {
@@ -605,6 +569,8 @@ public:
   }
 
   int val_int() const;
+  long val_long() const;
+  long long val_long_long() const;
   bool val_bool() const;
 
   const vector<JSONFormattable>& array() const {
@@ -625,6 +591,14 @@ public:
     return val_int();
   }
 
+  explicit operator long() const {
+    return val_long();
+  }
+
+  explicit operator long long() const {
+    return val_long_long();
+  }
+
   explicit operator bool() const {
     return val_bool();
   }