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