blob: 4951ff45adee453780a386507661098d13734c89 [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 Dunnc41609b2015-02-09 18:44:53 -060042 std::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
49 \return zero on success
50 \throw std::exception possibly, depending on configuration
51 */
52 virtual int write(Value const& root, std::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 */
69std::string 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 Dunna9e1ab32015-02-09 17:22:28 -060079 builder.settings_["commentStyle"] = "None";
80 builder.settings_["indentation"] = " "; // or whatever you like
81 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 Dunn5a744702015-02-10 21:15:43 -060095 - "enableYAMLCompatibility": False or True
96 - slightly change the whitespace around colons
97 - "dropNullPlaceholders": False or True
98 - Drop the "null" string from the writer's output for nullValues.
99 Strictly speaking, this is not valid JSON. But when the output is being
100 fed to a browser's Javascript, it makes for smaller output and the
101 browser can handle the output just fine.
Christopher Dunnf757c182015-02-09 18:24:56 -0600102
103 You can examine 'settings_` yourself
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600104 to see the defaults. You can also write and read them just like any
105 JSON Value.
Christopher Dunnf757c182015-02-09 18:24:56 -0600106 \sa setDefaults()
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600107 */
108 Json::Value settings_;
Christopher Dunn7eca3b42015-01-26 11:17:42 -0600109
110 StreamWriterBuilder();
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600111 virtual ~StreamWriterBuilder();
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600112
Christopher Dunnc41609b2015-02-09 18:44:53 -0600113 /**
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600114 * \throw std::exception if something goes wrong (e.g. invalid settings)
115 */
Christopher Dunnc41609b2015-02-09 18:44:53 -0600116 virtual StreamWriter* newStreamWriter() const;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600117
Christopher Dunnf757c182015-02-09 18:24:56 -0600118 /** \return true if 'settings' are legal and consistent;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600119 * otherwise, indicate bad settings via 'invalid'.
120 */
121 bool validate(Json::Value* invalid) const;
122 /** Called by ctor, but you can use this to reset settings_.
123 * \pre 'settings' != NULL (but Json::null is fine)
Christopher Dunn3cf91752015-02-09 18:16:24 -0600124 * \remark Defaults:
125 * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600126 */
127 static void setDefaults(Json::Value* settings);
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600128};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600129
Christopher Dunn177b7b82015-01-26 10:35:54 -0600130/** \brief Build a StreamWriter implementation.
131 * Comments are not written, and most whitespace is omitted.
132 * In addition, there are some special settings to allow compatibility
133 * with the old FastWriter.
134 * Usage:
135 * \code
136 * OldCompressingStreamWriterBuilder b;
137 * b.dropNullPlaceHolders_ = true; // etc.
Christopher Dunnc41609b2015-02-09 18:44:53 -0600138 * StreamWriter* w = b.newStreamWriter();
139 * w->write(value, &std::cout);
Christopher Dunn177b7b82015-01-26 10:35:54 -0600140 * delete w;
141 * \endcode
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600142 *
143 * \deprecated Use StreamWriterBuilder
Christopher Dunn177b7b82015-01-26 10:35:54 -0600144 */
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600145class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
Christopher Dunn177b7b82015-01-26 10:35:54 -0600146{
147public:
148 // Note: We cannot add data-members to this class without a major version bump.
149 // So these might as well be completely exposed.
150
151 /** \brief Drop the "null" string from the writer's output for nullValues.
152 * Strictly speaking, this is not valid JSON. But when the output is being
153 * fed to a browser's Javascript, it makes for smaller output and the
154 * browser can handle the output just fine.
155 */
156 bool dropNullPlaceholders_;
157 /** \brief Do not add \n at end of document.
158 * Normally, we add an extra newline, just because.
159 */
160 bool omitEndingLineFeed_;
161 /** \brief Add a space after ':'.
162 * If indentation is non-empty, we surround colon with whitespace,
163 * e.g. " : "
164 * This will add back the trailing space when there is no indentation.
165 * This seems dubious when the entire document is on a single line,
166 * but we leave this here to repduce the behavior of the old `FastWriter`.
167 */
168 bool enableYAMLCompatibility_;
169
170 OldCompressingStreamWriterBuilder()
171 : dropNullPlaceholders_(false)
172 , omitEndingLineFeed_(false)
173 , enableYAMLCompatibility_(false)
174 {}
Christopher Dunnc41609b2015-02-09 18:44:53 -0600175 virtual StreamWriter* newStreamWriter() const;
Christopher Dunn177b7b82015-01-26 10:35:54 -0600176};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600177
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178/** \brief Abstract class for writers.
Christopher Dunn38042b32015-01-26 11:23:31 -0600179 * \deprecated Use StreamWriter.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 */
181class JSON_API Writer {
182public:
183 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000184
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000185 virtual std::string write(const Value& root) = 0;
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 Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
189 *without formatting (not human friendly).
190 *
191 * The JSON document is written in a single line. It is not intended for 'human'
192 *consumption,
193 * but may be usefull to support feature such as RPC where bandwith is limited.
194 * \sa Reader, Value
Christopher Dunn38042b32015-01-26 11:23:31 -0600195 * \deprecated Use OldCompressingStreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000196 */
197class JSON_API FastWriter : public Writer {
198public:
199 FastWriter();
200 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000201
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000203
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204 /** \brief Drop the "null" string from the writer's output for nullValues.
205 * Strictly speaking, this is not valid JSON. But when the output is being
206 * fed to a browser's Javascript, it makes for smaller output and the
207 * browser can handle the output just fine.
208 */
209 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000210
Don Milham5bf16102014-09-02 17:09:07 -0600211 void omitEndingLineFeed();
212
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000213public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000214 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000215
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000217 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000218
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 std::string document_;
220 bool yamlCompatiblityEnabled_;
221 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600222 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000223};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000224
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
226 *human friendly way.
227 *
228 * The rules for line break and indent are as follow:
229 * - Object value:
230 * - if empty then print {} without indent and line break
231 * - if not empty the print '{', line break & indent, print one value per
232 *line
233 * and then unindent and line break and print '}'.
234 * - Array value:
235 * - if empty then print [] without indent and line break
236 * - if the array contains no object value, empty array or some other value
237 *types,
238 * and all the values fit on one lines, then print the array on a single
239 *line.
240 * - otherwise, it the values do not fit on one line, or the array contains
241 * object or non empty array, then print one value per line.
242 *
243 * If the Value have comments then they are outputed according to their
244 *#CommentPlacement.
245 *
246 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600247 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248 */
249class JSON_API StyledWriter : public Writer {
250public:
251 StyledWriter();
252 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000253
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000254public: // overridden from Writer
255 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
256 * \param root Value to serialize.
257 * \return String containing the JSON document that represents the root value.
258 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000259 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000260
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000262 void writeValue(const Value& value);
263 void writeArrayValue(const Value& value);
264 bool isMultineArray(const Value& value);
265 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000266 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000267 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 void indent();
269 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000270 void writeCommentBeforeValue(const Value& root);
271 void writeCommentAfterValueOnSameLine(const Value& root);
272 bool hasCommentForValue(const Value& value);
273 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000274
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000275 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000276
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277 ChildValues childValues_;
278 std::string document_;
279 std::string indentString_;
280 int rightMargin_;
281 int indentSize_;
282 bool addChildValues_;
283};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000284
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000285/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
286 human friendly way,
287 to a stream rather than to a string.
288 *
289 * The rules for line break and indent are as follow:
290 * - Object value:
291 * - if empty then print {} without indent and line break
292 * - if not empty the print '{', line break & indent, print one value per
293 line
294 * and then unindent and line break and print '}'.
295 * - Array value:
296 * - if empty then print [] without indent and line break
297 * - if the array contains no object value, empty array or some other value
298 types,
299 * and all the values fit on one lines, then print the array on a single
300 line.
301 * - otherwise, it the values do not fit on one line, or the array contains
302 * object or non empty array, then print one value per line.
303 *
304 * If the Value have comments then they are outputed according to their
305 #CommentPlacement.
306 *
307 * \param indentation Each level will be indented by this amount extra.
308 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600309 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000310 */
311class JSON_API StyledStreamWriter {
312public:
313 StyledStreamWriter(std::string indentation = "\t");
314 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000315
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000316public:
317 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
318 * \param out Stream to write to. (Can be ostringstream, e.g.)
319 * \param root Value to serialize.
320 * \note There is no point in deriving from Writer, since write() should not
321 * return a value.
322 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000323 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000324
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000326 void writeValue(const Value& value);
327 void writeArrayValue(const Value& value);
328 bool isMultineArray(const Value& value);
329 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000330 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000331 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332 void indent();
333 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000334 void writeCommentBeforeValue(const Value& root);
335 void writeCommentAfterValueOnSameLine(const Value& root);
336 bool hasCommentForValue(const Value& value);
337 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000338
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000339 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000340
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000341 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000342 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000343 std::string indentString_;
344 int rightMargin_;
345 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600346 bool addChildValues_ : 1;
347 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000348};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000349
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000350#if defined(JSON_HAS_INT64)
351std::string JSON_API valueToString(Int value);
352std::string JSON_API valueToString(UInt value);
353#endif // if defined(JSON_HAS_INT64)
354std::string JSON_API valueToString(LargestInt value);
355std::string JSON_API valueToString(LargestUInt value);
356std::string JSON_API valueToString(double value);
357std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000358std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000359
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000360/// \brief Output using the StyledStreamWriter.
361/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000362JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000363
364} // namespace Json
365
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000366#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000367#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000368#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
369
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000370#endif // JSON_WRITER_H_INCLUDED