Index: writer.h =================================================================== --- writer.h (revision 15446) +++ writer.h (working copy) @@ -4,6 +4,11 @@ License: BSD Project Webpage: http://cajun-jsonapi.sourceforge.net/ Author: Terry Caton +Modified to include a boolean that turns off indenting, +which returns a single-line string + +$Id$ + ***********************************************/ #pragma once @@ -17,19 +22,20 @@ namespace json class Writer : private ConstVisitor { public: - static void Write(const Object& object, std::ostream& ostr); - static void Write(const Array& array, std::ostream& ostr); - static void Write(const String& string, std::ostream& ostr); - static void Write(const Number& number, std::ostream& ostr); - static void Write(const Boolean& boolean, std::ostream& ostr); - static void Write(const Null& null, std::ostream& ostr); - static void Write(const UnknownElement& elementRoot, std::ostream& ostr); + static void Write(const Object& object, std::ostream& ostr, bool indent = true); + static void Write(const Array& array, std::ostream& ostr, bool indent = true); + static void Write(const String& string, std::ostream& ostr, bool indent = true); + static void Write(const Number& number, std::ostream& ostr, bool indent = true); + static void Write(const Boolean& boolean, std::ostream& ostr, bool indent = true); + static void Write(const Null& null, std::ostream& ostr, bool indent = true); + static void Write(const UnknownElement& elementRoot, std::ostream& ostr, bool indent = true); private: Writer(std::ostream& ostr); + Writer(std::ostream& ostr, bool indent); template - static void Write_i(const ElementTypeT& element, std::ostream& ostr); + static void Write_i(const ElementTypeT& element, std::ostream& ostr, bool indent); void Write_i(const Object& object); void Write_i(const Array& array); @@ -48,6 +54,7 @@ private: std::ostream& m_ostr; int m_nTabDepth; + bool m_indent; }; Index: writer.inl =================================================================== --- writer.inl (revision 15446) +++ writer.inl (working copy) @@ -4,6 +4,11 @@ License: BSD Project Webpage: http://cajun-jsonapi.sourceforge.net/ Author: Terry Caton +Modified to include a boolean that turns off indenting, +which returns a single-line string + +$Id$ + ***********************************************/ #include "writer.h" @@ -22,24 +27,25 @@ namespace json { -inline void Writer::Write(const UnknownElement& elementRoot, std::ostream& ostr) { Write_i(elementRoot, ostr); } -inline void Writer::Write(const Object& object, std::ostream& ostr) { Write_i(object, ostr); } -inline void Writer::Write(const Array& array, std::ostream& ostr) { Write_i(array, ostr); } -inline void Writer::Write(const Number& number, std::ostream& ostr) { Write_i(number, ostr); } -inline void Writer::Write(const String& string, std::ostream& ostr) { Write_i(string, ostr); } -inline void Writer::Write(const Boolean& boolean, std::ostream& ostr) { Write_i(boolean, ostr); } -inline void Writer::Write(const Null& null, std::ostream& ostr) { Write_i(null, ostr); } +inline void Writer::Write(const UnknownElement& elementRoot, std::ostream& ostr, bool indent) { Write_i(elementRoot, ostr, indent); } +inline void Writer::Write(const Object& object, std::ostream& ostr, bool indent) { Write_i(object, ostr, indent); } +inline void Writer::Write(const Array& array, std::ostream& ostr, bool indent) { Write_i(array, ostr, indent); } +inline void Writer::Write(const Number& number, std::ostream& ostr, bool indent) { Write_i(number, ostr, indent); } +inline void Writer::Write(const String& string, std::ostream& ostr, bool indent) { Write_i(string, ostr, indent); } +inline void Writer::Write(const Boolean& boolean, std::ostream& ostr, bool indent) { Write_i(boolean, ostr, indent); } +inline void Writer::Write(const Null& null, std::ostream& ostr, bool indent) { Write_i(null, ostr, indent); } -inline Writer::Writer(std::ostream& ostr) : +inline Writer::Writer(std::ostream& ostr, bool indent) : m_ostr(ostr), - m_nTabDepth(0) + m_nTabDepth(0), + m_indent(indent) {} template -void Writer::Write_i(const ElementTypeT& element, std::ostream& ostr) +void Writer::Write_i(const ElementTypeT& element, std::ostream& ostr, bool indent) { - Writer writer(ostr); + Writer writer(ostr, indent); writer.Write_i(element); ostr.flush(); // all done } @@ -50,23 +56,38 @@ inline void Writer::Write_i(const Array& m_ostr << "[]"; else { - m_ostr << '[' << std::endl; - ++m_nTabDepth; + m_ostr << '['; + if (m_indent) + { + m_ostr << std::endl; + ++m_nTabDepth; + } + else + m_ostr << ' '; Array::const_iterator it(array.Begin()), itEnd(array.End()); while (it != itEnd) { - m_ostr << std::string(m_nTabDepth, '\t'); + if (m_indent) + m_ostr << std::string(m_nTabDepth, '\t'); Write_i(*it); if (++it != itEnd) m_ostr << ','; - m_ostr << std::endl; + if (m_indent) + m_ostr << std::endl; + else + m_ostr << ' '; } - --m_nTabDepth; - m_ostr << std::string(m_nTabDepth, '\t') << ']'; + if (m_indent) + { + --m_nTabDepth; + m_ostr << std::string(m_nTabDepth, '\t'); + } + + m_ostr << ']'; } } @@ -76,22 +97,38 @@ inline void Writer::Write_i(const Object m_ostr << "{}"; else { - m_ostr << '{' << std::endl; - ++m_nTabDepth; + m_ostr << '{'; + if (m_indent) + { + m_ostr << std::endl; + ++m_nTabDepth; + } + else + m_ostr << ' '; Object::const_iterator it(object.Begin()), itEnd(object.End()); while (it != itEnd) { - m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->name << "\" : "; + if (m_indent) + m_ostr << std::string(m_nTabDepth, '\t'); + m_ostr << '"' << it->name << "\" : "; Write_i(it->element); if (++it != itEnd) m_ostr << ','; - m_ostr << std::endl; + if (m_indent) + m_ostr << std::endl; + else + m_ostr << ' '; + } + + if (m_indent) + { + --m_nTabDepth; + m_ostr << std::string(m_nTabDepth, '\t'); } - --m_nTabDepth; - m_ostr << std::string(m_nTabDepth, '\t') << '}'; + m_ostr << '}'; } }