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) |
Christopher Dunn | 177b7b8 | 2015-01-26 10:35:54 -0600 | [diff] [blame] | 12 | #include <ostream> |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
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: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [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 |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 51 | \return zero on success (For now, we always return zero, so check the |
| 52 | stream instead.) \throw std::exception possibly, depending on configuration |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 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; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [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 | */ |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 71 | JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, |
| 72 | Value const& root); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 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" |
uvok cheetah | c7728e8 | 2018-03-03 12:45:54 +0100 | [diff] [blame] | 96 | - "indentation": "<anything>". |
| 97 | - Setting this to an empty string also omits newline characters. |
Christopher Dunn | 29501c4 | 2015-02-10 23:03:27 -0600 | [diff] [blame] | 98 | - "enableYAMLCompatibility": false or true |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 99 | - slightly change the whitespace around colons |
Christopher Dunn | 29501c4 | 2015-02-10 23:03:27 -0600 | [diff] [blame] | 100 | - "dropNullPlaceholders": false or true |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 101 | - Drop the "null" string from the writer's output for nullValues. |
| 102 | Strictly speaking, this is not valid JSON. But when the output is being |
Josh Soref | e6a588a | 2017-12-03 11:54:29 -0500 | [diff] [blame] | 103 | fed to a browser's JavaScript, it makes for smaller output and the |
Christopher Dunn | 5a74470 | 2015-02-10 21:15:43 -0600 | [diff] [blame] | 104 | browser can handle the output just fine. |
drgler | 2084563 | 2015-09-03 22:19:22 +0200 | [diff] [blame] | 105 | - "useSpecialFloats": false or true |
| 106 | - If true, outputs non-finite floating point values in the following way: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 107 | NaN values as "NaN", positive infinity as "Infinity", and negative |
| 108 | infinity as "-Infinity". |
Mike R | a07fc53 | 2018-03-13 23:35:31 +0300 | [diff] [blame] | 109 | - "precision": int |
| 110 | - Number of precision digits for formatting of real values. |
| 111 | - "precisionType": "significant"(default) or "decimal" |
| 112 | - Type of precision for formatting of real values. |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 113 | |
| 114 | You can examine 'settings_` yourself |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 115 | to see the defaults. You can also write and read them just like any |
| 116 | JSON Value. |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 117 | \sa setDefaults() |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 118 | */ |
| 119 | Json::Value settings_; |
Christopher Dunn | 7eca3b4 | 2015-01-26 11:17:42 -0600 | [diff] [blame] | 120 | |
| 121 | StreamWriterBuilder(); |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 122 | ~StreamWriterBuilder() JSONCPP_OVERRIDE; |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 123 | |
Christopher Dunn | c41609b | 2015-02-09 18:44:53 -0600 | [diff] [blame] | 124 | /** |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 125 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
| 126 | */ |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 127 | StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 128 | |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 129 | /** \return true if 'settings' are legal and consistent; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 130 | * otherwise, indicate bad settings via 'invalid'. |
| 131 | */ |
| 132 | bool validate(Json::Value* invalid) const; |
Christopher Dunn | c312dd5 | 2015-03-04 14:56:37 -0600 | [diff] [blame] | 133 | /** A simple way to update a specific setting. |
| 134 | */ |
Radoslav Atanasov | ccd077f | 2018-12-13 14:18:04 +0100 | [diff] [blame^] | 135 | Value& operator[](const JSONCPP_STRING& key); |
Christopher Dunn | c312dd5 | 2015-03-04 14:56:37 -0600 | [diff] [blame] | 136 | |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 137 | /** Called by ctor, but you can use this to reset settings_. |
| 138 | * \pre 'settings' != NULL (but Json::null is fine) |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 139 | * \remark Defaults: |
| 140 | * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 141 | */ |
| 142 | static void setDefaults(Json::Value* settings); |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 143 | }; |
Christopher Dunn | 5fbfe3c | 2015-01-22 14:31:32 -0600 | [diff] [blame] | 144 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 145 | /** \brief Abstract class for writers. |
Christopher Dunn | ed495ed | 2015-03-08 14:01:28 -0500 | [diff] [blame] | 146 | * \deprecated Use StreamWriter. (And really, this is an implementation detail.) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 147 | */ |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 148 | class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 149 | public: |
| 150 | virtual ~Writer(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 151 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 152 | virtual JSONCPP_STRING write(const Value& root) = 0; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 153 | }; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 154 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 155 | /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format |
| 156 | *without formatting (not human friendly). |
| 157 | * |
| 158 | * The JSON document is written in a single line. It is not intended for 'human' |
| 159 | *consumption, |
luzpaz | 5b45aa5 | 2018-02-08 20:05:50 -0500 | [diff] [blame] | 160 | * but may be useful to support feature such as RPC where bandwidth is limited. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 161 | * \sa Reader, Value |
Christopher Dunn | 20d0967 | 2015-02-10 21:29:35 -0600 | [diff] [blame] | 162 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 163 | */ |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 164 | #if defined(_MSC_VER) |
Motti Lanzkron | 9bb984a | 2017-09-11 16:14:52 +0300 | [diff] [blame] | 165 | #pragma warning(push) |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 166 | #pragma warning(disable : 4996) // Deriving from deprecated class |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 167 | #endif |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 168 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter |
| 169 | : public Writer { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 170 | public: |
| 171 | FastWriter(); |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 172 | ~FastWriter() JSONCPP_OVERRIDE {} |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 173 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 174 | void enableYAMLCompatibility(); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 175 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 176 | /** \brief Drop the "null" string from the writer's output for nullValues. |
| 177 | * Strictly speaking, this is not valid JSON. But when the output is being |
Josh Soref | e6a588a | 2017-12-03 11:54:29 -0500 | [diff] [blame] | 178 | * fed to a browser's JavaScript, it makes for smaller output and the |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 179 | * browser can handle the output just fine. |
| 180 | */ |
| 181 | void dropNullPlaceholders(); |
Aaron Jacobs | ae3c7a7 | 2012-03-12 04:53:57 +0000 | [diff] [blame] | 182 | |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 183 | void omitEndingLineFeed(); |
| 184 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 185 | public: // overridden from Writer |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 186 | JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 187 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 188 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 189 | void writeValue(const Value& value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 190 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 191 | JSONCPP_STRING document_; |
Josh Soref | e6a588a | 2017-12-03 11:54:29 -0500 | [diff] [blame] | 192 | bool yamlCompatibilityEnabled_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 193 | bool dropNullPlaceholders_; |
Don Milham | 5bf1610 | 2014-09-02 17:09:07 -0600 | [diff] [blame] | 194 | bool omitEndingLineFeed_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 195 | }; |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 196 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 197 | #pragma warning(pop) |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 198 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 199 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 200 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 201 | *human friendly way. |
| 202 | * |
| 203 | * The rules for line break and indent are as follow: |
| 204 | * - Object value: |
| 205 | * - if empty then print {} without indent and line break |
| 206 | * - if not empty the print '{', line break & indent, print one value per |
| 207 | *line |
| 208 | * and then unindent and line break and print '}'. |
| 209 | * - Array value: |
| 210 | * - if empty then print [] without indent and line break |
| 211 | * - if the array contains no object value, empty array or some other value |
| 212 | *types, |
| 213 | * and all the values fit on one lines, then print the array on a single |
| 214 | *line. |
| 215 | * - otherwise, it the values do not fit on one line, or the array contains |
| 216 | * object or non empty array, then print one value per line. |
| 217 | * |
| 218 | * If the Value have comments then they are outputed according to their |
| 219 | *#CommentPlacement. |
| 220 | * |
| 221 | * \sa Reader, Value, Value::setComment() |
Christopher Dunn | 38042b3 | 2015-01-26 11:23:31 -0600 | [diff] [blame] | 222 | * \deprecated Use StreamWriterBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 223 | */ |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 224 | #if defined(_MSC_VER) |
Motti Lanzkron | 9bb984a | 2017-09-11 16:14:52 +0300 | [diff] [blame] | 225 | #pragma warning(push) |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 226 | #pragma warning(disable : 4996) // Deriving from deprecated class |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 227 | #endif |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 228 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API |
| 229 | StyledWriter : public Writer { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 230 | public: |
| 231 | StyledWriter(); |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 232 | ~StyledWriter() 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 | public: // overridden from Writer |
| 235 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 236 | * \param root Value to serialize. |
| 237 | * \return String containing the JSON document that represents the root value. |
| 238 | */ |
Christopher Dunn | 98e981d | 2016-03-21 21:00:24 -0500 | [diff] [blame] | 239 | JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 240 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 241 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 242 | void writeValue(const Value& value); |
| 243 | void writeArrayValue(const Value& value); |
Josh Soref | e6a588a | 2017-12-03 11:54:29 -0500 | [diff] [blame] | 244 | bool isMultilineArray(const Value& value); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 245 | void pushValue(const JSONCPP_STRING& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 246 | void writeIndent(); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 247 | void writeWithIndent(const JSONCPP_STRING& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 248 | void indent(); |
| 249 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 250 | void writeCommentBeforeValue(const Value& root); |
| 251 | void writeCommentAfterValueOnSameLine(const Value& root); |
Marian Klymov | 48112c8 | 2018-06-02 19:43:31 +0300 | [diff] [blame] | 252 | static bool hasCommentForValue(const Value& value); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 253 | static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 254 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 255 | typedef std::vector<JSONCPP_STRING> ChildValues; |
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 | ChildValues childValues_; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 258 | JSONCPP_STRING document_; |
| 259 | JSONCPP_STRING indentString_; |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 260 | unsigned int rightMargin_; |
| 261 | unsigned int indentSize_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 262 | bool addChildValues_; |
| 263 | }; |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 264 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 265 | #pragma warning(pop) |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 266 | #endif |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 267 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 268 | /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a |
| 269 | human friendly way, |
| 270 | to a stream rather than to a string. |
| 271 | * |
| 272 | * The rules for line break and indent are as follow: |
| 273 | * - Object value: |
| 274 | * - if empty then print {} without indent and line break |
| 275 | * - if not empty the print '{', line break & indent, print one value per |
| 276 | line |
| 277 | * and then unindent and line break and print '}'. |
| 278 | * - Array value: |
| 279 | * - if empty then print [] without indent and line break |
| 280 | * - if the array contains no object value, empty array or some other value |
| 281 | types, |
| 282 | * and all the values fit on one lines, then print the array on a single |
| 283 | line. |
| 284 | * - otherwise, it the values do not fit on one line, or the array contains |
| 285 | * object or non empty array, then print one value per line. |
| 286 | * |
| 287 | * If the Value have comments then they are outputed according to their |
| 288 | #CommentPlacement. |
| 289 | * |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 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 | */ |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 293 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 294 | #pragma warning(push) |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 295 | #pragma warning(disable : 4996) // Deriving from deprecated class |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 296 | #endif |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 297 | class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API |
| 298 | StyledStreamWriter { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 299 | public: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 300 | /** |
| 301 | * \param indentation Each level will be indented by this amount extra. |
| 302 | */ |
Marian Klymov | c8bb600 | 2018-06-02 19:41:57 +0300 | [diff] [blame] | 303 | StyledStreamWriter(const JSONCPP_STRING& indentation = "\t"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 304 | ~StyledStreamWriter() {} |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 305 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 306 | public: |
| 307 | /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. |
| 308 | * \param out Stream to write to. (Can be ostringstream, e.g.) |
| 309 | * \param root Value to serialize. |
| 310 | * \note There is no point in deriving from Writer, since write() should not |
| 311 | * return a value. |
| 312 | */ |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 313 | void write(JSONCPP_OSTREAM& out, const Value& root); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 314 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 315 | private: |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 316 | void writeValue(const Value& value); |
| 317 | void writeArrayValue(const Value& value); |
Josh Soref | e6a588a | 2017-12-03 11:54:29 -0500 | [diff] [blame] | 318 | bool isMultilineArray(const Value& value); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 319 | void pushValue(const JSONCPP_STRING& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 320 | void writeIndent(); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 321 | void writeWithIndent(const JSONCPP_STRING& value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 322 | void indent(); |
| 323 | void unindent(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 324 | void writeCommentBeforeValue(const Value& root); |
| 325 | void writeCommentAfterValueOnSameLine(const Value& root); |
Marian Klymov | 48112c8 | 2018-06-02 19:43:31 +0300 | [diff] [blame] | 326 | static bool hasCommentForValue(const Value& value); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 327 | static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text); |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 328 | |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 329 | typedef std::vector<JSONCPP_STRING> ChildValues; |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 330 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 331 | ChildValues childValues_; |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 332 | JSONCPP_OSTREAM* document_; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 333 | JSONCPP_STRING indentString_; |
Christopher Dunn | fef4b75 | 2016-02-06 09:59:18 -0600 | [diff] [blame] | 334 | unsigned int rightMargin_; |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 335 | JSONCPP_STRING indentation_; |
Christopher Dunn | d383056 | 2015-01-23 13:09:43 -0600 | [diff] [blame] | 336 | bool addChildValues_ : 1; |
| 337 | bool indented_ : 1; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 338 | }; |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 339 | #if defined(_MSC_VER) |
Christopher Dunn | 132840a | 2017-09-11 13:39:38 -0500 | [diff] [blame] | 340 | #pragma warning(pop) |
Hugh Bellamy | 7287065 | 2017-09-16 10:01:09 +0100 | [diff] [blame] | 341 | #endif |
Christopher Dunn | 605cd7e | 2007-06-13 15:55:50 +0000 | [diff] [blame] | 342 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 343 | #if defined(JSON_HAS_INT64) |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 344 | JSONCPP_STRING JSON_API valueToString(Int value); |
| 345 | JSONCPP_STRING JSON_API valueToString(UInt value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 346 | #endif // if defined(JSON_HAS_INT64) |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 347 | JSONCPP_STRING JSON_API valueToString(LargestInt value); |
| 348 | JSONCPP_STRING JSON_API valueToString(LargestUInt value); |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 349 | JSONCPP_STRING JSON_API |
| 350 | valueToString(double value, |
| 351 | unsigned int precision = Value::defaultRealPrecision, |
| 352 | PrecisionType precisionType = PrecisionType::significantDigits); |
Christopher Dawes | 75570d7 | 2016-03-07 08:29:59 +0000 | [diff] [blame] | 353 | JSONCPP_STRING JSON_API valueToString(bool value); |
| 354 | JSONCPP_STRING JSON_API valueToQuotedString(const char* value); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 355 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 356 | /// \brief Output using the StyledStreamWriter. |
| 357 | /// \see Json::operator>>() |
Christopher Dunn | 724ba29 | 2016-03-06 11:47:35 -0600 | [diff] [blame] | 358 | JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 359 | |
| 360 | } // namespace Json |
| 361 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 362 | #pragma pack(pop) |
| 363 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 364 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 365 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 366 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 367 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 368 | #endif // JSON_WRITER_H_INCLUDED |