00001 // Copyright 2007-2010 Baptiste Lepilleur 00002 // Distributed under MIT license, or public domain if desired and 00003 // recognized in your jurisdiction. 00004 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE 00005 00006 #ifndef JSON_WRITER_H_INCLUDED 00007 # define JSON_WRITER_H_INCLUDED 00008 00009 #if !defined(JSON_IS_AMALGAMATION) 00010 # include "value.h" 00011 #endif // if !defined(JSON_IS_AMALGAMATION) 00012 # include <vector> 00013 # include <string> 00014 # include <iostream> 00015 00016 namespace Json { 00017 00018 class Value; 00019 00022 class JSON_API Writer 00023 { 00024 public: 00025 virtual ~Writer(); 00026 00027 virtual std::string write( const Value &root ) = 0; 00028 }; 00029 00036 class JSON_API FastWriter : public Writer 00037 { 00038 public: 00039 FastWriter(); 00040 virtual ~FastWriter(){} 00041 00042 void enableYAMLCompatibility(); 00043 00044 public: // overridden from Writer 00045 virtual std::string write( const Value &root ); 00046 00047 private: 00048 void writeValue( const Value &value ); 00049 00050 std::string document_; 00051 bool yamlCompatiblityEnabled_; 00052 }; 00053 00072 class JSON_API StyledWriter: public Writer 00073 { 00074 public: 00075 StyledWriter(); 00076 virtual ~StyledWriter(){} 00077 00078 public: // overridden from Writer 00083 virtual std::string write( const Value &root ); 00084 00085 private: 00086 void writeValue( const Value &value ); 00087 void writeArrayValue( const Value &value ); 00088 bool isMultineArray( const Value &value ); 00089 void pushValue( const std::string &value ); 00090 void writeIndent(); 00091 void writeWithIndent( const std::string &value ); 00092 void indent(); 00093 void unindent(); 00094 void writeCommentBeforeValue( const Value &root ); 00095 void writeCommentAfterValueOnSameLine( const Value &root ); 00096 bool hasCommentForValue( const Value &value ); 00097 static std::string normalizeEOL( const std::string &text ); 00098 00099 typedef std::vector<std::string> ChildValues; 00100 00101 ChildValues childValues_; 00102 std::string document_; 00103 std::string indentString_; 00104 int rightMargin_; 00105 int indentSize_; 00106 bool addChildValues_; 00107 }; 00108 00129 class JSON_API StyledStreamWriter 00130 { 00131 public: 00132 StyledStreamWriter( std::string indentation="\t" ); 00133 ~StyledStreamWriter(){} 00134 00135 public: 00141 void write( std::ostream &out, const Value &root ); 00142 00143 private: 00144 void writeValue( const Value &value ); 00145 void writeArrayValue( const Value &value ); 00146 bool isMultineArray( const Value &value ); 00147 void pushValue( const std::string &value ); 00148 void writeIndent(); 00149 void writeWithIndent( const std::string &value ); 00150 void indent(); 00151 void unindent(); 00152 void writeCommentBeforeValue( const Value &root ); 00153 void writeCommentAfterValueOnSameLine( const Value &root ); 00154 bool hasCommentForValue( const Value &value ); 00155 static std::string normalizeEOL( const std::string &text ); 00156 00157 typedef std::vector<std::string> ChildValues; 00158 00159 ChildValues childValues_; 00160 std::ostream* document_; 00161 std::string indentString_; 00162 int rightMargin_; 00163 std::string indentation_; 00164 bool addChildValues_; 00165 }; 00166 00167 # if defined(JSON_HAS_INT64) 00168 std::string JSON_API valueToString( Int value ); 00169 std::string JSON_API valueToString( UInt value ); 00170 # endif // if defined(JSON_HAS_INT64) 00171 std::string JSON_API valueToString( LargestInt value ); 00172 std::string JSON_API valueToString( LargestUInt value ); 00173 std::string JSON_API valueToString( double value ); 00174 std::string JSON_API valueToString( bool value ); 00175 std::string JSON_API valueToQuotedString( const char *value ); 00176 00179 std::ostream& operator<<( std::ostream&, const Value &root ); 00180 00181 } // namespace Json 00182 00183 00184 00185 #endif // JSON_WRITER_H_INCLUDED