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 | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 14 | #include <ostream> |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 15 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 16 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 17 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 18 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 19 | #pragma warning(push) |
| 20 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 21 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 22 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 23 | namespace Json { |
| 24 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 25 | class Value; |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | |
| 29 | Usage: |
Christopher Dunn | 9da9f84 | 2015-01-25 19:20:43 -0600 | [diff] [blame] | 30 | \code |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame] | 31 | using namespace Json; |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 32 | void writeToStdout(StreamWriter::Factory const& factory, Value const& value) { |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 33 | std::unique_ptr<StreamWriter> const writer( |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 34 | factory.newStreamWriter()); |
| 35 | writer->write(value, &std::cout); |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 36 | std::cout << std::endl; // add lf and flush |
| 37 | } |
Christopher Dunn | 9da9f84 | 2015-01-25 19:20:43 -0600 | [diff] [blame] | 38 | \endcode |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame] | 39 | */ |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 40 | class JSON_API StreamWriter { |
| 41 | protected: |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 42 | std::ostream* sout_; // not owned; will not delete |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 43 | public: |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 44 | StreamWriter(); |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 45 | virtual ~StreamWriter(); |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 46 | /** Write Value into document as configured in sub-class. |
| 47 | Do not take ownership of sout, but maintain a reference during function. |
| 48 | \pre sout != NULL |
| 49 | \return zero on success |
| 50 | \throw std::exception possibly, depending on configuration |
| 51 | */ |
| 52 | virtual int write(Value const& root, std::ostream* sout) = 0; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 53 | |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 54 | /** \brief A simple abstract factory. |
| 55 | */ |
| 56 | class JSON_API Factory { |
| 57 | public: |
| 58 | virtual ~Factory(); |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 59 | /** \brief Allocate a CharReader via operator new(). |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 60 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
| 61 | */ |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 62 | virtual StreamWriter* newStreamWriter() const = 0; |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 63 | }; // Factory |
| 64 | }; // StreamWriter |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 65 | |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 66 | /** \brief Write into stringstream, then return string, for convenience. |
| 67 | * A StreamWriter will be created from the factory, used, and then deleted. |
| 68 | */ |
| 69 | std::string writeString(StreamWriter::Factory const& factory, Value const& root); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 70 | |
| 71 | |
| 72 | /** \brief Build a StreamWriter implementation. |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 73 | |
| 74 | Usage: |
| 75 | \code |
| 76 | using namespace Json; |
| 77 | Value value = ...; |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 78 | StreamWriterBuilder builder; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 79 | builder.settings_["commentStyle"] = "None"; |
| 80 | builder.settings_["indentation"] = " "; // or whatever you like |
| 81 | std::unique_ptr<Json::StreamWriter> writer( |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 82 | builder.newStreamWriter()); |
| 83 | writer->write(value, &std::cout); |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 84 | std::cout << std::endl; // add lf and flush |
| 85 | \endcode |
| 86 | */ |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 87 | class JSON_API StreamWriterBuilder : public StreamWriter::Factory { |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 88 | public: |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 89 | // Note: We use a Json::Value so that we can add data-members to this class |
| 90 | // without a major version bump. |
| 91 | /** Configuration of this builder. |
| 92 | Available settings (case-sensitive): |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 93 | - "commentStyle": "None", "Some", or "All" |
| 94 | - "indentation": "<anything>" |
| 95 | |
| 96 | You can examine 'settings_` yourself |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 97 | to see the defaults. You can also write and read them just like any |
| 98 | JSON Value. |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 99 | \sa setDefaults() |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 100 | */ |
| 101 | Json::Value settings_; |
Christopher Dunn | 7eca3b4 | 2015-01-26 11:17:42 -0600 | [diff] [blame] | 102 | |
| 103 | StreamWriterBuilder(); |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 104 | virtual ~StreamWriterBuilder(); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 105 | |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 106 | /** |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 107 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
| 108 | */ |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 109 | virtual StreamWriter* newStreamWriter() const; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 110 | |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 111 | /** \return true if 'settings' are legal and consistent; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 112 | * otherwise, indicate bad settings via 'invalid'. |
| 113 | */ |
| 114 | bool validate(Json::Value* invalid) const; |
| 115 | /** Called by ctor, but you can use this to reset settings_. |
| 116 | * \pre 'settings' != NULL (but Json::null is fine) |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 117 | * \remark Defaults: |
| 118 | * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 119 | */ |
| 120 | static void setDefaults(Json::Value* settings); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 121 | }; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 122 | |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 123 | /** \brief Build a StreamWriter implementation. |
| 124 | * Comments are not written, and most whitespace is omitted. |
| 125 | * In addition, there are some special settings to allow compatibility |
| 126 | * with the old FastWriter. |
| 127 | * Usage: |
| 128 | * \code |
| 129 | * OldCompressingStreamWriterBuilder b; |
| 130 | * b.dropNullPlaceHolders_ = true; // etc. |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 131 | * StreamWriter* w = b.newStreamWriter(); |
| 132 | * w->write(value, &std::cout); |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 133 | * delete w; |
| 134 | * \endcode |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 135 | * |
| 136 | * \deprecated Use StreamWriterBuilder |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 137 | */ |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 138 | class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 139 | { |
| 140 | public: |
| 141 | // Note: We cannot add data-members to this class without a major version bump. |
| 142 | // So these might as well be completely exposed. |
| 143 | |
| 144 | /** \brief Drop the "null" string from the writer's output for nullValues. |
| 145 | * Strictly speaking, this is not valid JSON. But when the output is being |
| 146 | * fed to a browser's Javascript, it makes for smaller output and the |
| 147 | * browser can handle the output just fine. |
| 148 | */ |
| 149 | bool dropNullPlaceholders_; |
| 150 | /** \brief Do not add \n at end of document. |
| 151 | * Normally, we add an extra newline, just because. |
| 152 | */ |
| 153 | bool omitEndingLineFeed_; |
| 154 | /** \brief Add a space after ':'. |
| 155 | * If indentation is non-empty, we surround colon with whitespace, |
| 156 | * e.g. " : " |
| 157 | * This will add back the trailing space when there is no indentation. |
| 158 | * This seems dubious when the entire document is on a single line, |
| 159 | * but we leave this here to repduce the behavior of the old `FastWriter`. |
| 160 | */ |
| 161 | bool enableYAMLCompatibility_; |
| 162 | |
| 163 | OldCompressingStreamWriterBuilder() |
| 164 | : dropNullPlaceholders_(false) |
| 165 | , omitEndingLineFeed_(false) |
| 166 | , enableYAMLCompatibility_(false) |
| 167 | {} |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 168 | virtual StreamWriter* newStreamWriter() const; |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 169 | }; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 170 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 171 | /** \brief Abstract class for writers. |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 172 | * \deprecated Use StreamWriter. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 173 | */ |
| 174 | class JSON_API Writer { |
| 175 | public: |
| 176 | virtual ~Writer(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 177 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 178 | virtual std::string write(const Value& root) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 179 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 180 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 181 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format |
| 182 | *without formatting (not human friendly). |
| 183 | * |
| 184 | * The JSON document is written in a single line. It is not intended for 'human' |
| 185 | *consumption, |
| 186 | * but may be usefull to support feature such as RPC where bandwith is limited. |
| 187 | * \sa Reader, Value |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 188 | * \deprecated Use OldCompressingStreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 189 | */ |
| 190 | class JSON_API FastWriter : public Writer { |
| 191 | public: |
| 192 | FastWriter(); |
| 193 | virtual ~FastWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 194 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 195 | void enableYAMLCompatibility(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 196 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 197 | /** \brief Drop the "null" string from the writer's output for nullValues. |
| 198 | * Strictly speaking, this is not valid JSON. But when the output is being |
| 199 | * fed to a browser's Javascript, it makes for smaller output and the |
| 200 | * browser can handle the output just fine. |
| 201 | */ |
| 202 | void dropNullPlaceholders(); |
Aaron Jacobs | ae3c7a7 | 2012-03-12 04:53:57 +0000 | [diff] [blame] | 203 | |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 204 | void omitEndingLineFeed(); |
| 205 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 206 | public: // overridden from Writer |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 207 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 208 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 209 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 210 | void writeValue(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 211 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 212 | std::string document_; |
| 213 | bool yamlCompatiblityEnabled_; |
| 214 | bool dropNullPlaceholders_; |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 215 | bool omitEndingLineFeed_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 216 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 217 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 218 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 219 | *human friendly way. |
| 220 | * |
| 221 | * The rules for line break and indent are as follow: |
| 222 | * - Object value: |
| 223 | * - if empty then print {} without indent and line break |
| 224 | * - if not empty the print '{', line break & indent, print one value per |
| 225 | *line |
| 226 | * and then unindent and line break and print '}'. |
| 227 | * - Array value: |
| 228 | * - if empty then print [] without indent and line break |
| 229 | * - if the array contains no object value, empty array or some other value |
| 230 | *types, |
| 231 | * and all the values fit on one lines, then print the array on a single |
| 232 | *line. |
| 233 | * - otherwise, it the values do not fit on one line, or the array contains |
| 234 | * object or non empty array, then print one value per line. |
| 235 | * |
| 236 | * If the Value have comments then they are outputed according to their |
| 237 | *#CommentPlacement. |
| 238 | * |
| 239 | * \sa Reader, Value, Value::setComment() |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 240 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 241 | */ |
| 242 | class JSON_API StyledWriter : public Writer { |
| 243 | public: |
| 244 | StyledWriter(); |
| 245 | virtual ~StyledWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 246 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 247 | public: // overridden from Writer |
| 248 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 249 | * \param root Value to serialize. |
| 250 | * \return String containing the JSON document that represents the root value. |
| 251 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 252 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 253 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 254 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 255 | void writeValue(const Value& value); |
| 256 | void writeArrayValue(const Value& value); |
| 257 | bool isMultineArray(const Value& value); |
| 258 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 259 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 260 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 261 | void indent(); |
| 262 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 263 | void writeCommentBeforeValue(const Value& root); |
| 264 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 265 | bool hasCommentForValue(const Value& value); |
| 266 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 267 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 268 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 269 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 270 | ChildValues childValues_; |
| 271 | std::string document_; |
| 272 | std::string indentString_; |
| 273 | int rightMargin_; |
| 274 | int indentSize_; |
| 275 | bool addChildValues_; |
| 276 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 277 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 278 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 279 | human friendly way, |
| 280 | to a stream rather than to a string. |
| 281 | * |
| 282 | * The rules for line break and indent are as follow: |
| 283 | * - Object value: |
| 284 | * - if empty then print {} without indent and line break |
| 285 | * - if not empty the print '{', line break & indent, print one value per |
| 286 | line |
| 287 | * and then unindent and line break and print '}'. |
| 288 | * - Array value: |
| 289 | * - if empty then print [] without indent and line break |
| 290 | * - if the array contains no object value, empty array or some other value |
| 291 | types, |
| 292 | * and all the values fit on one lines, then print the array on a single |
| 293 | line. |
| 294 | * - otherwise, it the values do not fit on one line, or the array contains |
| 295 | * object or non empty array, then print one value per line. |
| 296 | * |
| 297 | * If the Value have comments then they are outputed according to their |
| 298 | #CommentPlacement. |
| 299 | * |
| 300 | * \param indentation Each level will be indented by this amount extra. |
| 301 | * \sa Reader, Value, Value::setComment() |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 302 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 303 | */ |
| 304 | class JSON_API StyledStreamWriter { |
| 305 | public: |
| 306 | StyledStreamWriter(std::string indentation = "\t"); |
| 307 | ~StyledStreamWriter() {} |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 308 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 309 | public: |
| 310 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 311 | * \param out Stream to write to. (Can be ostringstream, e.g.) |
| 312 | * \param root Value to serialize. |
| 313 | * \note There is no point in deriving from Writer, since write() should not |
| 314 | * return a value. |
| 315 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 316 | void write(std::ostream& out, const Value& root); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 317 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 318 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 319 | void writeValue(const Value& value); |
| 320 | void writeArrayValue(const Value& value); |
| 321 | bool isMultineArray(const Value& value); |
| 322 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 323 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 324 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 325 | void indent(); |
| 326 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 327 | void writeCommentBeforeValue(const Value& root); |
| 328 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 329 | bool hasCommentForValue(const Value& value); |
| 330 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 331 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 332 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 333 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 334 | ChildValues childValues_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 335 | std::ostream* document_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 336 | std::string indentString_; |
| 337 | int rightMargin_; |
| 338 | std::string indentation_; |
Christopher Dunn | d383056 | 2015-01-23 13:09:43 -0600 | [diff] [blame] | 339 | bool addChildValues_ : 1; |
| 340 | bool indented_ : 1; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 341 | }; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 342 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 343 | #if defined(JSON_HAS_INT64) |
| 344 | std::string JSON_API valueToString(Int value); |
| 345 | std::string JSON_API valueToString(UInt value); |
| 346 | #endif // if defined(JSON_HAS_INT64) |
| 347 | std::string JSON_API valueToString(LargestInt value); |
| 348 | std::string JSON_API valueToString(LargestUInt value); |
| 349 | std::string JSON_API valueToString(double value); |
| 350 | std::string JSON_API valueToString(bool value); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 351 | std::string JSON_API valueToQuotedString(const char* value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 352 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 353 | /// \brief Output using the StyledStreamWriter. |
| 354 | /// \see Json::operator>>() |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 355 | JSON_API std::ostream& operator<<(std::ostream&, const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 356 | |
| 357 | } // namespace Json |
| 358 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 359 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 360 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 361 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 362 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 363 | #endif // JSON_WRITER_H_INCLUDED |