blob: 4b8a89ccccd46e2fd8be4fbc20b5a94cd171b3ac [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 Dunn38042b32015-01-26 11:23:31 -060034 factory.newStreamWriter(&std::cout));
Christopher Dunn999f5912015-01-26 11:12:53 -060035 writer->write(value);
36 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:
42 std::ostream& sout_; // not owned; will not delete
43public:
Christopher Dunn198cc352015-01-29 12:57:02 -060044 /// Scoped enums are not available until C++11.
45 struct CommentStyle {
46 /// Decide whether to write comments.
47 enum Enum {
48 None, ///< Drop all comments.
49 Most, ///< Recover odd behavior of previous versions (not implemented yet).
50 All ///< Keep all comments.
51 };
Christopher Dunn9da9f842015-01-25 19:20:43 -060052 };
Christopher Dunn489707f2015-01-22 15:25:30 -060053
Christopher Dunnfe3979c2015-01-24 13:54:28 -060054 /// Keep a reference, but do not take ownership of `sout`.
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060055 StreamWriter(std::ostream* sout);
56 virtual ~StreamWriter();
57 /// Write Value into document as configured in sub-class.
58 /// \return zero on success
59 /// \throw std::exception possibly, depending on configuration
Christopher Dunnbeb6f352015-01-23 07:51:40 -060060 virtual int write(Value const& root) = 0;
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060061
Christopher Dunn177b7b82015-01-26 10:35:54 -060062 /** \brief A simple abstract factory.
63 */
64 class JSON_API Factory {
65 public:
66 virtual ~Factory();
Christopher Dunn177b7b82015-01-26 10:35:54 -060067 /// Do not take ownership of sout, but maintain a reference.
68 virtual StreamWriter* newStreamWriter(std::ostream* sout) const = 0;
Christopher Dunn6065a1c2015-01-26 11:01:15 -060069 }; // Factory
70}; // StreamWriter
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060071
72/// \brief Write into stringstream, then return string, for convenience.
Christopher Dunn6065a1c2015-01-26 11:01:15 -060073std::string writeString(Value const& root, StreamWriter::Factory const& factory);
74
75
76/** \brief Build a StreamWriter implementation.
Christopher Dunn999f5912015-01-26 11:12:53 -060077
78Usage:
79\code
80 using namespace Json;
81 Value value = ...;
Christopher Dunn38042b32015-01-26 11:23:31 -060082 StreamWriterBuilder builder;
Christopher Dunn999f5912015-01-26 11:12:53 -060083 builder.cs_ = StreamWriter::CommentStyle::None;
84 std::shared_ptr<StreamWriter> writer(
85 builder.newStreamWriter(&std::cout));
86 writer->write(value);
87 std::cout << std::endl; // add lf and flush
88\endcode
89*/
Christopher Dunn6065a1c2015-01-26 11:01:15 -060090class JSON_API StreamWriterBuilder : public StreamWriter::Factory {
Christopher Dunn6065a1c2015-01-26 11:01:15 -060091public:
92 // Note: We cannot add data-members to this class without a major version bump.
93 // So these might as well be completely exposed.
94
95 /** \brief How to write comments.
96 * Default: All
97 */
Christopher Dunn198cc352015-01-29 12:57:02 -060098 StreamWriter::CommentStyle::Enum cs_;
Christopher Dunn6065a1c2015-01-26 11:01:15 -060099 /** \brief Write in human-friendly style.
100
101 If "", then skip all indentation and newlines.
102 In that case, you probably want CommentStyle::None also.
103 Default: "\t"
104 */
Christopher Dunn7eca3b42015-01-26 11:17:42 -0600105 std::string indentation_;
106
107 StreamWriterBuilder();
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600108
109 /// Do not take ownership of sout, but maintain a reference.
110 StreamWriter* newStreamWriter(std::ostream* sout) const;
111};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600112
Christopher Dunn177b7b82015-01-26 10:35:54 -0600113/** \brief Build a StreamWriter implementation.
114 * Comments are not written, and most whitespace is omitted.
115 * In addition, there are some special settings to allow compatibility
116 * with the old FastWriter.
117 * Usage:
118 * \code
119 * OldCompressingStreamWriterBuilder b;
120 * b.dropNullPlaceHolders_ = true; // etc.
121 * StreamWriter* w = b.newStreamWriter(&std::cout);
122 * w.write(value);
123 * delete w;
124 * \endcode
125 */
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600126class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
Christopher Dunn177b7b82015-01-26 10:35:54 -0600127{
128public:
129 // Note: We cannot add data-members to this class without a major version bump.
130 // So these might as well be completely exposed.
131
132 /** \brief Drop the "null" string from the writer's output for nullValues.
133 * Strictly speaking, this is not valid JSON. But when the output is being
134 * fed to a browser's Javascript, it makes for smaller output and the
135 * browser can handle the output just fine.
136 */
137 bool dropNullPlaceholders_;
138 /** \brief Do not add \n at end of document.
139 * Normally, we add an extra newline, just because.
140 */
141 bool omitEndingLineFeed_;
142 /** \brief Add a space after ':'.
143 * If indentation is non-empty, we surround colon with whitespace,
144 * e.g. " : "
145 * This will add back the trailing space when there is no indentation.
146 * This seems dubious when the entire document is on a single line,
147 * but we leave this here to repduce the behavior of the old `FastWriter`.
148 */
149 bool enableYAMLCompatibility_;
150
151 OldCompressingStreamWriterBuilder()
152 : dropNullPlaceholders_(false)
153 , omitEndingLineFeed_(false)
154 , enableYAMLCompatibility_(false)
155 {}
156 virtual StreamWriter* newStreamWriter(std::ostream*) const;
157};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600158
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159/** \brief Abstract class for writers.
Christopher Dunn38042b32015-01-26 11:23:31 -0600160 * \deprecated Use StreamWriter.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 */
162class JSON_API Writer {
163public:
164 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000165
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000166 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000167};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000168
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000169/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
170 *without formatting (not human friendly).
171 *
172 * The JSON document is written in a single line. It is not intended for 'human'
173 *consumption,
174 * but may be usefull to support feature such as RPC where bandwith is limited.
175 * \sa Reader, Value
Christopher Dunn38042b32015-01-26 11:23:31 -0600176 * \deprecated Use OldCompressingStreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177 */
178class JSON_API FastWriter : public Writer {
179public:
180 FastWriter();
181 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000182
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000184
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000185 /** \brief Drop the "null" string from the writer's output for nullValues.
186 * Strictly speaking, this is not valid JSON. But when the output is being
187 * fed to a browser's Javascript, it makes for smaller output and the
188 * browser can handle the output just fine.
189 */
190 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000191
Don Milham5bf16102014-09-02 17:09:07 -0600192 void omitEndingLineFeed();
193
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000195 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000196
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000198 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000199
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200 std::string document_;
201 bool yamlCompatiblityEnabled_;
202 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600203 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000205
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000206/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
207 *human friendly way.
208 *
209 * The rules for line break and indent are as follow:
210 * - Object value:
211 * - if empty then print {} without indent and line break
212 * - if not empty the print '{', line break & indent, print one value per
213 *line
214 * and then unindent and line break and print '}'.
215 * - Array value:
216 * - if empty then print [] without indent and line break
217 * - if the array contains no object value, empty array or some other value
218 *types,
219 * and all the values fit on one lines, then print the array on a single
220 *line.
221 * - otherwise, it the values do not fit on one line, or the array contains
222 * object or non empty array, then print one value per line.
223 *
224 * If the Value have comments then they are outputed according to their
225 *#CommentPlacement.
226 *
227 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600228 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000229 */
230class JSON_API StyledWriter : public Writer {
231public:
232 StyledWriter();
233 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000234
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235public: // overridden from Writer
236 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
237 * \param root Value to serialize.
238 * \return String containing the JSON document that represents the root value.
239 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000240 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000241
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000243 void writeValue(const Value& value);
244 void writeArrayValue(const Value& value);
245 bool isMultineArray(const Value& value);
246 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000247 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000248 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000249 void indent();
250 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000251 void writeCommentBeforeValue(const Value& root);
252 void writeCommentAfterValueOnSameLine(const Value& root);
253 bool hasCommentForValue(const Value& value);
254 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000255
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000256 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000257
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000258 ChildValues childValues_;
259 std::string document_;
260 std::string indentString_;
261 int rightMargin_;
262 int indentSize_;
263 bool addChildValues_;
264};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000265
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000266/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
267 human friendly way,
268 to a stream rather than to a string.
269 *
270 * The rules for line break and indent are as follow:
271 * - Object value:
272 * - if empty then print {} without indent and line break
273 * - if not empty the print '{', line break & indent, print one value per
274 line
275 * and then unindent and line break and print '}'.
276 * - Array value:
277 * - if empty then print [] without indent and line break
278 * - if the array contains no object value, empty array or some other value
279 types,
280 * and all the values fit on one lines, then print the array on a single
281 line.
282 * - otherwise, it the values do not fit on one line, or the array contains
283 * object or non empty array, then print one value per line.
284 *
285 * If the Value have comments then they are outputed according to their
286 #CommentPlacement.
287 *
288 * \param indentation Each level will be indented by this amount extra.
289 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600290 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000291 */
292class JSON_API StyledStreamWriter {
293public:
294 StyledStreamWriter(std::string indentation = "\t");
295 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000296
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000297public:
298 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
299 * \param out Stream to write to. (Can be ostringstream, e.g.)
300 * \param root Value to serialize.
301 * \note There is no point in deriving from Writer, since write() should not
302 * return a value.
303 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000304 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000305
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000306private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000307 void writeValue(const Value& value);
308 void writeArrayValue(const Value& value);
309 bool isMultineArray(const Value& value);
310 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000311 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000312 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000313 void indent();
314 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000315 void writeCommentBeforeValue(const Value& root);
316 void writeCommentAfterValueOnSameLine(const Value& root);
317 bool hasCommentForValue(const Value& value);
318 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000319
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000320 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000321
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000322 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000323 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000324 std::string indentString_;
325 int rightMargin_;
326 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600327 bool addChildValues_ : 1;
328 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000329};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000330
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000331#if defined(JSON_HAS_INT64)
332std::string JSON_API valueToString(Int value);
333std::string JSON_API valueToString(UInt value);
334#endif // if defined(JSON_HAS_INT64)
335std::string JSON_API valueToString(LargestInt value);
336std::string JSON_API valueToString(LargestUInt value);
337std::string JSON_API valueToString(double value);
338std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000339std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000340
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000341/// \brief Output using the StyledStreamWriter.
342/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000343JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000344
345} // namespace Json
346
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000347#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000348#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000349#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
350
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000351#endif // JSON_WRITER_H_INCLUDED