#include "include/int_types.h"
#include "include/buffer_fwd.h"
-#include <boost/utility/string_view.hpp>
-
#include <deque>
#include <list>
#include <vector>
class Formatter {
public:
- static Formatter *create(boost::string_view type,
- boost::string_view default_type,
- boost::string_view fallback);
- static Formatter *create(boost::string_view type,
- boost::string_view default_type) {
+ class ObjectSection {
+ Formatter& formatter;
+
+ public:
+ ObjectSection(Formatter& f, const char *name) : formatter(f) {
+ formatter.open_object_section(name);
+ }
+ ObjectSection(Formatter& f, const char *name, const char *ns) : formatter(f) {
+ formatter.open_object_section_in_ns(name, ns);
+ }
+ ~ObjectSection() {
+ formatter.close_section();
+ }
+ };
+ class ArraySection {
+ Formatter& formatter;
+
+ public:
+ ArraySection(Formatter& f, const char *name) : formatter(f) {
+ formatter.open_array_section(name);
+ }
+ ArraySection(Formatter& f, const char *name, const char *ns) : formatter(f) {
+ formatter.open_array_section_in_ns(name, ns);
+ }
+ ~ArraySection() {
+ formatter.close_section();
+ }
+ };
+
+ static Formatter *create(std::string_view type,
+ std::string_view default_type,
+ std::string_view fallback);
+ static Formatter *create(std::string_view type,
+ std::string_view default_type) {
return create(type, default_type, "");
}
- static Formatter *create(boost::string_view type) {
+ static Formatter *create(std::string_view type) {
return create(type, "json-pretty", "");
}
virtual void dump_unsigned(const char *name, uint64_t u) = 0;
virtual void dump_int(const char *name, int64_t s) = 0;
virtual void dump_float(const char *name, double d) = 0;
- virtual void dump_string(const char *name, boost::string_view s) = 0;
+ virtual void dump_string(const char *name, std::string_view s) = 0;
virtual void dump_bool(const char *name, bool b)
{
dump_format_unquoted(name, "%s", (b ? "true" : "false"));
{
open_object_section(name);
}
- virtual void dump_string_with_attrs(const char *name, boost::string_view s, const FormatterAttrs& attrs)
+ virtual void dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs)
{
dump_string(name, s);
}
};
+ class copyable_sstream : public std::stringstream {
+ public:
+ copyable_sstream() {}
+ copyable_sstream(const copyable_sstream& rhs) {
+ str(rhs.str());
+ }
+ copyable_sstream& operator=(const copyable_sstream& rhs) {
+ str(rhs.str());
+ return *this;
+ }
+ };
+
class JSONFormatter : public Formatter {
public:
explicit JSONFormatter(bool p = false);
void open_object_section_in_ns(const char *name, const char *ns) override;
void close_section() override;
void dump_unsigned(const char *name, uint64_t u) override;
- void dump_int(const char *name, int64_t u) override;
+ void dump_int(const char *name, int64_t s) override;
void dump_float(const char *name, double d) override;
- void dump_string(const char *name, boost::string_view s) override;
+ void dump_string(const char *name, std::string_view s) override;
std::ostream& dump_stream(const char *name) override;
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
int get_len() const override;
void write_raw_data(const char *data) override;
+ protected:
+ virtual bool handle_value(const char *name, std::string_view s, bool quoted) {
+ return false; /* is handling done? */
+ }
+
+ virtual bool handle_open_section(const char *name, const char *ns, bool is_array) {
+ return false; /* is handling done? */
+ }
+
+ virtual bool handle_close_section() {
+ return false; /* is handling done? */
+ }
+
private:
struct json_formatter_stack_entry_d {
};
bool m_pretty;
- void open_section(const char *name, bool is_array);
- void print_quoted_string(boost::string_view s);
+ void open_section(const char *name, const char *ns, bool is_array);
+ void print_quoted_string(std::string_view s);
void print_name(const char *name);
void print_comma(json_formatter_stack_entry_d& entry);
void finish_pending_string();
- std::stringstream m_ss, m_pending_string;
+ template <class T>
+ void add_value(const char *name, T val);
+ void add_value(const char *name, std::string_view val, bool quoted);
+
+ copyable_sstream m_ss;
+ copyable_sstream m_pending_string;
+ std::string m_pending_name;
std::list<json_formatter_stack_entry_d> m_stack;
bool m_is_pending_string;
bool m_line_break_enabled = false;
};
+ template <class T>
+ void add_value(const char *name, T val);
+
class XMLFormatter : public Formatter {
public:
static const char *XML_1_DTD;
void open_object_section_in_ns(const char *name, const char *ns) override;
void close_section() override;
void dump_unsigned(const char *name, uint64_t u) override;
- void dump_int(const char *name, int64_t u) override;
+ void dump_int(const char *name, int64_t s) override;
void dump_float(const char *name, double d) override;
- void dump_string(const char *name, boost::string_view s) override;
+ void dump_string(const char *name, std::string_view s) override;
std::ostream& dump_stream(const char *name) override;
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
int get_len() const override;
/* with attrs */
void open_array_section_with_attrs(const char *name, const FormatterAttrs& attrs) override;
void open_object_section_with_attrs(const char *name, const FormatterAttrs& attrs) override;
- void dump_string_with_attrs(const char *name, boost::string_view s, const FormatterAttrs& attrs) override;
+ void dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs) override;
protected:
void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
void finish_pending_string();
void print_spaces();
- static std::string escape_xml_str(boost::string_view str);
void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
char to_lower_underscore(char c) const;
std::string m_pending_string_name;
bool m_header_done;
bool m_line_break_enabled = false;
+ private:
+ template <class T>
+ void add_value(const char *name, T val);
};
class TableFormatter : public Formatter {
void close_section() override;
void dump_unsigned(const char *name, uint64_t u) override;
- void dump_int(const char *name, int64_t u) override;
+ void dump_int(const char *name, int64_t s) override;
void dump_float(const char *name, double d) override;
- void dump_string(const char *name, boost::string_view s) override;
+ void dump_string(const char *name, std::string_view s) override;
void dump_format_va(const char *name, const char *ns, bool quoted, const char *fmt, va_list ap) override;
- void dump_string_with_attrs(const char *name, boost::string_view s, const FormatterAttrs& attrs) override;
+ void dump_string_with_attrs(const char *name, std::string_view s, const FormatterAttrs& attrs) override;
std::ostream& dump_stream(const char *name) override;
int get_len() const override;
void get_attrs_str(const FormatterAttrs *attrs, std::string& attrs_str);
private:
+ template <class T>
+ void add_value(const char *name, T val);
void open_section_in_ns(const char *name, const char *ns, const FormatterAttrs *attrs);
std::vector< std::vector<std::pair<std::string, std::string> > > m_vec;
std::stringstream m_ss;
std::vector< std::string > m_column_name;
};
-
}
#endif