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