Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 1 | #ifndef JSON_WRITER_H_INCLUDED |
| 2 | # define JSON_WRITER_H_INCLUDED |
| 3 | |
| 4 | # include "value.h" |
| 5 | # include <vector> |
| 6 | # include <string> |
| 7 | # include <iostream> |
| 8 | |
| 9 | namespace Json { |
| 10 | |
| 11 | class Value; |
| 12 | |
| 13 | /** \brief Abstract class for writers. |
| 14 | */ |
| 15 | class JSON_API Writer |
| 16 | { |
| 17 | public: |
| 18 | virtual ~Writer(); |
| 19 | |
| 20 | virtual std::string write( const Value &root ) = 0; |
| 21 | }; |
| 22 | |
| 23 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly). |
| 24 | * |
| 25 | * The JSON document is written in a single line. It is not intended for 'human' consumption, |
| 26 | * but may be usefull to support feature such as RPC where bandwith is limited. |
| 27 | * \sa Reader, Value |
| 28 | */ |
| 29 | class JSON_API FastWriter : public Writer |
| 30 | { |
| 31 | public: |
| 32 | FastWriter(); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 33 | virtual ~FastWriter(){} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 34 | |
| 35 | void enableYAMLCompatibility(); |
| 36 | |
| 37 | public: // overridden from Writer |
| 38 | virtual std::string write( const Value &root ); |
| 39 | |
| 40 | private: |
| 41 | void writeValue( const Value &value ); |
| 42 | |
| 43 | std::string document_; |
| 44 | bool yamlCompatiblityEnabled_; |
| 45 | }; |
| 46 | |
| 47 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way. |
| 48 | * |
| 49 | * The rules for line break and indent are as follow: |
| 50 | * - Object value: |
| 51 | * - if empty then print {} without indent and line break |
| 52 | * - if not empty the print '{', line break & indent, print one value per line |
| 53 | * and then unindent and line break and print '}'. |
| 54 | * - Array value: |
| 55 | * - if empty then print [] without indent and line break |
| 56 | * - if the array contains no object value, empty array or some other value types, |
| 57 | * and all the values fit on one lines, then print the array on a single line. |
| 58 | * - otherwise, it the values do not fit on one line, or the array contains |
| 59 | * object or non empty array, then print one value per line. |
| 60 | * |
| 61 | * If the Value have comments then they are outputed according to their #CommentPlacement. |
| 62 | * |
| 63 | * \sa Reader, Value, Value::setComment() |
| 64 | */ |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 65 | class JSON_API StyledWriter: public Writer |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 66 | { |
| 67 | public: |
| 68 | StyledWriter(); |
| 69 | virtual ~StyledWriter(){} |
| 70 | |
| 71 | public: // overridden from Writer |
| 72 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 73 | * \param root Value to serialize. |
| 74 | * \return String containing the JSON document that represents the root value. |
| 75 | */ |
| 76 | virtual std::string write( const Value &root ); |
| 77 | |
| 78 | private: |
| 79 | void writeValue( const Value &value ); |
| 80 | void writeArrayValue( const Value &value ); |
| 81 | bool isMultineArray( const Value &value ); |
| 82 | void pushValue( const std::string &value ); |
| 83 | void writeIndent(); |
| 84 | void writeWithIndent( const std::string &value ); |
| 85 | void indent(); |
| 86 | void unindent(); |
| 87 | void writeCommentBeforeValue( const Value &root ); |
| 88 | void writeCommentAfterValueOnSameLine( const Value &root ); |
| 89 | bool hasCommentForValue( const Value &value ); |
| 90 | static std::string normalizeEOL( const std::string &text ); |
| 91 | |
| 92 | typedef std::vector<std::string> ChildValues; |
| 93 | |
| 94 | ChildValues childValues_; |
| 95 | std::string document_; |
| 96 | std::string indentString_; |
| 97 | int rightMargin_; |
| 98 | int indentSize_; |
| 99 | bool addChildValues_; |
| 100 | }; |
| 101 | |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 102 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way, |
| 103 | to a stream rather than to a string. |
| 104 | * |
| 105 | * The rules for line break and indent are as follow: |
| 106 | * - Object value: |
| 107 | * - if empty then print {} without indent and line break |
| 108 | * - if not empty the print '{', line break & indent, print one value per line |
| 109 | * and then unindent and line break and print '}'. |
| 110 | * - Array value: |
| 111 | * - if empty then print [] without indent and line break |
| 112 | * - if the array contains no object value, empty array or some other value types, |
| 113 | * and all the values fit on one lines, then print the array on a single line. |
| 114 | * - otherwise, it the values do not fit on one line, or the array contains |
| 115 | * object or non empty array, then print one value per line. |
| 116 | * |
| 117 | * If the Value have comments then they are outputed according to their #CommentPlacement. |
| 118 | * |
| 119 | * \param indentation Each level will be indented by this amount extra. |
| 120 | * \sa Reader, Value, Value::setComment() |
| 121 | */ |
| 122 | class JSON_API StyledStreamWriter |
| 123 | { |
| 124 | public: |
| 125 | StyledStreamWriter( std::string indentation="\t" ); |
| 126 | ~StyledStreamWriter(){} |
| 127 | |
| 128 | public: |
| 129 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 130 | * \param out Stream to write to. (Can be ostringstream, e.g.) |
| 131 | * \param root Value to serialize. |
| 132 | * \note There is no point in deriving from Writer, since write() should not return a value. |
| 133 | */ |
| 134 | void write( std::ostream &out, const Value &root ); |
| 135 | |
| 136 | private: |
| 137 | void writeValue( const Value &value ); |
| 138 | void writeArrayValue( const Value &value ); |
| 139 | bool isMultineArray( const Value &value ); |
| 140 | void pushValue( const std::string &value ); |
| 141 | void writeIndent(); |
| 142 | void writeWithIndent( const std::string &value ); |
| 143 | void indent(); |
| 144 | void unindent(); |
| 145 | void writeCommentBeforeValue( const Value &root ); |
| 146 | void writeCommentAfterValueOnSameLine( const Value &root ); |
| 147 | bool hasCommentForValue( const Value &value ); |
| 148 | static std::string normalizeEOL( const std::string &text ); |
| 149 | |
| 150 | typedef std::vector<std::string> ChildValues; |
| 151 | |
| 152 | ChildValues childValues_; |
| 153 | std::ostream* document_; |
| 154 | std::string indentString_; |
| 155 | int rightMargin_; |
| 156 | std::string indentation_; |
| 157 | bool addChildValues_; |
| 158 | }; |
| 159 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 160 | std::string JSON_API valueToString( Value::Int value ); |
| 161 | std::string JSON_API valueToString( Value::UInt value ); |
| 162 | std::string JSON_API valueToString( double value ); |
| 163 | std::string JSON_API valueToString( bool value ); |
| 164 | std::string JSON_API valueToQuotedString( const char *value ); |
| 165 | |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 166 | /// \brief Output using the StyledStreamWriter. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 167 | /// \see Json::operator>>() |
| 168 | std::ostream& operator<<( std::ostream&, const Value &root ); |
| 169 | |
| 170 | } // namespace Json |
| 171 | |
| 172 | |
| 173 | |
| 174 | #endif // JSON_WRITER_H_INCLUDED |