Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [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 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 6 | #ifndef JSON_WRITER_H_INCLUDED |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 7 | #define JSON_WRITER_H_INCLUDED |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 8 | |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 9 | #if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 10 | #include "value.h" |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 11 | #endif // if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 12 | #include <vector> |
| 13 | #include <string> |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 14 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 15 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 16 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 17 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 18 | #pragma warning(push) |
| 19 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 20 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 21 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 22 | namespace Json { |
| 23 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 24 | class Value; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 25 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 26 | /** \brief Abstract class for writers. |
| 27 | */ |
| 28 | class JSON_API Writer { |
| 29 | public: |
| 30 | virtual ~Writer(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 31 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 32 | virtual std::string write(const Value& root) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 33 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 34 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 35 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format |
| 36 | *without formatting (not human friendly). |
| 37 | * |
| 38 | * The JSON document is written in a single line. It is not intended for 'human' |
| 39 | *consumption, |
| 40 | * but may be usefull to support feature such as RPC where bandwith is limited. |
| 41 | * \sa Reader, Value |
| 42 | */ |
| 43 | class JSON_API FastWriter : public Writer { |
| 44 | public: |
| 45 | FastWriter(); |
| 46 | virtual ~FastWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 47 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 48 | void enableYAMLCompatibility(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 49 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 50 | /** \brief Drop the "null" string from the writer's output for nullValues. |
| 51 | * Strictly speaking, this is not valid JSON. But when the output is being |
| 52 | * fed to a browser's Javascript, it makes for smaller output and the |
| 53 | * browser can handle the output just fine. |
| 54 | */ |
| 55 | void dropNullPlaceholders(); |
Aaron Jacobs | ae3c7a7 | 2012-03-12 04:53:57 +0000 | [diff] [blame] | 56 | |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 57 | void omitEndingLineFeed(); |
| 58 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 59 | public: // overridden from Writer |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 60 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 61 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 62 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 63 | void writeValue(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 64 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 65 | std::string document_; |
| 66 | bool yamlCompatiblityEnabled_; |
| 67 | bool dropNullPlaceholders_; |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 68 | bool omitEndingLineFeed_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 69 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 70 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 71 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 72 | *human friendly way. |
| 73 | * |
| 74 | * The rules for line break and indent are as follow: |
| 75 | * - Object value: |
| 76 | * - if empty then print {} without indent and line break |
| 77 | * - if not empty the print '{', line break & indent, print one value per |
| 78 | *line |
| 79 | * and then unindent and line break and print '}'. |
| 80 | * - Array value: |
| 81 | * - if empty then print [] without indent and line break |
| 82 | * - if the array contains no object value, empty array or some other value |
| 83 | *types, |
| 84 | * and all the values fit on one lines, then print the array on a single |
| 85 | *line. |
| 86 | * - otherwise, it the values do not fit on one line, or the array contains |
| 87 | * object or non empty array, then print one value per line. |
| 88 | * |
| 89 | * If the Value have comments then they are outputed according to their |
| 90 | *#CommentPlacement. |
| 91 | * |
| 92 | * \sa Reader, Value, Value::setComment() |
| 93 | */ |
| 94 | class JSON_API StyledWriter : public Writer { |
| 95 | public: |
| 96 | StyledWriter(); |
| 97 | virtual ~StyledWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 98 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 99 | public: // overridden from Writer |
| 100 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 101 | * \param root Value to serialize. |
| 102 | * \return String containing the JSON document that represents the root value. |
| 103 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 104 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 105 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 106 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 107 | void writeValue(const Value& value); |
| 108 | void writeArrayValue(const Value& value); |
| 109 | bool isMultineArray(const Value& value); |
| 110 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 111 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 112 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 113 | void indent(); |
| 114 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 115 | void writeCommentBeforeValue(const Value& root); |
| 116 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 117 | bool hasCommentForValue(const Value& value); |
| 118 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 119 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 120 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 121 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 122 | ChildValues childValues_; |
| 123 | std::string document_; |
| 124 | std::string indentString_; |
| 125 | int rightMargin_; |
| 126 | int indentSize_; |
| 127 | bool addChildValues_; |
| 128 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 129 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 130 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 131 | human friendly way, |
| 132 | to a stream rather than to a string. |
| 133 | * |
| 134 | * The rules for line break and indent are as follow: |
| 135 | * - Object value: |
| 136 | * - if empty then print {} without indent and line break |
| 137 | * - if not empty the print '{', line break & indent, print one value per |
| 138 | line |
| 139 | * and then unindent and line break and print '}'. |
| 140 | * - Array value: |
| 141 | * - if empty then print [] without indent and line break |
| 142 | * - if the array contains no object value, empty array or some other value |
| 143 | types, |
| 144 | * and all the values fit on one lines, then print the array on a single |
| 145 | line. |
| 146 | * - otherwise, it the values do not fit on one line, or the array contains |
| 147 | * object or non empty array, then print one value per line. |
| 148 | * |
| 149 | * If the Value have comments then they are outputed according to their |
| 150 | #CommentPlacement. |
| 151 | * |
| 152 | * \param indentation Each level will be indented by this amount extra. |
| 153 | * \sa Reader, Value, Value::setComment() |
| 154 | */ |
| 155 | class JSON_API StyledStreamWriter { |
| 156 | public: |
| 157 | StyledStreamWriter(std::string indentation = "\t"); |
| 158 | ~StyledStreamWriter() {} |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 159 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 160 | public: |
| 161 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 162 | * \param out Stream to write to. (Can be ostringstream, e.g.) |
| 163 | * \param root Value to serialize. |
| 164 | * \note There is no point in deriving from Writer, since write() should not |
| 165 | * return a value. |
| 166 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 167 | void write(std::ostream& out, const Value& root); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 168 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 169 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 170 | void writeValue(const Value& value); |
| 171 | void writeArrayValue(const Value& value); |
| 172 | bool isMultineArray(const Value& value); |
| 173 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 174 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 175 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 176 | void indent(); |
| 177 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 178 | void writeCommentBeforeValue(const Value& root); |
| 179 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 180 | bool hasCommentForValue(const Value& value); |
| 181 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 182 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 183 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 184 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 185 | ChildValues childValues_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 186 | std::ostream* document_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 187 | std::string indentString_; |
| 188 | int rightMargin_; |
| 189 | std::string indentation_; |
| 190 | bool addChildValues_; |
| 191 | }; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 192 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 193 | #if defined(JSON_HAS_INT64) |
| 194 | std::string JSON_API valueToString(Int value); |
| 195 | std::string JSON_API valueToString(UInt value); |
| 196 | #endif // if defined(JSON_HAS_INT64) |
| 197 | std::string JSON_API valueToString(LargestInt value); |
| 198 | std::string JSON_API valueToString(LargestUInt value); |
| 199 | std::string JSON_API valueToString(double value); |
| 200 | std::string JSON_API valueToString(bool value); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 201 | std::string JSON_API valueToQuotedString(const char* value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 202 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 203 | /// \brief Output using the StyledStreamWriter. |
| 204 | /// \see Json::operator>>() |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 205 | JSON_API std::ostream& operator<<(std::ostream&, const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 206 | |
| 207 | } // namespace Json |
| 208 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 209 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 210 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 211 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 212 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 213 | #endif // JSON_WRITER_H_INCLUDED |