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