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,8 +362,10 @@ 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;
@ -371,8 +373,9 @@ json::object::print (std::ostream &os) const {
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);
(*i)->write (os);
if (i != m_values.end () - 1)
os << ", ";
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); }

View File

@ -43,6 +43,8 @@ namespace json {
extern std::unique_ptr<node> parse (const char *start);
extern std::unique_ptr<node> parse (const std::string&);
extern void write (const json::node&, std::ostream&);
/// Abstract base for all JSON values
class node {
public:
@ -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;
};