blob: 8a1279bfca4b9c41a13ea0817edcfbeef39056fb [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00002// 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 Dunn6d135cb2007-06-13 15:51:04 +00006#ifndef JSON_WRITER_H_INCLUDED
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10007#define JSON_WRITER_H_INCLUDED
Christopher Dunn6d135cb2007-06-13 15:51:04 +00008
Baptiste Lepilleureadc4782011-05-02 21:09:30 +00009#if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100010#include "value.h"
Baptiste Lepilleureadc4782011-05-02 21:09:30 +000011#endif // if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100012#include <vector>
13#include <string>
Christopher Dunn177b7b82015-01-26 10:35:54 -060014#include <ostream>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000015
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100016// Disable warning C4251: <data member>: <type> needs to have dll-interface to
17// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000018#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100019#pragma warning(push)
20#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000021#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
22
Sergiy80d6e666f2016-12-03 22:29:14 +020023#pragma pack(push, 8)
24
Christopher Dunn6d135cb2007-06-13 15:51:04 +000025namespace Json {
26
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100027class Value;
Christopher Dunn489707f2015-01-22 15:25:30 -060028
29/**
30
31Usage:
Christopher Dunn9da9f842015-01-25 19:20:43 -060032\code
Christopher Dunn489707f2015-01-22 15:25:30 -060033 using namespace Json;
Christopher Dunn38042b32015-01-26 11:23:31 -060034 void writeToStdout(StreamWriter::Factory const& factory, Value const& value) {
Christopher Dunn999f5912015-01-26 11:12:53 -060035 std::unique_ptr<StreamWriter> const writer(
Christopher Dunnc41609b2015-02-09 18:44:53 -060036 factory.newStreamWriter());
37 writer->write(value, &std::cout);
Christopher Dunn999f5912015-01-26 11:12:53 -060038 std::cout << std::endl; // add lf and flush
39 }
Christopher Dunn9da9f842015-01-25 19:20:43 -060040\endcode
Christopher Dunn489707f2015-01-22 15:25:30 -060041*/
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060042class JSON_API StreamWriter {
43protected:
Christopher Dunn724ba292016-03-06 11:47:35 -060044 JSONCPP_OSTREAM* sout_; // not owned; will not delete
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060045public:
Christopher Dunnc41609b2015-02-09 18:44:53 -060046 StreamWriter();
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060047 virtual ~StreamWriter();
Christopher Dunnc41609b2015-02-09 18:44:53 -060048 /** 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 Dunn717b0862015-03-08 12:05:28 -050051 \return zero on success (For now, we always return zero, so check the stream instead.)
Christopher Dunnc41609b2015-02-09 18:44:53 -060052 \throw std::exception possibly, depending on configuration
53 */
Christopher Dunn724ba292016-03-06 11:47:35 -060054 virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060055
Christopher Dunn177b7b82015-01-26 10:35:54 -060056 /** \brief A simple abstract factory.
57 */
58 class JSON_API Factory {
59 public:
60 virtual ~Factory();
Christopher Dunna9e1ab32015-02-09 17:22:28 -060061 /** \brief Allocate a CharReader via operator new().
Christopher Dunna9e1ab32015-02-09 17:22:28 -060062 * \throw std::exception if something goes wrong (e.g. invalid settings)
63 */
Christopher Dunnc41609b2015-02-09 18:44:53 -060064 virtual StreamWriter* newStreamWriter() const = 0;
Christopher Dunn6065a1c2015-01-26 11:01:15 -060065 }; // Factory
66}; // StreamWriter
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060067
Christopher Dunn694dbcb2015-02-09 15:25:57 -060068/** \brief Write into stringstream, then return string, for convenience.
69 * A StreamWriter will be created from the factory, used, and then deleted.
70 */
Christopher Dawes75570d72016-03-07 08:29:59 +000071JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
Christopher Dunn6065a1c2015-01-26 11:01:15 -060072
73
74/** \brief Build a StreamWriter implementation.
Christopher Dunn999f5912015-01-26 11:12:53 -060075
76Usage:
77\code
78 using namespace Json;
79 Value value = ...;
Christopher Dunn38042b32015-01-26 11:23:31 -060080 StreamWriterBuilder builder;
Christopher Dunn37dde9d2015-03-04 15:04:19 -060081 builder["commentStyle"] = "None";
82 builder["indentation"] = " "; // or whatever you like
Christopher Dunna9e1ab32015-02-09 17:22:28 -060083 std::unique_ptr<Json::StreamWriter> writer(
Christopher Dunnc41609b2015-02-09 18:44:53 -060084 builder.newStreamWriter());
85 writer->write(value, &std::cout);
Christopher Dunn999f5912015-01-26 11:12:53 -060086 std::cout << std::endl; // add lf and flush
87\endcode
88*/
Christopher Dunn6065a1c2015-01-26 11:01:15 -060089class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
Christopher Dunn6065a1c2015-01-26 11:01:15 -060090public:
Christopher Dunna9e1ab32015-02-09 17:22:28 -060091 // 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 Dunn5a744702015-02-10 21:15:43 -060095 - "commentStyle": "None" or "All"
Christopher Dunnf757c182015-02-09 18:24:56 -060096 - "indentation": "<anything>"
Christopher Dunn29501c42015-02-10 23:03:27 -060097 - "enableYAMLCompatibility": false or true
Christopher Dunn5a744702015-02-10 21:15:43 -060098 - slightly change the whitespace around colons
Christopher Dunn29501c42015-02-10 23:03:27 -060099 - "dropNullPlaceholders": false or true
Christopher Dunn5a744702015-02-10 21:15:43 -0600100 - 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.
drgler20845632015-09-03 22:19:22 +0200104 - "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 Dunnf757c182015-02-09 18:24:56 -0600108
109 You can examine 'settings_` yourself
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600110 to see the defaults. You can also write and read them just like any
111 JSON Value.
Christopher Dunnf757c182015-02-09 18:24:56 -0600112 \sa setDefaults()
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600113 */
114 Json::Value settings_;
Christopher Dunn7eca3b42015-01-26 11:17:42 -0600115
116 StreamWriterBuilder();
Christopher Dunn98e981d2016-03-21 21:00:24 -0500117 ~StreamWriterBuilder() JSONCPP_OVERRIDE;
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600118
Christopher Dunnc41609b2015-02-09 18:44:53 -0600119 /**
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600120 * \throw std::exception if something goes wrong (e.g. invalid settings)
121 */
Christopher Dunn98e981d2016-03-21 21:00:24 -0500122 StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600123
Christopher Dunnf757c182015-02-09 18:24:56 -0600124 /** \return true if 'settings' are legal and consistent;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600125 * otherwise, indicate bad settings via 'invalid'.
126 */
127 bool validate(Json::Value* invalid) const;
Christopher Dunnc312dd52015-03-04 14:56:37 -0600128 /** A simple way to update a specific setting.
129 */
Christopher Dawes75570d72016-03-07 08:29:59 +0000130 Value& operator[](JSONCPP_STRING key);
Christopher Dunnc312dd52015-03-04 14:56:37 -0600131
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600132 /** Called by ctor, but you can use this to reset settings_.
133 * \pre 'settings' != NULL (but Json::null is fine)
Christopher Dunn3cf91752015-02-09 18:16:24 -0600134 * \remark Defaults:
135 * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600136 */
137 static void setDefaults(Json::Value* settings);
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600138};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600139
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140/** \brief Abstract class for writers.
Christopher Dunned495ed2015-03-08 14:01:28 -0500141 * \deprecated Use StreamWriter. (And really, this is an implementation detail.)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000142 */
143class JSON_API Writer {
144public:
145 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000146
Christopher Dawes75570d72016-03-07 08:29:59 +0000147 virtual JSONCPP_STRING write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000148};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000149
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000150/** \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 Dunn20d09672015-02-10 21:29:35 -0600157 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000158 */
159class JSON_API FastWriter : public Writer {
Christopher Dunned495ed2015-03-08 14:01:28 -0500160
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161public:
162 FastWriter();
Christopher Dunn98e981d2016-03-21 21:00:24 -0500163 ~FastWriter() JSONCPP_OVERRIDE {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000164
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000166
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000167 /** \brief Drop the "null" string from the writer's output for nullValues.
168 * Strictly speaking, this is not valid JSON. But when the output is being
169 * fed to a browser's Javascript, it makes for smaller output and the
170 * browser can handle the output just fine.
171 */
172 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000173
Don Milham5bf16102014-09-02 17:09:07 -0600174 void omitEndingLineFeed();
175
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000176public: // overridden from Writer
Christopher Dunn98e981d2016-03-21 21:00:24 -0500177 JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000178
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000180 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000181
Christopher Dawes75570d72016-03-07 08:29:59 +0000182 JSONCPP_STRING document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 bool yamlCompatiblityEnabled_;
184 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600185 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000187
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
189 *human friendly way.
190 *
191 * The rules for line break and indent are as follow:
192 * - Object value:
193 * - if empty then print {} without indent and line break
194 * - if not empty the print '{', line break & indent, print one value per
195 *line
196 * and then unindent and line break and print '}'.
197 * - Array value:
198 * - if empty then print [] without indent and line break
199 * - if the array contains no object value, empty array or some other value
200 *types,
201 * and all the values fit on one lines, then print the array on a single
202 *line.
203 * - otherwise, it the values do not fit on one line, or the array contains
204 * object or non empty array, then print one value per line.
205 *
206 * If the Value have comments then they are outputed according to their
207 *#CommentPlacement.
208 *
209 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600210 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211 */
212class JSON_API StyledWriter : public Writer {
213public:
214 StyledWriter();
Christopher Dunn98e981d2016-03-21 21:00:24 -0500215 ~StyledWriter() JSONCPP_OVERRIDE {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000216
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000217public: // overridden from Writer
218 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
219 * \param root Value to serialize.
220 * \return String containing the JSON document that represents the root value.
221 */
Christopher Dunn98e981d2016-03-21 21:00:24 -0500222 JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000223
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000224private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000225 void writeValue(const Value& value);
226 void writeArrayValue(const Value& value);
227 bool isMultineArray(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000228 void pushValue(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000229 void writeIndent();
Christopher Dawes75570d72016-03-07 08:29:59 +0000230 void writeWithIndent(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000231 void indent();
232 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000233 void writeCommentBeforeValue(const Value& root);
234 void writeCommentAfterValueOnSameLine(const Value& root);
235 bool hasCommentForValue(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000236 static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000237
Christopher Dawes75570d72016-03-07 08:29:59 +0000238 typedef std::vector<JSONCPP_STRING> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000239
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000240 ChildValues childValues_;
Christopher Dawes75570d72016-03-07 08:29:59 +0000241 JSONCPP_STRING document_;
242 JSONCPP_STRING indentString_;
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600243 unsigned int rightMargin_;
244 unsigned int indentSize_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000245 bool addChildValues_;
246};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000247
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
249 human friendly way,
250 to a stream rather than to a string.
251 *
252 * The rules for line break and indent are as follow:
253 * - Object value:
254 * - if empty then print {} without indent and line break
255 * - if not empty the print '{', line break & indent, print one value per
256 line
257 * and then unindent and line break and print '}'.
258 * - Array value:
259 * - if empty then print [] without indent and line break
260 * - if the array contains no object value, empty array or some other value
261 types,
262 * and all the values fit on one lines, then print the array on a single
263 line.
264 * - otherwise, it the values do not fit on one line, or the array contains
265 * object or non empty array, then print one value per line.
266 *
267 * If the Value have comments then they are outputed according to their
268 #CommentPlacement.
269 *
270 * \param indentation Each level will be indented by this amount extra.
271 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600272 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000273 */
274class JSON_API StyledStreamWriter {
275public:
Christopher Dawes75570d72016-03-07 08:29:59 +0000276 StyledStreamWriter(JSONCPP_STRING indentation = "\t");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000278
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000279public:
280 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
281 * \param out Stream to write to. (Can be ostringstream, e.g.)
282 * \param root Value to serialize.
283 * \note There is no point in deriving from Writer, since write() should not
284 * return a value.
285 */
Christopher Dunn724ba292016-03-06 11:47:35 -0600286 void write(JSONCPP_OSTREAM& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000287
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000288private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000289 void writeValue(const Value& value);
290 void writeArrayValue(const Value& value);
291 bool isMultineArray(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000292 void pushValue(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293 void writeIndent();
Christopher Dawes75570d72016-03-07 08:29:59 +0000294 void writeWithIndent(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000295 void indent();
296 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000297 void writeCommentBeforeValue(const Value& root);
298 void writeCommentAfterValueOnSameLine(const Value& root);
299 bool hasCommentForValue(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000300 static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000301
Christopher Dawes75570d72016-03-07 08:29:59 +0000302 typedef std::vector<JSONCPP_STRING> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000303
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000304 ChildValues childValues_;
Christopher Dunn724ba292016-03-06 11:47:35 -0600305 JSONCPP_OSTREAM* document_;
Christopher Dawes75570d72016-03-07 08:29:59 +0000306 JSONCPP_STRING indentString_;
Christopher Dunnfef4b752016-02-06 09:59:18 -0600307 unsigned int rightMargin_;
Christopher Dawes75570d72016-03-07 08:29:59 +0000308 JSONCPP_STRING indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600309 bool addChildValues_ : 1;
310 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000311};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000312
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000313#if defined(JSON_HAS_INT64)
Christopher Dawes75570d72016-03-07 08:29:59 +0000314JSONCPP_STRING JSON_API valueToString(Int value);
315JSONCPP_STRING JSON_API valueToString(UInt value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316#endif // if defined(JSON_HAS_INT64)
Christopher Dawes75570d72016-03-07 08:29:59 +0000317JSONCPP_STRING JSON_API valueToString(LargestInt value);
318JSONCPP_STRING JSON_API valueToString(LargestUInt value);
319JSONCPP_STRING JSON_API valueToString(double value);
320JSONCPP_STRING JSON_API valueToString(bool value);
321JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000322
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323/// \brief Output using the StyledStreamWriter.
324/// \see Json::operator>>()
Christopher Dunn724ba292016-03-06 11:47:35 -0600325JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000326
327} // namespace Json
328
Sergiy80d6e666f2016-12-03 22:29:14 +0200329#pragma pack(pop)
330
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000331#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000333#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
334
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000335#endif // JSON_WRITER_H_INCLUDED