From fa553e1b46b30ced6ee5d9962d506002720895f0 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Wed, 25 Jul 2018 13:35:15 -0700 Subject: [PATCH] common/ceph_json: formattable encoding fixes Signed-off-by: Yehuda Sadeh --- src/common/ceph_json.cc | 53 +++++++++++++++++++++++++++++++++++++++++ src/common/ceph_json.h | 52 ++++++++++------------------------------ 2 files changed, 66 insertions(+), 39 deletions(-) diff --git a/src/common/ceph_json.cc b/src/common/ceph_json.cc index 0203f51f988d7..cc12a620211c2 100644 --- a/src/common/ceph_json.cc +++ b/src/common/ceph_json.cc @@ -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 */ +} + diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h index 195284c60d3a3..d83e6aae72164 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -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& 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(); } -- 2.39.5