#define CEPH_JSON_H
#include <stdexcept>
+#include <typeindex>
#include <include/types.h>
#include <boost/container/flat_map.hpp>
return true;
}
+class JSONEncodeFilter
+{
+public:
+ class HandlerBase {
+ public:
+ virtual ~HandlerBase() {}
+
+ virtual std::type_index get_type() = 0;
+ virtual void encode_json(const char *name, const void *pval, ceph::Formatter *) const = 0;
+ };
+
+ template <class T>
+ class Handler : public HandlerBase {
+ public:
+ virtual ~Handler() {}
+
+ std::type_index get_type() override {
+ return std::type_index(typeid(const T&));
+ }
+ };
+
+private:
+ std::map<std::type_index, HandlerBase *> handlers;
+
+public:
+ void register_type(HandlerBase *h) {
+ handlers[h->get_type()] = h;
+ }
+
+ template <class T>
+ bool encode_json(const char *name, const T& val, ceph::Formatter *f) {
+ auto iter = handlers.find(std::type_index(typeid(val)));
+ if (iter == handlers.end()) {
+ return false;
+ }
+
+ iter->second->encode_json(name, (const void *)&val, f);
+ return true;
+ }
+};
+
template<class T>
-static void encode_json(const char *name, const T& val, ceph::Formatter *f)
+static void encode_json_impl(const char *name, const T& val, ceph::Formatter *f)
{
f->open_object_section(name);
val.dump(f);
f->close_section();
}
+template<class T>
+static void encode_json(const char *name, const T& val, ceph::Formatter *f)
+{
+ JSONEncodeFilter *filter = static_cast<JSONEncodeFilter *>(f->get_external_feature_handler("JSONEncodeFilter"));
+
+ if (!filter ||
+ !filter->encode_json(name, val, f)) {
+ encode_json_impl(name, val, f);
+ }
+}
+
class utime_t;
void encode_json(const char *name, const std::string& val, ceph::Formatter *f);