Eliminate a bunch of pointlessness in json::Writer

Originally committed to SVN as r5753.
This commit is contained in:
Thomas Goyne 2011-10-17 22:00:49 +00:00
parent dd46c7af7f
commit a75aa7a633
2 changed files with 32 additions and 56 deletions

View file

@ -17,27 +17,18 @@ 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);
template <typename ElementTypeT>
static void Write(const ElementTypeT& element, std::ostream& ostr);
private:
Writer(std::ostream& ostr);
template <typename ElementTypeT>
static void Write_i(const ElementTypeT& element, std::ostream& ostr);
void Write_i(const Object& object);
void Write_i(const Array& array);
void Write_i(const String& string);
void Write_i(const Number& number);
void Write_i(const Boolean& boolean);
void Write_i(const Null& null);
void Write_i(const UnknownElement& unknown);
void Write(const Object& object);
void Write(const Array& array);
void Write(const String& string);
void Write(const Number& number);
void Write(const Boolean& boolean);
void Write(const Null& null);
void Write(const UnknownElement& unknown);
virtual void Visit(const Array& array);
virtual void Visit(const Object& object);
@ -53,5 +44,4 @@ private:
} // End namespace
#include "writer.inl"

View file

@ -21,30 +21,20 @@ TODO:
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 Writer::Writer(std::ostream& ostr) :
m_ostr(ostr),
m_nTabDepth(0)
{}
template <typename ElementTypeT>
void Writer::Write_i(const ElementTypeT& element, std::ostream& ostr)
void Writer::Write(const ElementTypeT& element, std::ostream& ostr)
{
Writer writer(ostr);
writer.Write_i(element);
writer.Write(element);
ostr.flush(); // all done
}
inline void Writer::Write_i(const Array& array)
inline void Writer::Write(const Array& array)
{
if (array.empty())
m_ostr << "[]";
@ -53,12 +43,11 @@ inline void Writer::Write_i(const Array& array)
m_ostr << '[' << std::endl;
++m_nTabDepth;
Array::const_iterator it(array.begin()),
itend(array.end());
Array::const_iterator it(array.begin()), itend(array.end());
while (it != itend) {
m_ostr << std::string(m_nTabDepth, '\t');
Write_i(*it);
Write(*it);
if (++it != itend)
m_ostr << ',';
@ -70,7 +59,7 @@ inline void Writer::Write_i(const Array& array)
}
}
inline void Writer::Write_i(const Object& object)
inline void Writer::Write(const Object& object)
{
if (object.empty())
m_ostr << "{}";
@ -79,11 +68,10 @@ inline void Writer::Write_i(const Object& object)
m_ostr << '{' << std::endl;
++m_nTabDepth;
Object::const_iterator it(object.begin()),
itend(object.end());
Object::const_iterator it(object.begin()), itend(object.end());
while (it != itend) {
m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->first << "\" : ";
Write_i(it->second);
Write(it->second);
if (++it != itend)
m_ostr << ',';
@ -95,17 +83,17 @@ inline void Writer::Write_i(const Object& object)
}
}
inline void Writer::Write_i(const Number& numberElement)
inline void Writer::Write(const Number& numberElement)
{
m_ostr << std::setprecision(20) << numberElement;
}
inline void Writer::Write_i(const Boolean& booleanElement)
inline void Writer::Write(const Boolean& booleanElement)
{
m_ostr << (booleanElement ? "true" : "false");
}
inline void Writer::Write_i(const String& stringElement)
inline void Writer::Write(const String& stringElement)
{
m_ostr << '"';
@ -130,23 +118,21 @@ inline void Writer::Write_i(const String& stringElement)
m_ostr << '"';
}
inline void Writer::Write_i(const Null& )
inline void Writer::Write(const Null& )
{
m_ostr << "null";
}
inline void Writer::Write_i(const UnknownElement& unknown)
inline void Writer::Write(const UnknownElement& unknown)
{
unknown.Accept(*this);
}
inline void Writer::Visit(const Array& array) { Write_i(array); }
inline void Writer::Visit(const Object& object) { Write_i(object); }
inline void Writer::Visit(const Number& number) { Write_i(number); }
inline void Writer::Visit(const String& string) { Write_i(string); }
inline void Writer::Visit(const Boolean& boolean) { Write_i(boolean); }
inline void Writer::Visit(const Null& null) { Write_i(null); }
inline void Writer::Visit(const Array& array) { Write(array); }
inline void Writer::Visit(const Object& object) { Write(object); }
inline void Writer::Visit(const Number& number) { Write(number); }
inline void Writer::Visit(const String& string) { Write(string); }
inline void Writer::Visit(const Boolean& boolean) { Write(boolean); }
inline void Writer::Visit(const Null& null) { Write(null); }
} // end namespace