User Manual, Developers Guide and API Documentation

writerInl.hpp

Go to the documentation of this file.
00001 /**********************************************
00002 
00003 License: BSD
00004 Project Webpage: http://cajun-jsonapi.sourceforge.net/
00005 Author: Terry Caton
00006 
00007 ***********************************************/
00008 
00009 #include <iostream>
00010 #include <iomanip>
00011 
00012 /*  
00013 
00014 TODO:
00015 * better documentation
00016 * unicode character encoding
00017 
00018 */
00019 
00020 namespace wns{ namespace probe  { namespace bus { namespace json {
00021 
00022 
00023 inline void Writer::Write(const UnknownElement& elementRoot, std::ostream& ostr) { Write_i(elementRoot, ostr); }
00024 inline void Writer::Write(const Object& object, std::ostream& ostr)              { Write_i(object, ostr); }
00025 inline void Writer::Write(const Array& array, std::ostream& ostr)                { Write_i(array, ostr); }
00026 inline void Writer::Write(const Number& number, std::ostream& ostr)              { Write_i(number, ostr); }
00027 inline void Writer::Write(const String& string, std::ostream& ostr)              { Write_i(string, ostr); }
00028 inline void Writer::Write(const Boolean& boolean, std::ostream& ostr)            { Write_i(boolean, ostr); }
00029 inline void Writer::Write(const Null& null, std::ostream& ostr)                  { Write_i(null, ostr); }
00030 
00031 
00032 inline Writer::Writer(std::ostream& ostr) :
00033    m_ostr(ostr),
00034    m_nTabDepth(0)
00035 {}
00036 
00037 template <typename ElementTypeT>
00038 void Writer::Write_i(const ElementTypeT& element, std::ostream& ostr)
00039 {
00040    Writer writer(ostr);
00041    writer.Write_i(element);
00042    ostr.flush(); // all done
00043 }
00044 
00045 inline void Writer::Write_i(const Array& array)
00046 {
00047    if (array.Empty())
00048       m_ostr << "[]";
00049    else
00050    {
00051       m_ostr << '[' << std::endl;
00052       ++m_nTabDepth;
00053 
00054       Array::const_iterator it(array.Begin()),
00055                             itEnd(array.End());
00056       while (it != itEnd) {
00057          m_ostr << std::string(m_nTabDepth, '\t');
00058          
00059          Write_i(*it);
00060 
00061          if (++it != itEnd)
00062             m_ostr << ',';
00063          m_ostr << std::endl;
00064       }
00065 
00066       --m_nTabDepth;
00067       m_ostr << std::string(m_nTabDepth, '\t') << ']';
00068    }
00069 }
00070 
00071 inline void Writer::Write_i(const Object& object)
00072 {
00073    if (object.Empty())
00074       m_ostr << "{}";
00075    else
00076    {
00077       m_ostr << '{' << std::endl;
00078       ++m_nTabDepth;
00079 
00080       Object::const_iterator it(object.Begin()),
00081                              itEnd(object.End());
00082       while (it != itEnd) {
00083          m_ostr << std::string(m_nTabDepth, '\t') << '"' << it->name << "\" : ";
00084          Write_i(it->element); 
00085 
00086          if (++it != itEnd)
00087             m_ostr << ',';
00088          m_ostr << std::endl;
00089       }
00090 
00091       --m_nTabDepth;
00092       m_ostr << std::string(m_nTabDepth, '\t') << '}';
00093    }
00094 }
00095 
00096 inline void Writer::Write_i(const Number& numberElement)
00097 {
00098    m_ostr << std::setprecision(20) << numberElement.Value();
00099 }
00100 
00101 inline void Writer::Write_i(const Boolean& booleanElement)
00102 {
00103    m_ostr << (booleanElement.Value() ? "true" : "false");
00104 }
00105 
00106 inline void Writer::Write_i(const String& stringElement)
00107 {
00108    m_ostr << '"';
00109 
00110    const std::string& s = stringElement.Value();
00111    std::string::const_iterator it(s.begin()),
00112                                itEnd(s.end());
00113    for (; it != itEnd; ++it)
00114    {
00115       switch (*it)
00116       {
00117          case '"':         m_ostr << "\\\"";   break;
00118          case '\\':        m_ostr << "\\\\";   break;
00119          case '\b':        m_ostr << "\\b";    break;
00120          case '\f':        m_ostr << "\\f";    break;
00121          case '\n':        m_ostr << "\\n";    break;
00122          case '\r':        m_ostr << "\\r";    break;
00123          case '\t':        m_ostr << "\\t";    break;
00124          //case '\u':        m_ostr << "";    break;  ??
00125          default:          m_ostr << *it;       break;
00126       }
00127    }
00128 
00129    m_ostr << '"';   
00130 }
00131 
00132 inline void Writer::Write_i(const Null& )
00133 {
00134    m_ostr << "null";
00135 }
00136 
00137 inline void Writer::Write_i(const UnknownElement& unknown)
00138 {
00139    unknown.Accept(*this); 
00140 }
00141 
00142 inline void Writer::Visit(const Array& array)       { Write_i(array); }
00143 inline void Writer::Visit(const Object& object)     { Write_i(object); }
00144 inline void Writer::Visit(const Number& number)     { Write_i(number); }
00145 inline void Writer::Visit(const String& string)     { Write_i(string); }
00146 inline void Writer::Visit(const Boolean& boolean)   { Write_i(boolean); }
00147 inline void Writer::Visit(const Null& null)         { Write_i(null); }
00148 
00149 
00150 } // json
00151 } // bus
00152 } // probe
00153 } // wns

Generated on Sun May 27 03:31:50 2012 for openWNS by  doxygen 1.5.5