Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 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... |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 18 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) && defined(_MSC_VER) |
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 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 23 | #pragma pack(push, 8) |
| 24 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 25 | namespace Json { |
| 26 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 27 | class Value; |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | |
| 31 | Usage: |
Christopher Dunn | 9da9f84 | 2015-01-25 19:20:43 -0600 | [diff] [blame] | 32 | \code |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame] | 33 | using namespace Json; |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 34 | void writeToStdout(StreamWriter::Factory const& factory, Value const& value) { |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 35 | std::unique_ptr<StreamWriter> const writer( |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 36 | factory.newStreamWriter()); |
| 37 | writer->write(value, &std::cout); |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 38 | std::cout << std::endl; // add lf and flush |
| 39 | } |
Christopher Dunn | 9da9f84 | 2015-01-25 19:20:43 -0600 | [diff] [blame] | 40 | \endcode |
Christopher Dunn | 489707f | 2015-01-22 15:25:30 -0600 | [diff] [blame] | 41 | */ |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 42 | class JSON_API StreamWriter { |
| 43 | protected: |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 44 | JSONCPP_OSTREAM* sout_; // not owned; will not delete |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 45 | public: |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 46 | StreamWriter(); |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 47 | virtual ~StreamWriter(); |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 48 | /** Write Value into document as configured in sub-class. |
| 49 | Do not take ownership of sout, but maintain a reference during function. |
| 50 | \pre sout != NULL |
Christopher Dunn | 717b086 | 2015-03-08 12:05:28 -0500 | [diff] [blame] | 51 | \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] | 52 | \throw std::exception possibly, depending on configuration |
| 53 | */ |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 54 | virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 55 | |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 56 | /** \brief A simple abstract factory. |
| 57 | */ |
| 58 | class JSON_API Factory { |
| 59 | public: |
| 60 | virtual ~Factory(); |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 61 | /** \brief Allocate a CharReader via operator new(). |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 62 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
| 63 | */ |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 64 | virtual StreamWriter* newStreamWriter() const = 0; |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 65 | }; // Factory |
| 66 | }; // StreamWriter |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 67 | |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 68 | /** \brief Write into stringstream, then return string, for convenience. |
| 69 | * A StreamWriter will be created from the factory, used, and then deleted. |
| 70 | */ |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 71 | JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 72 | |
| 73 | |
| 74 | /** \brief Build a StreamWriter implementation. |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 75 | |
| 76 | Usage: |
| 77 | \code |
| 78 | using namespace Json; |
| 79 | Value value = ...; |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 80 | StreamWriterBuilder builder; |
Christopher Dunn | 37dde9d | 2015-03-04 15:04:19 -0600 | [diff] [blame] | 81 | builder["commentStyle"] = "None"; |
| 82 | builder["indentation"] = " "; // or whatever you like |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 83 | std::unique_ptr<Json::StreamWriter> writer( |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 84 | builder.newStreamWriter()); |
| 85 | writer->write(value, &std::cout); |
Christopher Dunn | 999f591 | 2015-01-26 11:12:53 -0600 | [diff] [blame] | 86 | std::cout << std::endl; // add lf and flush |
| 87 | \endcode |
| 88 | */ |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 89 | class JSON_API StreamWriterBuilder : public StreamWriter::Factory { |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 90 | public: |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 91 | // Note: We use a Json::Value so that we can add data-members to this class |
| 92 | // without a major version bump. |
| 93 | /** Configuration of this builder. |
| 94 | Available settings (case-sensitive): |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 95 | - "commentStyle": "None" or "All" |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 96 | - "indentation": "<anything>" |
Christopher Dunn | 29501c4 | 2015-02-10 23:03:27 -0600 | [diff] [blame] | 97 | - "enableYAMLCompatibility": false or true |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 98 | - slightly change the whitespace around colons |
Christopher Dunn | 29501c4 | 2015-02-10 23:03:27 -0600 | [diff] [blame] | 99 | - "dropNullPlaceholders": false or true |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 100 | - Drop the "null" string from the writer's output for nullValues. |
| 101 | Strictly speaking, this is not valid JSON. But when the output is being |
| 102 | fed to a browser's Javascript, it makes for smaller output and the |
| 103 | browser can handle the output just fine. |
drgler | 2084563 | 2015-09-03 22:19:22 +0200 | [diff] [blame] | 104 | - "useSpecialFloats": false or true |
| 105 | - If true, outputs non-finite floating point values in the following way: |
| 106 | NaN values as "NaN", positive infinity as "Infinity", and negative infinity |
| 107 | as "-Infinity". |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 108 | |
| 109 | You can examine 'settings_` yourself |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 110 | to see the defaults. You can also write and read them just like any |
| 111 | JSON Value. |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 112 | \sa setDefaults() |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 113 | */ |
| 114 | Json::Value settings_; |
Christopher Dunn | 7eca3b4 | 2015-01-26 11:17:42 -0600 | [diff] [blame] | 115 | |
| 116 | StreamWriterBuilder(); |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 117 | ~StreamWriterBuilder() JSONCPP_OVERRIDE; |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 118 | |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 119 | /** |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 120 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
| 121 | */ |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 122 | StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 123 | |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 124 | /** \return true if 'settings' are legal and consistent; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 125 | * otherwise, indicate bad settings via 'invalid'. |
| 126 | */ |
| 127 | bool validate(Json::Value* invalid) const; |
Christopher Dunn | c312dd5 | 2015-03-04 14:56:37 -0600 | [diff] [blame] | 128 | /** A simple way to update a specific setting. |
| 129 | */ |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 130 | Value& operator[](JSONCPP_STRING key); |
Christopher Dunn | c312dd5 | 2015-03-04 14:56:37 -0600 | [diff] [blame] | 131 | |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 132 | /** Called by ctor, but you can use this to reset settings_. |
| 133 | * \pre 'settings' != NULL (but Json::null is fine) |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 134 | * \remark Defaults: |
| 135 | * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 136 | */ |
| 137 | static void setDefaults(Json::Value* settings); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 138 | }; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 139 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 140 | /** \brief Abstract class for writers. |
Christopher Dunn | ed495ed | 2015-03-08 14:01:28 -0500 | [diff] [blame] | 141 | * \deprecated Use StreamWriter. (And really, this is an implementation detail.) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 142 | */ |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 143 | class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 144 | public: |
| 145 | virtual ~Writer(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 146 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 147 | virtual JSONCPP_STRING write(const Value& root) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 148 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 149 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 150 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format |
| 151 | *without formatting (not human friendly). |
| 152 | * |
| 153 | * The JSON document is written in a single line. It is not intended for 'human' |
| 154 | *consumption, |
| 155 | * but may be usefull to support feature such as RPC where bandwith is limited. |
| 156 | * \sa Reader, Value |
Christopher Dunn | 20d0967 | 2015-02-10 21:29:35 -0600 | [diff] [blame] | 157 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 158 | */ |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 159 | #if defined(_MSC_VER) |
Motti Lanzkron | 9bb984a | 2017-09-11 16:14:52 +0300 | [diff] [blame] | 160 | #pragma warning(push) |
| 161 | #pragma warning(disable:4996) // Deriving from deprecated class |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 162 | #endif |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 163 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter : public Writer { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 164 | public: |
| 165 | FastWriter(); |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 166 | ~FastWriter() JSONCPP_OVERRIDE {} |
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 | void enableYAMLCompatibility(); |
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 Drop the "null" string from the writer's output for nullValues. |
| 171 | * Strictly speaking, this is not valid JSON. But when the output is being |
| 172 | * fed to a browser's Javascript, it makes for smaller output and the |
| 173 | * browser can handle the output just fine. |
| 174 | */ |
| 175 | void dropNullPlaceholders(); |
Aaron Jacobs | ae3c7a7 | 2012-03-12 04:53:57 +0000 | [diff] [blame] | 176 | |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 177 | void omitEndingLineFeed(); |
| 178 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 179 | public: // overridden from Writer |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 180 | JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 181 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 182 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 183 | void writeValue(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 184 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 185 | JSONCPP_STRING document_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 186 | bool yamlCompatiblityEnabled_; |
| 187 | bool dropNullPlaceholders_; |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 188 | bool omitEndingLineFeed_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 189 | }; |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 190 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 191 | #pragma warning(pop) |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 192 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 193 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 194 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 195 | *human friendly way. |
| 196 | * |
| 197 | * The rules for line break and indent are as follow: |
| 198 | * - Object value: |
| 199 | * - if empty then print {} without indent and line break |
| 200 | * - if not empty the print '{', line break & indent, print one value per |
| 201 | *line |
| 202 | * and then unindent and line break and print '}'. |
| 203 | * - Array value: |
| 204 | * - if empty then print [] without indent and line break |
| 205 | * - if the array contains no object value, empty array or some other value |
| 206 | *types, |
| 207 | * and all the values fit on one lines, then print the array on a single |
| 208 | *line. |
| 209 | * - otherwise, it the values do not fit on one line, or the array contains |
| 210 | * object or non empty array, then print one value per line. |
| 211 | * |
| 212 | * If the Value have comments then they are outputed according to their |
| 213 | *#CommentPlacement. |
| 214 | * |
| 215 | * \sa Reader, Value, Value::setComment() |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 216 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 217 | */ |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 218 | #if defined(_MSC_VER) |
Motti Lanzkron | 9bb984a | 2017-09-11 16:14:52 +0300 | [diff] [blame] | 219 | #pragma warning(push) |
| 220 | #pragma warning(disable:4996) // Deriving from deprecated class |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 221 | #endif |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 222 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledWriter : public Writer { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 223 | public: |
| 224 | StyledWriter(); |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 225 | ~StyledWriter() JSONCPP_OVERRIDE {} |
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 | public: // overridden from Writer |
| 228 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 229 | * \param root Value to serialize. |
| 230 | * \return String containing the JSON document that represents the root value. |
| 231 | */ |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 232 | JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 233 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 234 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 235 | void writeValue(const Value& value); |
| 236 | void writeArrayValue(const Value& value); |
| 237 | bool isMultineArray(const Value& value); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 238 | void pushValue(const JSONCPP_STRING& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 239 | void writeIndent(); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 240 | void writeWithIndent(const JSONCPP_STRING& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 241 | void indent(); |
| 242 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 243 | void writeCommentBeforeValue(const Value& root); |
| 244 | void writeCommentAfterValueOnSameLine(const Value& root); |
| 245 | bool hasCommentForValue(const Value& value); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 246 | static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 247 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 248 | typedef std::vector<JSONCPP_STRING> ChildValues; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 249 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 250 | ChildValues childValues_; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 251 | JSONCPP_STRING document_; |
| 252 | JSONCPP_STRING indentString_; |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 253 | unsigned int rightMargin_; |
| 254 | unsigned int indentSize_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 255 | bool addChildValues_; |
| 256 | }; |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 257 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 258 | #pragma warning(pop) |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 259 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 260 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 261 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 262 | human friendly way, |
| 263 | to a stream rather than to a string. |
| 264 | * |
| 265 | * The rules for line break and indent are as follow: |
| 266 | * - Object value: |
| 267 | * - if empty then print {} without indent and line break |
| 268 | * - if not empty the print '{', line break & indent, print one value per |
| 269 | line |
| 270 | * and then unindent and line break and print '}'. |
| 271 | * - Array value: |
| 272 | * - if empty then print [] without indent and line break |
| 273 | * - if the array contains no object value, empty array or some other value |
| 274 | types, |
| 275 | * and all the values fit on one lines, then print the array on a single |
| 276 | line. |
| 277 | * - otherwise, it the values do not fit on one line, or the array contains |
| 278 | * object or non empty array, then print one value per line. |
| 279 | * |
| 280 | * If the Value have comments then they are outputed according to their |
| 281 | #CommentPlacement. |
| 282 | * |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 283 | * \sa Reader, Value, Value::setComment() |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 284 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 285 | */ |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 286 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 287 | #pragma warning(push) |
| 288 | #pragma warning(disable:4996) // Deriving from deprecated class |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 289 | #endif |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 290 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledStreamWriter { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 291 | public: |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 292 | /** |
| 293 | * \param indentation Each level will be indented by this amount extra. |
| 294 | */ |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 295 | StyledStreamWriter(JSONCPP_STRING indentation = "\t"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 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 | */ |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 305 | void write(JSONCPP_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); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 311 | void pushValue(const JSONCPP_STRING& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 312 | void writeIndent(); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 313 | void writeWithIndent(const JSONCPP_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); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 319 | static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 320 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 321 | typedef std::vector<JSONCPP_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_; |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 324 | JSONCPP_OSTREAM* document_; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 325 | JSONCPP_STRING indentString_; |
Christopher Dunn | fef4b75 | 2016-02-06 09:59:18 -0600 | [diff] [blame] | 326 | unsigned int rightMargin_; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 327 | JSONCPP_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 | }; |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 331 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 332 | #pragma warning(pop) |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 333 | #endif |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 334 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 335 | #if defined(JSON_HAS_INT64) |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 336 | JSONCPP_STRING JSON_API valueToString(Int value); |
| 337 | JSONCPP_STRING JSON_API valueToString(UInt value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 338 | #endif // if defined(JSON_HAS_INT64) |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 339 | JSONCPP_STRING JSON_API valueToString(LargestInt value); |
| 340 | JSONCPP_STRING JSON_API valueToString(LargestUInt value); |
| 341 | JSONCPP_STRING JSON_API valueToString(double value); |
| 342 | JSONCPP_STRING JSON_API valueToString(bool value); |
| 343 | JSONCPP_STRING JSON_API valueToQuotedString(const char* value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 344 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 345 | /// \brief Output using the StyledStreamWriter. |
| 346 | /// \see Json::operator>>() |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 347 | JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 348 | |
| 349 | } // namespace Json |
| 350 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 351 | #pragma pack(pop) |
| 352 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 353 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 354 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 355 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 356 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 357 | #endif // JSON_WRITER_H_INCLUDED |