rt300@13: #ifndef JSON_WRITER_H_INCLUDED rt300@13: # define JSON_WRITER_H_INCLUDED rt300@13: rt300@13: # include "value.h" rt300@13: # include rt300@13: # include rt300@13: # include rt300@13: rt300@13: namespace Json { rt300@13: rt300@13: class Value; rt300@13: rt300@13: /** \brief Abstract class for writers. rt300@13: */ rt300@13: class JSON_API Writer rt300@13: { rt300@13: public: rt300@13: virtual ~Writer(); rt300@13: rt300@13: virtual std::string write( const Value &root ) = 0; rt300@13: }; rt300@13: rt300@13: /** \brief Outputs a Value in JSON format without formatting (not human friendly). rt300@13: * rt300@13: * The JSON document is written in a single line. It is not intended for 'human' consumption, rt300@13: * but may be usefull to support feature such as RPC where bandwith is limited. rt300@13: * \sa Reader, Value rt300@13: */ rt300@13: class JSON_API FastWriter : public Writer rt300@13: { rt300@13: public: rt300@13: FastWriter(); rt300@13: virtual ~FastWriter(){} rt300@13: rt300@13: void enableYAMLCompatibility(); rt300@13: rt300@13: public: // overridden from Writer rt300@13: virtual std::string write( const Value &root ); rt300@13: rt300@13: private: rt300@13: void writeValue( const Value &value ); rt300@13: rt300@13: std::string document_; rt300@13: bool yamlCompatiblityEnabled_; rt300@13: }; rt300@13: rt300@13: /** \brief Writes a Value in JSON format in a human friendly way. rt300@13: * rt300@13: * The rules for line break and indent are as follow: rt300@13: * - Object value: rt300@13: * - if empty then print {} without indent and line break rt300@13: * - if not empty the print '{', line break & indent, print one value per line rt300@13: * and then unindent and line break and print '}'. rt300@13: * - Array value: rt300@13: * - if empty then print [] without indent and line break rt300@13: * - if the array contains no object value, empty array or some other value types, rt300@13: * and all the values fit on one lines, then print the array on a single line. rt300@13: * - otherwise, it the values do not fit on one line, or the array contains rt300@13: * object or non empty array, then print one value per line. rt300@13: * rt300@13: * If the Value have comments then they are outputed according to their #CommentPlacement. rt300@13: * rt300@13: * \sa Reader, Value, Value::setComment() rt300@13: */ rt300@13: class JSON_API StyledWriter: public Writer rt300@13: { rt300@13: public: rt300@13: StyledWriter(); rt300@13: virtual ~StyledWriter(){} rt300@13: rt300@13: public: // overridden from Writer rt300@13: /** \brief Serialize a Value in JSON format. rt300@13: * \param root Value to serialize. rt300@13: * \return String containing the JSON document that represents the root value. rt300@13: */ rt300@13: virtual std::string write( const Value &root ); rt300@13: rt300@13: private: rt300@13: void writeValue( const Value &value ); rt300@13: void writeArrayValue( const Value &value ); rt300@13: bool isMultineArray( const Value &value ); rt300@13: void pushValue( const std::string &value ); rt300@13: void writeIndent(); rt300@13: void writeWithIndent( const std::string &value ); rt300@13: void indent(); rt300@13: void unindent(); rt300@13: void writeCommentBeforeValue( const Value &root ); rt300@13: void writeCommentAfterValueOnSameLine( const Value &root ); rt300@13: bool hasCommentForValue( const Value &value ); rt300@13: static std::string normalizeEOL( const std::string &text ); rt300@13: rt300@13: typedef std::vector ChildValues; rt300@13: rt300@13: ChildValues childValues_; rt300@13: std::string document_; rt300@13: std::string indentString_; rt300@13: int rightMargin_; rt300@13: int indentSize_; rt300@13: bool addChildValues_; rt300@13: }; rt300@13: rt300@13: /** \brief Writes a Value in JSON format in a human friendly way, rt300@13: to a stream rather than to a string. rt300@13: * rt300@13: * The rules for line break and indent are as follow: rt300@13: * - Object value: rt300@13: * - if empty then print {} without indent and line break rt300@13: * - if not empty the print '{', line break & indent, print one value per line rt300@13: * and then unindent and line break and print '}'. rt300@13: * - Array value: rt300@13: * - if empty then print [] without indent and line break rt300@13: * - if the array contains no object value, empty array or some other value types, rt300@13: * and all the values fit on one lines, then print the array on a single line. rt300@13: * - otherwise, it the values do not fit on one line, or the array contains rt300@13: * object or non empty array, then print one value per line. rt300@13: * rt300@13: * If the Value have comments then they are outputed according to their #CommentPlacement. rt300@13: * rt300@13: * \param indentation Each level will be indented by this amount extra. rt300@13: * \sa Reader, Value, Value::setComment() rt300@13: */ rt300@13: class JSON_API StyledStreamWriter rt300@13: { rt300@13: public: rt300@13: StyledStreamWriter( std::string indentation="\t" ); rt300@13: ~StyledStreamWriter(){} rt300@13: rt300@13: public: rt300@13: /** \brief Serialize a Value in JSON format. rt300@13: * \param out Stream to write to. (Can be ostringstream, e.g.) rt300@13: * \param root Value to serialize. rt300@13: * \note There is no point in deriving from Writer, since write() should not return a value. rt300@13: */ rt300@13: void write( std::ostream &out, const Value &root ); rt300@13: rt300@13: private: rt300@13: void writeValue( const Value &value ); rt300@13: void writeArrayValue( const Value &value ); rt300@13: bool isMultineArray( const Value &value ); rt300@13: void pushValue( const std::string &value ); rt300@13: void writeIndent(); rt300@13: void writeWithIndent( const std::string &value ); rt300@13: void indent(); rt300@13: void unindent(); rt300@13: void writeCommentBeforeValue( const Value &root ); rt300@13: void writeCommentAfterValueOnSameLine( const Value &root ); rt300@13: bool hasCommentForValue( const Value &value ); rt300@13: static std::string normalizeEOL( const std::string &text ); rt300@13: rt300@13: typedef std::vector ChildValues; rt300@13: rt300@13: ChildValues childValues_; rt300@13: std::ostream* document_; rt300@13: std::string indentString_; rt300@13: int rightMargin_; rt300@13: std::string indentation_; rt300@13: bool addChildValues_; rt300@13: }; rt300@13: rt300@13: std::string JSON_API valueToString( Int value ); rt300@13: std::string JSON_API valueToString( UInt value ); rt300@13: std::string JSON_API valueToString( double value ); rt300@13: std::string JSON_API valueToString( bool value ); rt300@13: std::string JSON_API valueToQuotedString( const char *value ); rt300@13: rt300@13: /// \brief Output using the StyledStreamWriter. rt300@13: /// \see Json::operator>>() rt300@13: std::ostream& operator<<( std::ostream&, const Value &root ); rt300@13: rt300@13: } // namespace Json rt300@13: rt300@13: rt300@13: rt300@13: #endif // JSON_WRITER_H_INCLUDED