From d42fdfc7718e6f09f5bc5cb4073da2ddb897e4f5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 12 Apr 2012 14:09:33 +1000 Subject: [PATCH] Add indentation to json output --- json.cpp.rl | 49 +++++++++++++++++++++++++++---------------------- json.hpp | 30 ++++++++++++++++-------------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/json.cpp.rl b/json.cpp.rl index 298b13ec..92155ec0 100644 --- a/json.cpp.rl +++ b/json.cpp.rl @@ -362,17 +362,20 @@ json::object::operator[](const std::string &key) const { std::ostream& -json::object::print (std::ostream &os) const { - os << "{"; +json::object::write (std::ostream &os) const { + os << "{\n"; + { + indenter raii(os); - for (auto i = m_values.begin (); i != m_values.end ();) { - os << '"' << i->first << "\" : " << *i->second; + for (auto i = m_values.begin (); i != m_values.end ();) { + os << '"' << i->first << "\" : " << *i->second; - if (++i != m_values.end ()) - os << ",\n"; + if (++i != m_values.end ()) + os << ",\n"; + } } - os << "}"; + os << "\n}"; return os; } @@ -404,17 +407,19 @@ json::array::operator ==(const json::array &rhs) const { std::ostream& -json::array::print (std::ostream &os) const { - os << "[ "; +json::array::write (std::ostream &os) const { + os << "[\n"; + { + indenter raii(os); - for (auto i = m_values.begin (); i != m_values.end (); ++i) { - os << (*i)->print (os); + for (auto i = m_values.begin (); i != m_values.end (); ++i) { + (*i)->write (os); - if (i != m_values.end () - 1) - os << ", "; + if (i != m_values.end () - 1) + os << ",\n"; + } } - - os << "]"; + os << "\n]"; return os; } @@ -424,7 +429,7 @@ json::array::print (std::ostream &os) const { */ std::ostream& -json::string::print (std::ostream &os) const { +json::string::write (std::ostream &os) const { os << '"' << m_value << '"'; return os; } @@ -440,8 +445,8 @@ json::string::operator ==(const json::string &rhs) const */ std::ostream& -json::number::print (std::ostream &os) const { - os << '"' << m_value << '"'; +json::number::write (std::ostream &os) const { + os << m_value; return os; } @@ -456,8 +461,8 @@ json::number::operator ==(const json::number &rhs) const */ std::ostream& -json::boolean::print (std::ostream &os) const { - os << '"' << (m_value ? "true" : "false") << '"'; +json::boolean::write (std::ostream &os) const { + os << (m_value ? "true" : "false"); return os; } @@ -471,11 +476,11 @@ json::boolean::operator ==(const json::boolean &rhs) const */ std::ostream& -json::null::print (std::ostream &os) const { +json::null::write (std::ostream &os) const { os << "null"; return os; } ostream& operator <<(ostream &os, const json::node &n) - { return n.print (os); } + { return n.write (os); } diff --git a/json.hpp b/json.hpp index e68bea5e..70ce1636 100644 --- a/json.hpp +++ b/json.hpp @@ -43,6 +43,8 @@ namespace json { extern std::unique_ptr parse (const char *start); extern std::unique_ptr parse (const std::string&); + extern void write (const json::node&, std::ostream&); + /// Abstract base for all JSON values class node { public: @@ -54,12 +56,12 @@ namespace json { virtual const number& to_number (void) const; virtual const boolean& to_boolean (void) const; - virtual bool is_object (void) const { return false; } - virtual bool is_array (void) const { return false; } - virtual bool is_string (void) const { return false; } - virtual bool is_number (void) const { return false; } - virtual bool is_boolean (void) const { return false; } - virtual bool is_null (void) const { return false; } + virtual bool is_object (void) const { return false; } + virtual bool is_array (void) const { return false; } + virtual bool is_string (void) const { return false; } + virtual bool is_number (void) const { return false; } + virtual bool is_boolean (void) const { return false; } + virtual bool is_null (void) const { return false; } virtual bool operator==(const node &rhs) const = 0; virtual bool operator!=(const node &rhs) const; @@ -76,7 +78,7 @@ namespace json { virtual const node& operator[] (const std::string&) const; virtual const node& operator[] (unsigned int) const; - virtual std::ostream& print (std::ostream &os) const = 0; + virtual std::ostream& write (std::ostream &os) const = 0; }; @@ -98,8 +100,7 @@ namespace json { virtual void insert (const std::string &_key, node *value); virtual const node& operator[](const std::string &key) const; - virtual std::ostream& print (std::ostream &os) const; - + virtual std::ostream& write (std::ostream &os) const; }; @@ -133,7 +134,7 @@ namespace json { virtual void insert (json::node *_value); - virtual std::ostream& print (std::ostream &os) const; + virtual std::ostream& write (std::ostream &os) const; }; @@ -155,7 +156,7 @@ namespace json { operator const std::string&(void) const { return m_value; } const std::string& native (void) const { return m_value; } - virtual std::ostream& print (std::ostream &os) const; + virtual std::ostream& write (std::ostream &os) const; }; @@ -177,7 +178,7 @@ namespace json { operator double(void) const { return m_value; } double native (void) const { return m_value; } - virtual std::ostream& print (std::ostream &os) const; + virtual std::ostream& write (std::ostream &os) const; }; @@ -198,7 +199,7 @@ namespace json { operator bool (void) const { return m_value; } bool native (void) const { return m_value; } - virtual std::ostream& print (std::ostream &os) const; + virtual std::ostream& write (std::ostream &os) const; }; @@ -206,10 +207,11 @@ namespace json { class null : public node { public: virtual bool is_null (void) const { return true; } - virtual std::ostream& print (std::ostream &os) const; virtual bool operator==(const null&) const { return true; } virtual bool operator==(const node &rhs) const { return rhs == *this; } + + virtual std::ostream& write (std::ostream &os) const; };