From: Adam C. Emerson Date: Tue, 10 Sep 2019 22:53:58 +0000 (-0400) Subject: common/ceph_json: Support en/decoding Boost's flat containers X-Git-Tag: v16.1.0~1154^2~15 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2d4c9acc98bff8531a5204d4dd4dfced5a17756c;p=ceph.git common/ceph_json: Support en/decoding Boost's flat containers Specifically boost::container::flat_map and boost::container::flat_set Signed-off-by: Adam C. Emerson --- diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h index 041f52455b8..185d0c36443 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "common/ceph_time.h" @@ -219,6 +220,21 @@ void decode_json_obj(std::set& l, JSONObj *obj) } } +template +void decode_json_obj(boost::container::flat_set& l, JSONObj *obj) +{ + l.clear(); + + JSONObjIter iter = obj->find_first(); + + for (; !iter.end(); ++iter) { + T val; + JSONObj *o = *iter; + decode_json_obj(val, o); + l.insert(val); + } +} + template void decode_json_obj(std::vector& l, JSONObj *obj) { @@ -251,6 +267,23 @@ void decode_json_obj(std::map& m, JSONObj *obj) } } +template > +void decode_json_obj(boost::container::flat_map& m, JSONObj *obj) +{ + m.clear(); + + JSONObjIter iter = obj->find_first(); + + for (; !iter.end(); ++iter) { + K key; + V val; + JSONObj *o = *iter; + JSONDecoder::decode_json("key", key, o); + JSONDecoder::decode_json("val", val, o); + m[key] = val; + } +} + template void decode_json_obj(std::multimap& m, JSONObj *obj) { @@ -524,6 +557,18 @@ static void encode_json(const char *name, const std::set& l, ceph::F f->close_section(); } +template > +static void encode_json(const char *name, + const boost::container::flat_set& l, + ceph::Formatter *f) +{ + f->open_array_section(name); + for (auto iter = l.cbegin(); iter != l.cend(); ++iter) { + encode_json("obj", *iter, f); + } + f->close_section(); +} + template static void encode_json(const char *name, const std::vector& l, ceph::Formatter *f) { @@ -547,6 +592,19 @@ static void encode_json(const char *name, const std::map& m, ceph::Form f->close_section(); } +template > +static void encode_json(const char *name, const boost::container::flat_map& m, ceph::Formatter *f) +{ + f->open_array_section(name); + for (auto i = m.cbegin(); i != m.cend(); ++i) { + f->open_object_section("entry"); + encode_json("key", i->first, f); + encode_json("val", i->second, f); + f->close_section(); + } + f->close_section(); +} + template static void encode_json(const char *name, const std::multimap& m, ceph::Formatter *f) { @@ -642,6 +700,66 @@ static void encode_json(const char *name, const std::optional& o, ceph::Forma } +template +void encode_json_map(const char *name, const boost::container::flat_map& m, ceph::Formatter *f) +{ + f->open_array_section(name); + for (auto iter = m.cbegin(); iter != m.cend(); ++iter) { + encode_json("obj", iter->second, f); + } + f->close_section(); +} + + +template +void encode_json_map(const char *name, const char *index_name, + const char *object_name, const char *value_name, + void (*cb)(const char *, const V&, ceph::Formatter *, void *), void *parent, + const boost::container::flat_map& m, ceph::Formatter *f) +{ + f->open_array_section(name); + for (auto iter = m.cbegin(); iter != m.cend(); ++iter) { + if (index_name) { + f->open_object_section("key_value"); + f->dump_string(index_name, iter->first); + } + + if (object_name) { + f->open_object_section(object_name); + } + + if (cb) { + cb(value_name, iter->second, f, parent); + } else { + encode_json(value_name, iter->second, f); + } + + if (object_name) { + f->close_section(); + } + if (index_name) { + f->close_section(); + } + } + f->close_section(); +} + +template +void encode_json_map(const char *name, const char *index_name, + const char *object_name, const char *value_name, + const boost::container::flat_map& m, ceph::Formatter *f) +{ + encode_json_map(name, index_name, object_name, value_name, NULL, NULL, m, f); +} + +template +void encode_json_map(const char *name, const char *index_name, const char *value_name, + const boost::container::flat_map& m, ceph::Formatter *f) +{ + encode_json_map(name, index_name, NULL, value_name, NULL, NULL, m, f); +} + + class JSONFormattable : public ceph::JSONFormatter { JSONObj::data_val value; std::vector arr;