blob: c4fd6ed2503689c1048b3a4b5e9b59861f2d3e0c [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 Dunn9da9f842015-01-25 19:20:43 -060044 /// Decide whether to write comments.
45 enum class CommentStyle {
46 None, ///< Drop all comments.
47 Most, ///< Recover odd behavior of previous versions (not implemented yet).
48 All ///< Keep all comments.
49 };
Christopher Dunn489707f2015-01-22 15:25:30 -060050
Christopher Dunnfe3979c2015-01-24 13:54:28 -060051 /// Keep a reference, but do not take ownership of `sout`.
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060052 StreamWriter(std::ostream* sout);
53 virtual ~StreamWriter();
54 /// Write Value into document as configured in sub-class.
55 /// \return zero on success
56 /// \throw std::exception possibly, depending on configuration
Christopher Dunnbeb6f352015-01-23 07:51:40 -060057 virtual int write(Value const& root) = 0;
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060058
Christopher Dunn177b7b82015-01-26 10:35:54 -060059 /** \brief A simple abstract factory.
60 */
61 class JSON_API Factory {
62 public:
63 virtual ~Factory();
Christopher Dunn177b7b82015-01-26 10:35:54 -060064 /// Do not take ownership of sout, but maintain a reference.
65 virtual StreamWriter* newStreamWriter(std::ostream* sout) const = 0;
Christopher Dunn6065a1c2015-01-26 11:01:15 -060066 }; // Factory
67}; // StreamWriter
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060068
69/// \brief Write into stringstream, then return string, for convenience.
Christopher Dunn6065a1c2015-01-26 11:01:15 -060070std::string writeString(Value const& root, StreamWriter::Factory const& factory);
71
72
73/** \brief Build a StreamWriter implementation.
Christopher Dunn999f5912015-01-26 11:12:53 -060074
75Usage:
76\code
77 using namespace Json;
78 Value value = ...;
Christopher Dunn38042b32015-01-26 11:23:31 -060079 StreamWriterBuilder builder;
Christopher Dunn999f5912015-01-26 11:12:53 -060080 builder.cs_ = StreamWriter::CommentStyle::None;
81 std::shared_ptr<StreamWriter> writer(
82 builder.newStreamWriter(&std::cout));
83 writer->write(value);
84 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:
89 // Note: We cannot add data-members to this class without a major version bump.
90 // So these might as well be completely exposed.
91
92 /** \brief How to write comments.
93 * Default: All
94 */
Christopher Dunn7eca3b42015-01-26 11:17:42 -060095 StreamWriter::CommentStyle cs_;
Christopher Dunn6065a1c2015-01-26 11:01:15 -060096 /** \brief Write in human-friendly style.
97
98 If "", then skip all indentation and newlines.
99 In that case, you probably want CommentStyle::None also.
100 Default: "\t"
101 */
Christopher Dunn7eca3b42015-01-26 11:17:42 -0600102 std::string indentation_;
103
104 StreamWriterBuilder();
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600105
106 /// Do not take ownership of sout, but maintain a reference.
107 StreamWriter* newStreamWriter(std::ostream* sout) const;
108};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600109
Christopher Dunn177b7b82015-01-26 10:35:54 -0600110/** \brief Build a StreamWriter implementation.
111 * Comments are not written, and most whitespace is omitted.
112 * In addition, there are some special settings to allow compatibility
113 * with the old FastWriter.
114 * Usage:
115 * \code
116 * OldCompressingStreamWriterBuilder b;
117 * b.dropNullPlaceHolders_ = true; // etc.
118 * StreamWriter* w = b.newStreamWriter(&std::cout);
119 * w.write(value);
120 * delete w;
121 * \endcode
122 */
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600123class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
Christopher Dunn177b7b82015-01-26 10:35:54 -0600124{
125public:
126 // Note: We cannot add data-members to this class without a major version bump.
127 // So these might as well be completely exposed.
128
129 /** \brief Drop the "null" string from the writer's output for nullValues.
130 * Strictly speaking, this is not valid JSON. But when the output is being
131 * fed to a browser's Javascript, it makes for smaller output and the
132 * browser can handle the output just fine.
133 */
134 bool dropNullPlaceholders_;
135 /** \brief Do not add \n at end of document.
136 * Normally, we add an extra newline, just because.
137 */
138 bool omitEndingLineFeed_;
139 /** \brief Add a space after ':'.
140 * If indentation is non-empty, we surround colon with whitespace,
141 * e.g. " : "
142 * This will add back the trailing space when there is no indentation.
143 * This seems dubious when the entire document is on a single line,
144 * but we leave this here to repduce the behavior of the old `FastWriter`.
145 */
146 bool enableYAMLCompatibility_;
147
148 OldCompressingStreamWriterBuilder()
149 : dropNullPlaceholders_(false)
150 , omitEndingLineFeed_(false)
151 , enableYAMLCompatibility_(false)
152 {}
153 virtual StreamWriter* newStreamWriter(std::ostream*) const;
154};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600155
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000156/** \brief Abstract class for writers.
Christopher Dunn38042b32015-01-26 11:23:31 -0600157 * \deprecated Use StreamWriter.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000158 */
159class JSON_API Writer {
160public:
161 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000162
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000163 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000164};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000165
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000166/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
167 *without formatting (not human friendly).
168 *
169 * The JSON document is written in a single line. It is not intended for 'human'
170 *consumption,
171 * but may be usefull to support feature such as RPC where bandwith is limited.
172 * \sa Reader, Value
Christopher Dunn38042b32015-01-26 11:23:31 -0600173 * \deprecated Use OldCompressingStreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 */
175class JSON_API FastWriter : public Writer {
176public:
177 FastWriter();
178 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000179
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000181
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000182 /** \brief Drop the "null" string from the writer's output for nullValues.
183 * Strictly speaking, this is not valid JSON. But when the output is being
184 * fed to a browser's Javascript, it makes for smaller output and the
185 * browser can handle the output just fine.
186 */
187 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000188
Don Milham5bf16102014-09-02 17:09:07 -0600189 void omitEndingLineFeed();
190
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000191public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000192 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000193
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000195 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000196
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197 std::string document_;
198 bool yamlCompatiblityEnabled_;
199 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600200 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000201};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000202
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000203/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
204 *human friendly way.
205 *
206 * The rules for line break and indent are as follow:
207 * - Object value:
208 * - if empty then print {} without indent and line break
209 * - if not empty the print '{', line break & indent, print one value per
210 *line
211 * and then unindent and line break and print '}'.
212 * - Array value:
213 * - if empty then print [] without indent and line break
214 * - if the array contains no object value, empty array or some other value
215 *types,
216 * and all the values fit on one lines, then print the array on a single
217 *line.
218 * - otherwise, it the values do not fit on one line, or the array contains
219 * object or non empty array, then print one value per line.
220 *
221 * If the Value have comments then they are outputed according to their
222 *#CommentPlacement.
223 *
224 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600225 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000226 */
227class JSON_API StyledWriter : public Writer {
228public:
229 StyledWriter();
230 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000231
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000232public: // overridden from Writer
233 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
234 * \param root Value to serialize.
235 * \return String containing the JSON document that represents the root value.
236 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000237 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000238
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000240 void writeValue(const Value& value);
241 void writeArrayValue(const Value& value);
242 bool isMultineArray(const Value& value);
243 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000245 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 void indent();
247 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000248 void writeCommentBeforeValue(const Value& root);
249 void writeCommentAfterValueOnSameLine(const Value& root);
250 bool hasCommentForValue(const Value& value);
251 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000252
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000254
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255 ChildValues childValues_;
256 std::string document_;
257 std::string indentString_;
258 int rightMargin_;
259 int indentSize_;
260 bool addChildValues_;
261};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000262
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
264 human friendly way,
265 to a stream rather than to a string.
266 *
267 * The rules for line break and indent are as follow:
268 * - Object value:
269 * - if empty then print {} without indent and line break
270 * - if not empty the print '{', line break & indent, print one value per
271 line
272 * and then unindent and line break and print '}'.
273 * - Array value:
274 * - if empty then print [] without indent and line break
275 * - if the array contains no object value, empty array or some other value
276 types,
277 * and all the values fit on one lines, then print the array on a single
278 line.
279 * - otherwise, it the values do not fit on one line, or the array contains
280 * object or non empty array, then print one value per line.
281 *
282 * If the Value have comments then they are outputed according to their
283 #CommentPlacement.
284 *
285 * \param indentation Each level will be indented by this amount extra.
286 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600287 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000288 */
289class JSON_API StyledStreamWriter {
290public:
291 StyledStreamWriter(std::string indentation = "\t");
292 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000293
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000294public:
295 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
296 * \param out Stream to write to. (Can be ostringstream, e.g.)
297 * \param root Value to serialize.
298 * \note There is no point in deriving from Writer, since write() should not
299 * return a value.
300 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000301 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000302
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000303private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000304 void writeValue(const Value& value);
305 void writeArrayValue(const Value& value);
306 bool isMultineArray(const Value& value);
307 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000308 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000309 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000310 void indent();
311 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000312 void writeCommentBeforeValue(const Value& root);
313 void writeCommentAfterValueOnSameLine(const Value& root);
314 bool hasCommentForValue(const Value& value);
315 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000316
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000317 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000318
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000319 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000320 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000321 std::string indentString_;
322 int rightMargin_;
323 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600324 bool addChildValues_ : 1;
325 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000326};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000327
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000328#if defined(JSON_HAS_INT64)
329std::string JSON_API valueToString(Int value);
330std::string JSON_API valueToString(UInt value);
331#endif // if defined(JSON_HAS_INT64)
332std::string JSON_API valueToString(LargestInt value);
333std::string JSON_API valueToString(LargestUInt value);
334std::string JSON_API valueToString(double value);
335std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000336std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000337
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000338/// \brief Output using the StyledStreamWriter.
339/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000340JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000341
342} // namespace Json
343
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000344#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000345#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000346#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
347
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000348#endif // JSON_WRITER_H_INCLUDED