blob: 2b081a747303fea2c54b8781e6df61fec91b37ca [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 Dunnf757c182015-02-09 18:24:56 -060093 - "commentStyle": "None", "Some", or "All"
94 - "indentation": "<anything>"
95
96 You can examine 'settings_` yourself
Christopher Dunna9e1ab32015-02-09 17:22:28 -060097 to see the defaults. You can also write and read them just like any
98 JSON Value.
Christopher Dunnf757c182015-02-09 18:24:56 -060099 \sa setDefaults()
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600100 */
101 Json::Value settings_;
Christopher Dunn7eca3b42015-01-26 11:17:42 -0600102
103 StreamWriterBuilder();
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600104 virtual ~StreamWriterBuilder();
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600105
Christopher Dunnc41609b2015-02-09 18:44:53 -0600106 /**
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600107 * \throw std::exception if something goes wrong (e.g. invalid settings)
108 */
Christopher Dunnc41609b2015-02-09 18:44:53 -0600109 virtual StreamWriter* newStreamWriter() const;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600110
Christopher Dunnf757c182015-02-09 18:24:56 -0600111 /** \return true if 'settings' are legal and consistent;
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600112 * otherwise, indicate bad settings via 'invalid'.
113 */
114 bool validate(Json::Value* invalid) const;
115 /** Called by ctor, but you can use this to reset settings_.
116 * \pre 'settings' != NULL (but Json::null is fine)
Christopher Dunn3cf91752015-02-09 18:16:24 -0600117 * \remark Defaults:
118 * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600119 */
120 static void setDefaults(Json::Value* settings);
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600121};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600122
Christopher Dunn177b7b82015-01-26 10:35:54 -0600123/** \brief Build a StreamWriter implementation.
124 * Comments are not written, and most whitespace is omitted.
125 * In addition, there are some special settings to allow compatibility
126 * with the old FastWriter.
127 * Usage:
128 * \code
129 * OldCompressingStreamWriterBuilder b;
130 * b.dropNullPlaceHolders_ = true; // etc.
Christopher Dunnc41609b2015-02-09 18:44:53 -0600131 * StreamWriter* w = b.newStreamWriter();
132 * w->write(value, &std::cout);
Christopher Dunn177b7b82015-01-26 10:35:54 -0600133 * delete w;
134 * \endcode
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600135 *
136 * \deprecated Use StreamWriterBuilder
Christopher Dunn177b7b82015-01-26 10:35:54 -0600137 */
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600138class JSON_API OldCompressingStreamWriterBuilder : public StreamWriter::Factory
Christopher Dunn177b7b82015-01-26 10:35:54 -0600139{
140public:
141 // Note: We cannot add data-members to this class without a major version bump.
142 // So these might as well be completely exposed.
143
144 /** \brief Drop the "null" string from the writer's output for nullValues.
145 * Strictly speaking, this is not valid JSON. But when the output is being
146 * fed to a browser's Javascript, it makes for smaller output and the
147 * browser can handle the output just fine.
148 */
149 bool dropNullPlaceholders_;
150 /** \brief Do not add \n at end of document.
151 * Normally, we add an extra newline, just because.
152 */
153 bool omitEndingLineFeed_;
154 /** \brief Add a space after ':'.
155 * If indentation is non-empty, we surround colon with whitespace,
156 * e.g. " : "
157 * This will add back the trailing space when there is no indentation.
158 * This seems dubious when the entire document is on a single line,
159 * but we leave this here to repduce the behavior of the old `FastWriter`.
160 */
161 bool enableYAMLCompatibility_;
162
163 OldCompressingStreamWriterBuilder()
164 : dropNullPlaceholders_(false)
165 , omitEndingLineFeed_(false)
166 , enableYAMLCompatibility_(false)
167 {}
Christopher Dunnc41609b2015-02-09 18:44:53 -0600168 virtual StreamWriter* newStreamWriter() const;
Christopher Dunn177b7b82015-01-26 10:35:54 -0600169};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600170
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000171/** \brief Abstract class for writers.
Christopher Dunn38042b32015-01-26 11:23:31 -0600172 * \deprecated Use StreamWriter.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000173 */
174class JSON_API Writer {
175public:
176 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000177
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000178 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000180
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
182 *without formatting (not human friendly).
183 *
184 * The JSON document is written in a single line. It is not intended for 'human'
185 *consumption,
186 * but may be usefull to support feature such as RPC where bandwith is limited.
187 * \sa Reader, Value
Christopher Dunn38042b32015-01-26 11:23:31 -0600188 * \deprecated Use OldCompressingStreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 */
190class JSON_API FastWriter : public Writer {
191public:
192 FastWriter();
193 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000194
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000195 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000196
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197 /** \brief Drop the "null" string from the writer's output for nullValues.
198 * Strictly speaking, this is not valid JSON. But when the output is being
199 * fed to a browser's Javascript, it makes for smaller output and the
200 * browser can handle the output just fine.
201 */
202 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000203
Don Milham5bf16102014-09-02 17:09:07 -0600204 void omitEndingLineFeed();
205
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000206public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000207 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000208
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000210 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000211
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000212 std::string document_;
213 bool yamlCompatiblityEnabled_;
214 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600215 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000217
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
219 *human friendly way.
220 *
221 * The rules for line break and indent are as follow:
222 * - Object value:
223 * - if empty then print {} without indent and line break
224 * - if not empty the print '{', line break & indent, print one value per
225 *line
226 * and then unindent and line break and print '}'.
227 * - Array value:
228 * - if empty then print [] without indent and line break
229 * - if the array contains no object value, empty array or some other value
230 *types,
231 * and all the values fit on one lines, then print the array on a single
232 *line.
233 * - otherwise, it the values do not fit on one line, or the array contains
234 * object or non empty array, then print one value per line.
235 *
236 * If the Value have comments then they are outputed according to their
237 *#CommentPlacement.
238 *
239 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600240 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000241 */
242class JSON_API StyledWriter : public Writer {
243public:
244 StyledWriter();
245 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000246
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000247public: // overridden from Writer
248 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
249 * \param root Value to serialize.
250 * \return String containing the JSON document that represents the root value.
251 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000252 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000253
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000254private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000255 void writeValue(const Value& value);
256 void writeArrayValue(const Value& value);
257 bool isMultineArray(const Value& value);
258 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000259 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000260 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261 void indent();
262 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000263 void writeCommentBeforeValue(const Value& root);
264 void writeCommentAfterValueOnSameLine(const Value& root);
265 bool hasCommentForValue(const Value& value);
266 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000267
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000269
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000270 ChildValues childValues_;
271 std::string document_;
272 std::string indentString_;
273 int rightMargin_;
274 int indentSize_;
275 bool addChildValues_;
276};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000277
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000278/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
279 human friendly way,
280 to a stream rather than to a string.
281 *
282 * The rules for line break and indent are as follow:
283 * - Object value:
284 * - if empty then print {} without indent and line break
285 * - if not empty the print '{', line break & indent, print one value per
286 line
287 * and then unindent and line break and print '}'.
288 * - Array value:
289 * - if empty then print [] without indent and line break
290 * - if the array contains no object value, empty array or some other value
291 types,
292 * and all the values fit on one lines, then print the array on a single
293 line.
294 * - otherwise, it the values do not fit on one line, or the array contains
295 * object or non empty array, then print one value per line.
296 *
297 * If the Value have comments then they are outputed according to their
298 #CommentPlacement.
299 *
300 * \param indentation Each level will be indented by this amount extra.
301 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600302 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000303 */
304class JSON_API StyledStreamWriter {
305public:
306 StyledStreamWriter(std::string indentation = "\t");
307 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000308
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000309public:
310 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
311 * \param out Stream to write to. (Can be ostringstream, e.g.)
312 * \param root Value to serialize.
313 * \note There is no point in deriving from Writer, since write() should not
314 * return a value.
315 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000316 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000317
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000318private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000319 void writeValue(const Value& value);
320 void writeArrayValue(const Value& value);
321 bool isMultineArray(const Value& value);
322 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000323 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000324 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000325 void indent();
326 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000327 void writeCommentBeforeValue(const Value& root);
328 void writeCommentAfterValueOnSameLine(const Value& root);
329 bool hasCommentForValue(const Value& value);
330 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000331
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000332 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000333
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000334 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000335 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000336 std::string indentString_;
337 int rightMargin_;
338 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600339 bool addChildValues_ : 1;
340 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000341};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000342
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000343#if defined(JSON_HAS_INT64)
344std::string JSON_API valueToString(Int value);
345std::string JSON_API valueToString(UInt value);
346#endif // if defined(JSON_HAS_INT64)
347std::string JSON_API valueToString(LargestInt value);
348std::string JSON_API valueToString(LargestUInt value);
349std::string JSON_API valueToString(double value);
350std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000351std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000352
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000353/// \brief Output using the StyledStreamWriter.
354/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000355JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000356
357} // namespace Json
358
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000359#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000360#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000361#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
362
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000363#endif // JSON_WRITER_H_INCLUDED