00001 #ifndef JSON_WRITER_H_INCLUDED 00002 # define JSON_WRITER_H_INCLUDED 00003 00004 # include "value.h" 00005 # include <vector> 00006 # include <string> 00007 # include <iostream> 00008 00009 namespace Json { 00010 00011 class Value; 00012 00015 class JSON_API Writer 00016 { 00017 public: 00018 virtual ~Writer(); 00019 00020 virtual std::string write( const Value &root ) = 0; 00021 }; 00022 00029 class JSON_API FastWriter : public Writer 00030 { 00031 public: 00032 FastWriter(); 00033 virtual ~FastWriter(){} 00034 00035 void enableYAMLCompatibility(); 00036 00037 public: // overridden from Writer 00038 virtual std::string write( const Value &root ); 00039 00040 private: 00041 void writeValue( const Value &value ); 00042 00043 std::string document_; 00044 bool yamlCompatiblityEnabled_; 00045 }; 00046 00065 class JSON_API StyledWriter: public Writer 00066 { 00067 public: 00068 StyledWriter(); 00069 virtual ~StyledWriter(){} 00070 00071 public: // overridden from Writer 00076 virtual std::string write( const Value &root ); 00077 00078 private: 00079 void writeValue( const Value &value ); 00080 void writeArrayValue( const Value &value ); 00081 bool isMultineArray( const Value &value ); 00082 void pushValue( const std::string &value ); 00083 void writeIndent(); 00084 void writeWithIndent( const std::string &value ); 00085 void indent(); 00086 void unindent(); 00087 void writeCommentBeforeValue( const Value &root ); 00088 void writeCommentAfterValueOnSameLine( const Value &root ); 00089 bool hasCommentForValue( const Value &value ); 00090 static std::string normalizeEOL( const std::string &text ); 00091 00092 typedef std::vector<std::string> ChildValues; 00093 00094 ChildValues childValues_; 00095 std::string document_; 00096 std::string indentString_; 00097 int rightMargin_; 00098 int indentSize_; 00099 bool addChildValues_; 00100 }; 00101 00122 class JSON_API StyledStreamWriter 00123 { 00124 public: 00125 StyledStreamWriter( std::string indentation="\t" ); 00126 ~StyledStreamWriter(){} 00127 00128 public: 00134 void write( std::ostream &out, const Value &root ); 00135 00136 private: 00137 void writeValue( const Value &value ); 00138 void writeArrayValue( const Value &value ); 00139 bool isMultineArray( const Value &value ); 00140 void pushValue( const std::string &value ); 00141 void writeIndent(); 00142 void writeWithIndent( const std::string &value ); 00143 void indent(); 00144 void unindent(); 00145 void writeCommentBeforeValue( const Value &root ); 00146 void writeCommentAfterValueOnSameLine( const Value &root ); 00147 bool hasCommentForValue( const Value &value ); 00148 static std::string normalizeEOL( const std::string &text ); 00149 00150 typedef std::vector<std::string> ChildValues; 00151 00152 ChildValues childValues_; 00153 std::ostream* document_; 00154 std::string indentString_; 00155 int rightMargin_; 00156 std::string indentation_; 00157 bool addChildValues_; 00158 }; 00159 00160 std::string JSON_API valueToString( Int value ); 00161 std::string JSON_API valueToString( UInt value ); 00162 std::string JSON_API valueToString( double value ); 00163 std::string JSON_API valueToString( bool value ); 00164 std::string JSON_API valueToQuotedString( const char *value ); 00165 00168 std::ostream& operator<<( std::ostream&, const Value &root ); 00169 00170 } // namespace Json 00171 00172 00173 00174 #endif // JSON_WRITER_H_INCLUDED