blob: 2c1e65bf16f86b11253892c15fa0ecebf061c064 [file] [log] [blame]
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00001// 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 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
Christopher Dunn6d135cb2007-06-13 15:51:04 +000023namespace Json {
24
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100025class Value;
Christopher Dunn489707f2015-01-22 15:25:30 -060026
27/**
28
29Usage:
Christopher Dunn9da9f842015-01-25 19:20:43 -060030\code
Christopher Dunn489707f2015-01-22 15:25:30 -060031 using namespace Json;
Christopher Dunn38042b32015-01-26 11:23:31 -060032 void writeToStdout(StreamWriter::Factory const& factory, Value const& value) {
Christopher Dunn999f5912015-01-26 11:12:53 -060033 std::unique_ptr<StreamWriter> const writer(
Christopher Dunnc41609b2015-02-09 18:44:53 -060034 factory.newStreamWriter());
35 writer->write(value, &std::cout);
Christopher Dunn999f5912015-01-26 11:12:53 -060036 std::cout << std::endl; // add lf and flush
37 }
Christopher Dunn9da9f842015-01-25 19:20:43 -060038\endcode
Christopher Dunn489707f2015-01-22 15:25:30 -060039*/
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060040class JSON_API StreamWriter {
41protected:
Christopher Dunn724ba292016-03-06 11:47:35 -060042 JSONCPP_OSTREAM* sout_; // not owned; will not delete
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060043public:
Christopher Dunnc41609b2015-02-09 18:44:53 -060044 StreamWriter();
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060045 virtual ~StreamWriter();
Christopher Dunnc41609b2015-02-09 18:44:53 -060046 /** 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 Dunn717b0862015-03-08 12:05:28 -050049 \return zero on success (For now, we always return zero, so check the stream instead.)
Christopher Dunnc41609b2015-02-09 18:44:53 -060050 \throw std::exception possibly, depending on configuration
51 */
Christopher Dunn724ba292016-03-06 11:47:35 -060052 virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060053
Christopher Dunn177b7b82015-01-26 10:35:54 -060054 /** \brief A simple abstract factory.
55 */
56 class JSON_API Factory {
57 public:
58 virtual ~Factory();
Christopher Dunna9e1ab32015-02-09 17:22:28 -060059 /** \brief Allocate a CharReader via operator new().
Christopher Dunna9e1ab32015-02-09 17:22:28 -060060 * \throw std::exception if something goes wrong (e.g. invalid settings)
61 */
Christopher Dunnc41609b2015-02-09 18:44:53 -060062 virtual StreamWriter* newStreamWriter() const = 0;
Christopher Dunn6065a1c2015-01-26 11:01:15 -060063 }; // Factory
64}; // StreamWriter
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060065
Christopher Dunn694dbcb2015-02-09 15:25:57 -060066/** \brief Write into stringstream, then return string, for convenience.
67 * A StreamWriter will be created from the factory, used, and then deleted.
68 */
Christopher Dawes75570d72016-03-07 08:29:59 +000069JSONCPP_STRING JSON_API writeString(StreamWriter::Factory const& factory, Value const& root);
Christopher Dunn6065a1c2015-01-26 11:01:15 -060070
71
72/** \brief Build a StreamWriter implementation.
Christopher Dunn999f5912015-01-26 11:12:53 -060073
74Usage:
75\code
76 using namespace Json;
77 Value value = ...;
Christopher Dunn38042b32015-01-26 11:23:31 -060078 StreamWriterBuilder builder;
Christopher Dunn37dde9d2015-03-04 15:04:19 -060079 builder["commentStyle"] = "None";
80 builder["indentation"] = " "; // or whatever you like
Christopher Dunna9e1ab32015-02-09 17:22:28 -060081 std::unique_ptr<Json::StreamWriter> writer(
Christopher Dunnc41609b2015-02-09 18:44:53 -060082 builder.newStreamWriter());
83 writer->write(value, &std::cout);
Christopher Dunn999f5912015-01-26 11:12:53 -060084 std::cout << std::endl; // add lf and flush
85\endcode
86*/
Christopher Dunn6065a1c2015-01-26 11:01:15 -060087class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
Christopher Dunn6065a1c2015-01-26 11:01:15 -060088public:
Christopher Dunna9e1ab32015-02-09 17:22:28 -060089 // 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 Dunn5a744702015-02-10 21:15:43 -060093 - "commentStyle": "None" or "All"
Christopher Dunnf757c182015-02-09 18:24:56 -060094 - "indentation": "<anything>"
Christopher Dunn29501c42015-02-10 23:03:27 -060095 - "enableYAMLCompatibility": false or true
Christopher Dunn5a744702015-02-10 21:15:43 -060096 - slightly change the whitespace around colons
Christopher Dunn29501c42015-02-10 23:03:27 -060097 - "dropNullPlaceholders": false or true
Christopher Dunn5a744702015-02-10 21:15:43 -060098 - 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.
drgler20845632015-09-03 22:19:22 +0200102 - "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 Dunnf757c182015-02-09 18:24:56 -0600106
107 You can examine 'settings_` yourself
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600108 to see the defaults. You can also write and read them just like any
109 JSON Value.
Christopher Dunnf757c182015-02-09 18:24:56 -0600110 \sa setDefaults()
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600111 */
112 Json::Value settings_;
Christopher Dunn7eca3b42015-01-26 11:17:42 -0600113
114 StreamWriterBuilder();
Christopher Dunn98e981d2016-03-21 21:00:24 -0500115 ~StreamWriterBuilder() JSONCPP_OVERRIDE;
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600116
Christopher Dunnc41609b2015-02-09 18:44:53 -0600117 /**
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600118 * \throw std::exception if something goes wrong (e.g. invalid settings)
119 */
Christopher Dunn98e981d2016-03-21 21:00:24 -0500120 StreamWriter* newStreamWriter() const JSONCPP_OVERRIDE;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600121
Christopher Dunnf757c182015-02-09 18:24:56 -0600122 /** \return true if 'settings' are legal and consistent;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600123 * otherwise, indicate bad settings via 'invalid'.
124 */
125 bool validate(Json::Value* invalid) const;
Christopher Dunnc312dd52015-03-04 14:56:37 -0600126 /** A simple way to update a specific setting.
127 */
Christopher Dawes75570d72016-03-07 08:29:59 +0000128 Value& operator[](JSONCPP_STRING key);
Christopher Dunnc312dd52015-03-04 14:56:37 -0600129
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600130 /** Called by ctor, but you can use this to reset settings_.
131 * \pre 'settings' != NULL (but Json::null is fine)
Christopher Dunn3cf91752015-02-09 18:16:24 -0600132 * \remark Defaults:
133 * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600134 */
135 static void setDefaults(Json::Value* settings);
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600136};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600137
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000138/** \brief Abstract class for writers.
Christopher Dunned495ed2015-03-08 14:01:28 -0500139 * \deprecated Use StreamWriter. (And really, this is an implementation detail.)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140 */
141class JSON_API Writer {
142public:
143 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000144
Christopher Dawes75570d72016-03-07 08:29:59 +0000145 virtual JSONCPP_STRING write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000146};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000147
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000148/** \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 Dunn20d09672015-02-10 21:29:35 -0600155 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000156 */
157class JSON_API FastWriter : public Writer {
Christopher Dunned495ed2015-03-08 14:01:28 -0500158
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159public:
160 FastWriter();
Christopher Dunn98e981d2016-03-21 21:00:24 -0500161 ~FastWriter() JSONCPP_OVERRIDE {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000162
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000163 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000164
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165 /** \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 Jacobsae3c7a72012-03-12 04:53:57 +0000171
Don Milham5bf16102014-09-02 17:09:07 -0600172 void omitEndingLineFeed();
173
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174public: // overridden from Writer
Christopher Dunn98e981d2016-03-21 21:00:24 -0500175 JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000176
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000178 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000179
Christopher Dawes75570d72016-03-07 08:29:59 +0000180 JSONCPP_STRING document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 bool yamlCompatiblityEnabled_;
182 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600183 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000184};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000185
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186/** \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 Dunn38042b32015-01-26 11:23:31 -0600208 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209 */
210class JSON_API StyledWriter : public Writer {
211public:
212 StyledWriter();
Christopher Dunn98e981d2016-03-21 21:00:24 -0500213 ~StyledWriter() JSONCPP_OVERRIDE {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000214
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000215public: // 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 Dunn98e981d2016-03-21 21:00:24 -0500220 JSONCPP_STRING write(const Value& root) JSONCPP_OVERRIDE;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000221
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000223 void writeValue(const Value& value);
224 void writeArrayValue(const Value& value);
225 bool isMultineArray(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000226 void pushValue(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227 void writeIndent();
Christopher Dawes75570d72016-03-07 08:29:59 +0000228 void writeWithIndent(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000229 void indent();
230 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000231 void writeCommentBeforeValue(const Value& root);
232 void writeCommentAfterValueOnSameLine(const Value& root);
233 bool hasCommentForValue(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000234 static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000235
Christopher Dawes75570d72016-03-07 08:29:59 +0000236 typedef std::vector<JSONCPP_STRING> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000237
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000238 ChildValues childValues_;
Christopher Dawes75570d72016-03-07 08:29:59 +0000239 JSONCPP_STRING document_;
240 JSONCPP_STRING indentString_;
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600241 unsigned int rightMargin_;
242 unsigned int indentSize_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 bool addChildValues_;
244};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000245
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246/** \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 Dunn38042b32015-01-26 11:23:31 -0600270 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271 */
272class JSON_API StyledStreamWriter {
273public:
Christopher Dawes75570d72016-03-07 08:29:59 +0000274 StyledStreamWriter(JSONCPP_STRING indentation = "\t");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000275 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000276
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277public:
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 Dunn724ba292016-03-06 11:47:35 -0600284 void write(JSONCPP_OSTREAM& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000285
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000286private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000287 void writeValue(const Value& value);
288 void writeArrayValue(const Value& value);
289 bool isMultineArray(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000290 void pushValue(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000291 void writeIndent();
Christopher Dawes75570d72016-03-07 08:29:59 +0000292 void writeWithIndent(const JSONCPP_STRING& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293 void indent();
294 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000295 void writeCommentBeforeValue(const Value& root);
296 void writeCommentAfterValueOnSameLine(const Value& root);
297 bool hasCommentForValue(const Value& value);
Christopher Dawes75570d72016-03-07 08:29:59 +0000298 static JSONCPP_STRING normalizeEOL(const JSONCPP_STRING& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000299
Christopher Dawes75570d72016-03-07 08:29:59 +0000300 typedef std::vector<JSONCPP_STRING> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000301
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000302 ChildValues childValues_;
Christopher Dunn724ba292016-03-06 11:47:35 -0600303 JSONCPP_OSTREAM* document_;
Christopher Dawes75570d72016-03-07 08:29:59 +0000304 JSONCPP_STRING indentString_;
Christopher Dunnfef4b752016-02-06 09:59:18 -0600305 unsigned int rightMargin_;
Christopher Dawes75570d72016-03-07 08:29:59 +0000306 JSONCPP_STRING indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600307 bool addChildValues_ : 1;
308 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000309};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000310
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000311#if defined(JSON_HAS_INT64)
Christopher Dawes75570d72016-03-07 08:29:59 +0000312JSONCPP_STRING JSON_API valueToString(Int value);
313JSONCPP_STRING JSON_API valueToString(UInt value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000314#endif // if defined(JSON_HAS_INT64)
Christopher Dawes75570d72016-03-07 08:29:59 +0000315JSONCPP_STRING JSON_API valueToString(LargestInt value);
316JSONCPP_STRING JSON_API valueToString(LargestUInt value);
317JSONCPP_STRING JSON_API valueToString(double value);
318JSONCPP_STRING JSON_API valueToString(bool value);
319JSONCPP_STRING JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000320
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321/// \brief Output using the StyledStreamWriter.
322/// \see Json::operator>>()
Christopher Dunn724ba292016-03-06 11:47:35 -0600323JSON_API JSONCPP_OSTREAM& operator<<(JSONCPP_OSTREAM&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000324
325} // namespace Json
326
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000327#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000328#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000329#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
330
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000331#endif // JSON_WRITER_H_INCLUDED