Add indentation to json output

This commit is contained in:
Danny Robson 2012-04-12 14:09:33 +10:00
parent a13c1b9183
commit d42fdfc771
2 changed files with 43 additions and 36 deletions

View File

@ -362,17 +362,20 @@ json::object::operator[](const std::string &key) const {
std::ostream& std::ostream&
json::object::print (std::ostream &os) const { json::object::write (std::ostream &os) const {
os << "{"; os << "{\n";
{
indenter raii(os);
for (auto i = m_values.begin (); i != m_values.end ();) { for (auto i = m_values.begin (); i != m_values.end ();) {
os << '"' << i->first << "\" : " << *i->second; os << '"' << i->first << "\" : " << *i->second;
if (++i != m_values.end ()) if (++i != m_values.end ())
os << ",\n"; os << ",\n";
}
} }
os << "}"; os << "\n}";
return os; return os;
} }
@ -404,17 +407,19 @@ json::array::operator ==(const json::array &rhs) const {
std::ostream& std::ostream&
json::array::print (std::ostream &os) const { json::array::write (std::ostream &os) const {
os << "[ "; os << "[\n";
{
indenter raii(os);
for (auto i = m_values.begin (); i != m_values.end (); ++i) { for (auto i = m_values.begin (); i != m_values.end (); ++i) {
os << (*i)->print (os); (*i)->write (os);
if (i != m_values.end () - 1) if (i != m_values.end () - 1)
os << ", "; os << ",\n";
}
} }
os << "\n]";
os << "]";
return os; return os;
} }
@ -424,7 +429,7 @@ json::array::print (std::ostream &os) const {
*/ */
std::ostream& std::ostream&
json::string::print (std::ostream &os) const { json::string::write (std::ostream &os) const {
os << '"' << m_value << '"'; os << '"' << m_value << '"';
return os; return os;
} }
@ -440,8 +445,8 @@ json::string::operator ==(const json::string &rhs) const
*/ */
std::ostream& std::ostream&
json::number::print (std::ostream &os) const { json::number::write (std::ostream &os) const {
os << '"' << m_value << '"'; os << m_value;
return os; return os;
} }
@ -456,8 +461,8 @@ json::number::operator ==(const json::number &rhs) const
*/ */
std::ostream& std::ostream&
json::boolean::print (std::ostream &os) const { json::boolean::write (std::ostream &os) const {
os << '"' << (m_value ? "true" : "false") << '"'; os << (m_value ? "true" : "false");
return os; return os;
} }
@ -471,11 +476,11 @@ json::boolean::operator ==(const json::boolean &rhs) const
*/ */
std::ostream& std::ostream&
json::null::print (std::ostream &os) const { json::null::write (std::ostream &os) const {
os << "null"; os << "null";
return os; return os;
} }
ostream& ostream&
operator <<(ostream &os, const json::node &n) operator <<(ostream &os, const json::node &n)
{ return n.print (os); } { return n.write (os); }

View File

@ -43,6 +43,8 @@ namespace json {
extern std::unique_ptr<node> parse (const char *start); extern std::unique_ptr<node> parse (const char *start);
extern std::unique_ptr<node> parse (const std::string&); extern std::unique_ptr<node> parse (const std::string&);
extern void write (const json::node&, std::ostream&);
/// Abstract base for all JSON values /// Abstract base for all JSON values
class node { class node {
public: public:
@ -54,12 +56,12 @@ namespace json {
virtual const number& to_number (void) const; virtual const number& to_number (void) const;
virtual const boolean& to_boolean (void) const; virtual const boolean& to_boolean (void) const;
virtual bool is_object (void) const { return false; } virtual bool is_object (void) const { return false; }
virtual bool is_array (void) const { return false; } virtual bool is_array (void) const { return false; }
virtual bool is_string (void) const { return false; } virtual bool is_string (void) const { return false; }
virtual bool is_number (void) const { return false; } virtual bool is_number (void) const { return false; }
virtual bool is_boolean (void) const { return false; } virtual bool is_boolean (void) const { return false; }
virtual bool is_null (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 = 0;
virtual bool operator!=(const node &rhs) const; 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[] (const std::string&) const;
virtual const node& operator[] (unsigned int) 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 void insert (const std::string &_key, node *value);
virtual const node& operator[](const std::string &key) const; 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 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; } operator const std::string&(void) const { return m_value; }
const std::string& native (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; } operator double(void) const { return m_value; }
double native (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; } operator bool (void) const { return m_value; }
bool native (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 { class null : public node {
public: public:
virtual bool is_null (void) const { return true; } 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 null&) const { return true; }
virtual bool operator==(const node &rhs) const virtual bool operator==(const node &rhs) const
{ return rhs == *this; } { return rhs == *this; }
virtual std::ostream& write (std::ostream &os) const;
}; };