blob: 871287f141c158a2f16a37188e85dc28fb7ba3f0 [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 Dunn66a8ba22015-02-09 01:29:43 -0600108 virtual ~StreamWriterBuilder();
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600109
110 /// Do not take ownership of sout, but maintain a reference.
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600111 virtual StreamWriter* newStreamWriter(std::ostream* sout) const;
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600112};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600113
Christopher Dunn177b7b82015-01-26 10:35:54 -0600114/** \brief Build a StreamWriter implementation.
115 * Comments are not written, and most whitespace is omitted.
116 * In addition, there are some special settings to allow compatibility
117 * with the old FastWriter.
118 * Usage:
119 * \code
120 * OldCompressingStreamWriterBuilder b;
121 * b.dropNullPlaceHolders_ = true; // etc.
122 * StreamWriter* w = b.newStreamWriter(&std::cout);
123 * w.write(value);
124 * delete w;
125 * \endcode
126 */
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600127class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
Christopher Dunn177b7b82015-01-26 10:35:54 -0600128{
129public:
130 // Note: We cannot add data-members to this class without a major version bump.
131 // So these might as well be completely exposed.
132
133 /** \brief Drop the "null" string from the writer's output for nullValues.
134 * Strictly speaking, this is not valid JSON. But when the output is being
135 * fed to a browser's Javascript, it makes for smaller output and the
136 * browser can handle the output just fine.
137 */
138 bool dropNullPlaceholders_;
139 /** \brief Do not add \n at end of document.
140 * Normally, we add an extra newline, just because.
141 */
142 bool omitEndingLineFeed_;
143 /** \brief Add a space after ':'.
144 * If indentation is non-empty, we surround colon with whitespace,
145 * e.g. " : "
146 * This will add back the trailing space when there is no indentation.
147 * This seems dubious when the entire document is on a single line,
148 * but we leave this here to repduce the behavior of the old `FastWriter`.
149 */
150 bool enableYAMLCompatibility_;
151
152 OldCompressingStreamWriterBuilder()
153 : dropNullPlaceholders_(false)
154 , omitEndingLineFeed_(false)
155 , enableYAMLCompatibility_(false)
156 {}
157 virtual StreamWriter* newStreamWriter(std::ostream*) const;
158};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600159
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160/** \brief Abstract class for writers.
Christopher Dunn38042b32015-01-26 11:23:31 -0600161 * \deprecated Use StreamWriter.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000162 */
163class JSON_API Writer {
164public:
165 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000166
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000167 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000168};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000169
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000170/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
171 *without formatting (not human friendly).
172 *
173 * The JSON document is written in a single line. It is not intended for 'human'
174 *consumption,
175 * but may be usefull to support feature such as RPC where bandwith is limited.
176 * \sa Reader, Value
Christopher Dunn38042b32015-01-26 11:23:31 -0600177 * \deprecated Use OldCompressingStreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178 */
179class JSON_API FastWriter : public Writer {
180public:
181 FastWriter();
182 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000183
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000184 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000185
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186 /** \brief Drop the "null" string from the writer's output for nullValues.
187 * Strictly speaking, this is not valid JSON. But when the output is being
188 * fed to a browser's Javascript, it makes for smaller output and the
189 * browser can handle the output just fine.
190 */
191 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000192
Don Milham5bf16102014-09-02 17:09:07 -0600193 void omitEndingLineFeed();
194
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000195public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000196 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000197
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000198private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000199 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000200
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000201 std::string document_;
202 bool yamlCompatiblityEnabled_;
203 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600204 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000206
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000207/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
208 *human friendly way.
209 *
210 * The rules for line break and indent are as follow:
211 * - Object value:
212 * - if empty then print {} without indent and line break
213 * - if not empty the print '{', line break & indent, print one value per
214 *line
215 * and then unindent and line break and print '}'.
216 * - Array value:
217 * - if empty then print [] without indent and line break
218 * - if the array contains no object value, empty array or some other value
219 *types,
220 * and all the values fit on one lines, then print the array on a single
221 *line.
222 * - otherwise, it the values do not fit on one line, or the array contains
223 * object or non empty array, then print one value per line.
224 *
225 * If the Value have comments then they are outputed according to their
226 *#CommentPlacement.
227 *
228 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600229 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000230 */
231class JSON_API StyledWriter : public Writer {
232public:
233 StyledWriter();
234 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000235
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000236public: // overridden from Writer
237 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
238 * \param root Value to serialize.
239 * \return String containing the JSON document that represents the root value.
240 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000241 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000242
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000244 void writeValue(const Value& value);
245 void writeArrayValue(const Value& value);
246 bool isMultineArray(const Value& value);
247 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000249 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000250 void indent();
251 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000252 void writeCommentBeforeValue(const Value& root);
253 void writeCommentAfterValueOnSameLine(const Value& root);
254 bool hasCommentForValue(const Value& value);
255 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000256
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000257 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000258
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000259 ChildValues childValues_;
260 std::string document_;
261 std::string indentString_;
262 int rightMargin_;
263 int indentSize_;
264 bool addChildValues_;
265};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000266
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000267/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
268 human friendly way,
269 to a stream rather than to a string.
270 *
271 * The rules for line break and indent are as follow:
272 * - Object value:
273 * - if empty then print {} without indent and line break
274 * - if not empty the print '{', line break & indent, print one value per
275 line
276 * and then unindent and line break and print '}'.
277 * - Array value:
278 * - if empty then print [] without indent and line break
279 * - if the array contains no object value, empty array or some other value
280 types,
281 * and all the values fit on one lines, then print the array on a single
282 line.
283 * - otherwise, it the values do not fit on one line, or the array contains
284 * object or non empty array, then print one value per line.
285 *
286 * If the Value have comments then they are outputed according to their
287 #CommentPlacement.
288 *
289 * \param indentation Each level will be indented by this amount extra.
290 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600291 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000292 */
293class JSON_API StyledStreamWriter {
294public:
295 StyledStreamWriter(std::string indentation = "\t");
296 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000297
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000298public:
299 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
300 * \param out Stream to write to. (Can be ostringstream, e.g.)
301 * \param root Value to serialize.
302 * \note There is no point in deriving from Writer, since write() should not
303 * return a value.
304 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000305 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000306
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000307private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000308 void writeValue(const Value& value);
309 void writeArrayValue(const Value& value);
310 bool isMultineArray(const Value& value);
311 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000312 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000313 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000314 void indent();
315 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000316 void writeCommentBeforeValue(const Value& root);
317 void writeCommentAfterValueOnSameLine(const Value& root);
318 bool hasCommentForValue(const Value& value);
319 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000320
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000322
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000324 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325 std::string indentString_;
326 int rightMargin_;
327 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600328 bool addChildValues_ : 1;
329 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000330};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000331
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332#if defined(JSON_HAS_INT64)
333std::string JSON_API valueToString(Int value);
334std::string JSON_API valueToString(UInt value);
335#endif // if defined(JSON_HAS_INT64)
336std::string JSON_API valueToString(LargestInt value);
337std::string JSON_API valueToString(LargestUInt value);
338std::string JSON_API valueToString(double value);
339std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000340std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000341
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000342/// \brief Output using the StyledStreamWriter.
343/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000344JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000345
346} // namespace Json
347
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000348#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000349#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000350#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
351
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000352#endif // JSON_WRITER_H_INCLUDED