2010-05-21 03:13:36 +02:00
|
|
|
/**********************************************
|
|
|
|
|
|
|
|
License: BSD
|
|
|
|
Project Webpage: http://cajun-jsonapi.sourceforge.net/
|
|
|
|
Author: Terry Caton
|
|
|
|
|
|
|
|
***********************************************/
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
#include "libaegisub/cajun/writer.h"
|
|
|
|
|
|
|
|
#ifndef LAGI_PRE
|
2010-05-21 03:13:36 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2011-12-22 22:10:10 +01:00
|
|
|
#endif
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-10-18 00:00:49 +02:00
|
|
|
/*
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
TODO:
|
|
|
|
* better documentation
|
|
|
|
* unicode character encoding
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace json
|
|
|
|
{
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
Writer::Writer(std::ostream& ostr) :
|
2010-05-21 03:13:36 +02:00
|
|
|
m_ostr(ostr),
|
|
|
|
m_nTabDepth(0)
|
|
|
|
{}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Write(const Array& array)
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
2011-10-17 23:59:35 +02:00
|
|
|
if (array.empty())
|
2010-05-21 03:13:36 +02:00
|
|
|
m_ostr << "[]";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_ostr << '[' << std::endl;
|
|
|
|
++m_nTabDepth;
|
|
|
|
|
2011-10-18 00:00:49 +02:00
|
|
|
Array::const_iterator it(array.begin()), itend(array.end());
|
2011-10-17 23:59:35 +02:00
|
|
|
while (it != itend) {
|
2010-05-21 03:13:36 +02:00
|
|
|
m_ostr << std::string(m_nTabDepth, '\t');
|
2011-10-18 00:00:49 +02:00
|
|
|
|
|
|
|
Write(*it);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
if (++it != itend)
|
2010-05-21 03:13:36 +02:00
|
|
|
m_ostr << ',';
|
|
|
|
m_ostr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
--m_nTabDepth;
|
|
|
|
m_ostr << std::string(m_nTabDepth, '\t') << ']';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Write(const Object& object)
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
2011-10-17 23:59:35 +02:00
|
|
|
if (object.empty())
|
2010-05-21 03:13:36 +02:00
|
|
|
m_ostr << "{}";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_ostr << '{' << std::endl;
|
|
|
|
++m_nTabDepth;
|
|
|
|
|
2011-10-18 00:00:49 +02:00
|
|
|
Object::const_iterator it(object.begin()), itend(object.end());
|
2011-10-17 23:59:35 +02:00
|
|
|
while (it != itend) {
|
2011-10-17 23:59:47 +02:00
|
|
|
m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->first << "\" : ";
|
2011-10-18 00:00:49 +02:00
|
|
|
Write(it->second);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
if (++it != itend)
|
2010-05-21 03:13:36 +02:00
|
|
|
m_ostr << ',';
|
|
|
|
m_ostr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
--m_nTabDepth;
|
|
|
|
m_ostr << std::string(m_nTabDepth, '\t') << '}';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Write(const Number& numberElement)
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
2011-10-17 23:59:59 +02:00
|
|
|
m_ostr << std::setprecision(20) << numberElement;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Write(const Boolean& booleanElement)
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
2011-10-17 23:59:59 +02:00
|
|
|
m_ostr << (booleanElement ? "true" : "false");
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Write(const String& stringElement)
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
|
|
|
m_ostr << '"';
|
|
|
|
|
2011-10-17 23:59:59 +02:00
|
|
|
const std::string& s = stringElement;
|
|
|
|
std::string::const_iterator it(s.begin()), itend(s.end());
|
2011-10-17 23:59:35 +02:00
|
|
|
for (; it != itend; ++it)
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
|
|
|
switch (*it)
|
|
|
|
{
|
|
|
|
case '"': m_ostr << "\\\""; break;
|
|
|
|
case '\\': m_ostr << "\\\\"; break;
|
|
|
|
case '\b': m_ostr << "\\b"; break;
|
|
|
|
case '\f': m_ostr << "\\f"; break;
|
|
|
|
case '\n': m_ostr << "\\n"; break;
|
|
|
|
case '\r': m_ostr << "\\r"; break;
|
|
|
|
case '\t': m_ostr << "\\t"; break;
|
|
|
|
//case '\u': m_ostr << ""; break; ??
|
|
|
|
default: m_ostr << *it; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 00:00:49 +02:00
|
|
|
m_ostr << '"';
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Write(const Null& )
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
|
|
|
m_ostr << "null";
|
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Write(const UnknownElement& unknown)
|
2010-05-21 03:13:36 +02:00
|
|
|
{
|
2011-10-18 00:00:49 +02:00
|
|
|
unknown.Accept(*this);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:10 +01:00
|
|
|
void Writer::Visit(const Array& array) { Write(array); }
|
|
|
|
void Writer::Visit(const Object& object) { Write(object); }
|
|
|
|
void Writer::Visit(const Number& number) { Write(number); }
|
|
|
|
void Writer::Visit(const String& string) { Write(string); }
|
|
|
|
void Writer::Visit(const Boolean& boolean) { Write(boolean); }
|
|
|
|
void Writer::Visit(const Null& null) { Write(null); }
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
} // end namespace
|