forked from mia/Aegisub
Eliminate a bunch of pointlessness in json::Writer
Originally committed to SVN as r5753.
This commit is contained in:
parent
dd46c7af7f
commit
a75aa7a633
2 changed files with 32 additions and 56 deletions
|
@ -17,27 +17,18 @@ namespace json
|
||||||
class Writer : private ConstVisitor
|
class Writer : private ConstVisitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void Write(const Object& object, std::ostream& ostr);
|
template <typename ElementTypeT>
|
||||||
static void Write(const Array& array, std::ostream& ostr);
|
static void Write(const ElementTypeT& element, 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);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Writer(std::ostream& ostr);
|
Writer(std::ostream& ostr);
|
||||||
|
void Write(const Object& object);
|
||||||
template <typename ElementTypeT>
|
void Write(const Array& array);
|
||||||
static void Write_i(const ElementTypeT& element, std::ostream& ostr);
|
void Write(const String& string);
|
||||||
|
void Write(const Number& number);
|
||||||
void Write_i(const Object& object);
|
void Write(const Boolean& boolean);
|
||||||
void Write_i(const Array& array);
|
void Write(const Null& null);
|
||||||
void Write_i(const String& string);
|
void Write(const UnknownElement& unknown);
|
||||||
void Write_i(const Number& number);
|
|
||||||
void Write_i(const Boolean& boolean);
|
|
||||||
void Write_i(const Null& null);
|
|
||||||
void Write_i(const UnknownElement& unknown);
|
|
||||||
|
|
||||||
virtual void Visit(const Array& array);
|
virtual void Visit(const Array& array);
|
||||||
virtual void Visit(const Object& object);
|
virtual void Visit(const Object& object);
|
||||||
|
@ -53,5 +44,4 @@ private:
|
||||||
|
|
||||||
} // End namespace
|
} // End namespace
|
||||||
|
|
||||||
|
|
||||||
#include "writer.inl"
|
#include "writer.inl"
|
||||||
|
|
|
@ -10,7 +10,7 @@ Author: Terry Caton
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
* better documentation
|
* better documentation
|
||||||
|
@ -21,30 +21,20 @@ TODO:
|
||||||
namespace json
|
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) :
|
inline Writer::Writer(std::ostream& ostr) :
|
||||||
m_ostr(ostr),
|
m_ostr(ostr),
|
||||||
m_nTabDepth(0)
|
m_nTabDepth(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template <typename ElementTypeT>
|
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 writer(ostr);
|
||||||
writer.Write_i(element);
|
writer.Write(element);
|
||||||
ostr.flush(); // all done
|
ostr.flush(); // all done
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Writer::Write_i(const Array& array)
|
inline void Writer::Write(const Array& array)
|
||||||
{
|
{
|
||||||
if (array.empty())
|
if (array.empty())
|
||||||
m_ostr << "[]";
|
m_ostr << "[]";
|
||||||
|
@ -53,12 +43,11 @@ inline void Writer::Write_i(const Array& array)
|
||||||
m_ostr << '[' << std::endl;
|
m_ostr << '[' << std::endl;
|
||||||
++m_nTabDepth;
|
++m_nTabDepth;
|
||||||
|
|
||||||
Array::const_iterator it(array.begin()),
|
Array::const_iterator it(array.begin()), itend(array.end());
|
||||||
itend(array.end());
|
|
||||||
while (it != itend) {
|
while (it != itend) {
|
||||||
m_ostr << std::string(m_nTabDepth, '\t');
|
m_ostr << std::string(m_nTabDepth, '\t');
|
||||||
|
|
||||||
Write_i(*it);
|
Write(*it);
|
||||||
|
|
||||||
if (++it != itend)
|
if (++it != itend)
|
||||||
m_ostr << ',';
|
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())
|
if (object.empty())
|
||||||
m_ostr << "{}";
|
m_ostr << "{}";
|
||||||
|
@ -79,11 +68,10 @@ inline void Writer::Write_i(const Object& object)
|
||||||
m_ostr << '{' << std::endl;
|
m_ostr << '{' << std::endl;
|
||||||
++m_nTabDepth;
|
++m_nTabDepth;
|
||||||
|
|
||||||
Object::const_iterator it(object.begin()),
|
Object::const_iterator it(object.begin()), itend(object.end());
|
||||||
itend(object.end());
|
|
||||||
while (it != itend) {
|
while (it != itend) {
|
||||||
m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->first << "\" : ";
|
m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->first << "\" : ";
|
||||||
Write_i(it->second);
|
Write(it->second);
|
||||||
|
|
||||||
if (++it != itend)
|
if (++it != itend)
|
||||||
m_ostr << ',';
|
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;
|
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");
|
m_ostr << (booleanElement ? "true" : "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Writer::Write_i(const String& stringElement)
|
inline void Writer::Write(const String& stringElement)
|
||||||
{
|
{
|
||||||
m_ostr << '"';
|
m_ostr << '"';
|
||||||
|
|
||||||
|
@ -127,26 +115,24 @@ inline void Writer::Write_i(const String& stringElement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_ostr << '"';
|
m_ostr << '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Writer::Write_i(const Null& )
|
inline void Writer::Write(const Null& )
|
||||||
{
|
{
|
||||||
m_ostr << "null";
|
m_ostr << "null";
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Writer::Write_i(const UnknownElement& unknown)
|
inline void Writer::Write(const UnknownElement& unknown)
|
||||||
{
|
{
|
||||||
unknown.Accept(*this);
|
unknown.Accept(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Writer::Visit(const Array& array) { Write_i(array); }
|
inline void Writer::Visit(const Array& array) { Write(array); }
|
||||||
inline void Writer::Visit(const Object& object) { Write_i(object); }
|
inline void Writer::Visit(const Object& object) { Write(object); }
|
||||||
inline void Writer::Visit(const Number& number) { Write_i(number); }
|
inline void Writer::Visit(const Number& number) { Write(number); }
|
||||||
inline void Writer::Visit(const String& string) { Write_i(string); }
|
inline void Writer::Visit(const String& string) { Write(string); }
|
||||||
inline void Writer::Visit(const Boolean& boolean) { Write_i(boolean); }
|
inline void Writer::Visit(const Boolean& boolean) { Write(boolean); }
|
||||||
inline void Writer::Visit(const Null& null) { Write_i(null); }
|
inline void Writer::Visit(const Null& null) { Write(null); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // end namespace
|
} // end namespace
|
||||||
|
|
Loading…
Reference in a new issue