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 | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 93 | - "commentStyle": "None" or "All" |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 94 | - "indentation": "<anything>" |
Christopher Dunn | 29501c4 | 2015-02-10 23:03:27 -0600 | [diff] [blame^] | 95 | - "enableYAMLCompatibility": false or true |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 96 | - slightly change the whitespace around colons |
Christopher Dunn | 29501c4 | 2015-02-10 23:03:27 -0600 | [diff] [blame^] | 97 | - "dropNullPlaceholders": false or true |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 98 | - Drop the "null" string from the writer's output for nullValues. |
| 99 | Strictly speaking, this is not valid JSON. But when the output is being |
| 100 | fed to a browser's Javascript, it makes for smaller output and the |
| 101 | browser can handle the output just fine. |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 102 | |
| 103 | You can examine 'settings_` yourself |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 104 | to see the defaults. You can also write and read them just like any |
| 105 | JSON Value. |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 106 | \sa setDefaults() |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 107 | */ |
| 108 | Json::Value settings_; |
Christopher Dunn | 7eca3b4 | 2015-01-26 11:17:42 -0600 | [diff] [blame] | 109 | |
| 110 | StreamWriterBuilder(); |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 111 | virtual ~StreamWriterBuilder(); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 112 | |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 113 | /** |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 114 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
| 115 | */ |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 116 | virtual StreamWriter* newStreamWriter() const; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 117 | |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 118 | /** \return true if 'settings' are legal and consistent; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 119 | * otherwise, indicate bad settings via 'invalid'. |
| 120 | */ |
| 121 | bool validate(Json::Value* invalid) const; |
| 122 | /** Called by ctor, but you can use this to reset settings_. |
| 123 | * \pre 'settings' != NULL (but Json::null is fine) |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 124 | * \remark Defaults: |
| 125 | * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 126 | */ |
| 127 | static void setDefaults(Json::Value* settings); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 128 | }; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 129 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 130 | /** \brief Abstract class for writers. |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 131 | * \deprecated Use StreamWriter. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 132 | */ |
| 133 | class JSON_API Writer { |
| 134 | public: |
| 135 | virtual ~Writer(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 136 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 137 | virtual std::string write(const Value& root) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 138 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 139 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 140 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format |
| 141 | *without formatting (not human friendly). |
| 142 | * |
| 143 | * The JSON document is written in a single line. It is not intended for 'human' |
| 144 | *consumption, |
| 145 | * but may be usefull to support feature such as RPC where bandwith is limited. |
| 146 | * \sa Reader, Value |
Christopher Dunn | 20d0967 | 2015-02-10 21:29:35 -0600 | [diff] [blame] | 147 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 148 | */ |
| 149 | class JSON_API FastWriter : public Writer { |
| 150 | public: |
| 151 | FastWriter(); |
| 152 | virtual ~FastWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 153 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 154 | void enableYAMLCompatibility(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 155 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 156 | /** \brief Drop the "null" string from the writer's output for nullValues. |
| 157 | * Strictly speaking, this is not valid JSON. But when the output is being |
| 158 | * fed to a browser's Javascript, it makes for smaller output and the |
| 159 | * browser can handle the output just fine. |
| 160 | */ |
| 161 | void dropNullPlaceholders(); |
Aaron Jacobs | ae3c7a7 | 2012-03-12 04:53:57 +0000 | [diff] [blame] | 162 | |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 163 | void omitEndingLineFeed(); |
| 164 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 165 | public: // overridden from Writer |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 166 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 167 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 168 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 169 | void writeValue(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 170 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 171 | std::string document_; |
| 172 | bool yamlCompatiblityEnabled_; |
| 173 | bool dropNullPlaceholders_; |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 174 | bool omitEndingLineFeed_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 175 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 176 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 177 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 178 | *human friendly way. |
| 179 | * |
| 180 | * The rules for line break and indent are as follow: |
| 181 | * - Object value: |
| 182 | * - if empty then print {} without indent and line break |
| 183 | * - if not empty the print '{', line break & indent, print one value per |
| 184 | *line |
| 185 | * and then unindent and line break and print '}'. |
| 186 | * - Array value: |
| 187 | * - if empty then print [] without indent and line break |
| 188 | * - if the array contains no object value, empty array or some other value |
| 189 | *types, |
| 190 | * and all the values fit on one lines, then print the array on a single |
| 191 | *line. |
| 192 | * - otherwise, it the values do not fit on one line, or the array contains |
| 193 | * object or non empty array, then print one value per line. |
| 194 | * |
| 195 | * If the Value have comments then they are outputed according to their |
| 196 | *#CommentPlacement. |
| 197 | * |
| 198 | * \sa Reader, Value, Value::setComment() |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 199 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 200 | */ |
| 201 | class JSON_API StyledWriter : public Writer { |
| 202 | public: |
| 203 | StyledWriter(); |
| 204 | virtual ~StyledWriter() {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 205 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 206 | public: // overridden from Writer |
| 207 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 208 | * \param root Value to serialize. |
| 209 | * \return String containing the JSON document that represents the root value. |
| 210 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 211 | virtual std::string write(const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 212 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 213 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 214 | void writeValue(const Value& value); |
| 215 | void writeArrayValue(const Value& value); |
| 216 | bool isMultineArray(const Value& value); |
| 217 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 218 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 219 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 220 | void indent(); |
| 221 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 222 | void writeCommentBeforeValue(const Value& root); |
| 223 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 224 | bool hasCommentForValue(const Value& value); |
| 225 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 226 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 227 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 228 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 229 | ChildValues childValues_; |
| 230 | std::string document_; |
| 231 | std::string indentString_; |
| 232 | int rightMargin_; |
| 233 | int indentSize_; |
| 234 | bool addChildValues_; |
| 235 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 236 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 237 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 238 | human friendly way, |
| 239 | to a stream rather than to a string. |
| 240 | * |
| 241 | * The rules for line break and indent are as follow: |
| 242 | * - Object value: |
| 243 | * - if empty then print {} without indent and line break |
| 244 | * - if not empty the print '{', line break & indent, print one value per |
| 245 | line |
| 246 | * and then unindent and line break and print '}'. |
| 247 | * - Array value: |
| 248 | * - if empty then print [] without indent and line break |
| 249 | * - if the array contains no object value, empty array or some other value |
| 250 | types, |
| 251 | * and all the values fit on one lines, then print the array on a single |
| 252 | line. |
| 253 | * - otherwise, it the values do not fit on one line, or the array contains |
| 254 | * object or non empty array, then print one value per line. |
| 255 | * |
| 256 | * If the Value have comments then they are outputed according to their |
| 257 | #CommentPlacement. |
| 258 | * |
| 259 | * \param indentation Each level will be indented by this amount extra. |
| 260 | * \sa Reader, Value, Value::setComment() |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 261 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 262 | */ |
| 263 | class JSON_API StyledStreamWriter { |
| 264 | public: |
| 265 | StyledStreamWriter(std::string indentation = "\t"); |
| 266 | ~StyledStreamWriter() {} |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 267 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 268 | public: |
| 269 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 270 | * \param out Stream to write to. (Can be ostringstream, e.g.) |
| 271 | * \param root Value to serialize. |
| 272 | * \note There is no point in deriving from Writer, since write() should not |
| 273 | * return a value. |
| 274 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 275 | void write(std::ostream& out, const Value& root); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 276 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 277 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 278 | void writeValue(const Value& value); |
| 279 | void writeArrayValue(const Value& value); |
| 280 | bool isMultineArray(const Value& value); |
| 281 | void pushValue(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 282 | void writeIndent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 283 | void writeWithIndent(const std::string& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 284 | void indent(); |
| 285 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 286 | void writeCommentBeforeValue(const Value& root); |
| 287 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 288 | bool hasCommentForValue(const Value& value); |
| 289 | static std::string normalizeEOL(const std::string& text); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 290 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 291 | typedef std::vector<std::string> ChildValues; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 292 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 293 | ChildValues childValues_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 294 | std::ostream* document_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 295 | std::string indentString_; |
| 296 | int rightMargin_; |
| 297 | std::string indentation_; |
Christopher Dunn | d383056 | 2015-01-23 13:09:43 -0600 | [diff] [blame] | 298 | bool addChildValues_ : 1; |
| 299 | bool indented_ : 1; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 300 | }; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 301 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 302 | #if defined(JSON_HAS_INT64) |
| 303 | std::string JSON_API valueToString(Int value); |
| 304 | std::string JSON_API valueToString(UInt value); |
| 305 | #endif // if defined(JSON_HAS_INT64) |
| 306 | std::string JSON_API valueToString(LargestInt value); |
| 307 | std::string JSON_API valueToString(LargestUInt value); |
| 308 | std::string JSON_API valueToString(double value); |
| 309 | std::string JSON_API valueToString(bool value); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 310 | std::string JSON_API valueToQuotedString(const char* value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 311 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 312 | /// \brief Output using the StyledStreamWriter. |
| 313 | /// \see Json::operator>>() |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 314 | JSON_API std::ostream& operator<<(std::ostream&, const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 315 | |
| 316 | } // namespace Json |
| 317 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 318 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 319 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 320 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 321 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 322 | #endif // JSON_WRITER_H_INCLUDED |