blob: f5f0a389ee82b1f758504512e4dd6d6101ffd8ae [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
Christopher Dunn717b0862015-03-08 12:05:28 -050049 \return zero on success (For now, we always return zero, so check the stream instead.)
Christopher Dunnc41609b2015-02-09 18:44:53 -060050 \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 */
Christopher Dunneaa3fd82015-03-05 10:11:43 -060069std::string JSON_API 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 Dunn37dde9d2015-03-04 15:04:19 -060079 builder["commentStyle"] = "None";
80 builder["indentation"] = " "; // or whatever you like
Christopher Dunna9e1ab32015-02-09 17:22:28 -060081 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 Dunn29501c42015-02-10 23:03:27 -060095 - "enableYAMLCompatibility": false or true
Christopher Dunn5a744702015-02-10 21:15:43 -060096 - slightly change the whitespace around colons
Christopher Dunn29501c42015-02-10 23:03:27 -060097 - "dropNullPlaceholders": false or true
Christopher Dunn5a744702015-02-10 21:15:43 -060098 - 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;
Christopher Dunnc312dd52015-03-04 14:56:37 -0600122 /** A simple way to update a specific setting.
123 */
124 Value& operator[](std::string key);
125
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600126 /** Called by ctor, but you can use this to reset settings_.
127 * \pre 'settings' != NULL (but Json::null is fine)
Christopher Dunn3cf91752015-02-09 18:16:24 -0600128 * \remark Defaults:
129 * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
Christopher Dunna9e1ab32015-02-09 17:22:28 -0600130 */
131 static void setDefaults(Json::Value* settings);
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600132};
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600133
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000134/** \brief Abstract class for writers.
Christopher Dunned495ed2015-03-08 14:01:28 -0500135 * \deprecated Use StreamWriter. (And really, this is an implementation detail.)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000136 */
137class JSON_API Writer {
138public:
139 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000140
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000141 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000142};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000143
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000144/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
145 *without formatting (not human friendly).
146 *
147 * The JSON document is written in a single line. It is not intended for 'human'
148 *consumption,
149 * but may be usefull to support feature such as RPC where bandwith is limited.
150 * \sa Reader, Value
Christopher Dunn20d09672015-02-10 21:29:35 -0600151 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000152 */
153class JSON_API FastWriter : public Writer {
Christopher Dunned495ed2015-03-08 14:01:28 -0500154
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000155public:
156 FastWriter();
157 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000158
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000160
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 /** \brief Drop the "null" string from the writer's output for nullValues.
162 * Strictly speaking, this is not valid JSON. But when the output is being
163 * fed to a browser's Javascript, it makes for smaller output and the
164 * browser can handle the output just fine.
165 */
166 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000167
Don Milham5bf16102014-09-02 17:09:07 -0600168 void omitEndingLineFeed();
169
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000170public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000171 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000172
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000173private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000174 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000175
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000176 std::string document_;
177 bool yamlCompatiblityEnabled_;
178 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600179 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000181
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000182/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
183 *human friendly way.
184 *
185 * The rules for line break and indent are as follow:
186 * - Object value:
187 * - if empty then print {} without indent and line break
188 * - if not empty the print '{', line break & indent, print one value per
189 *line
190 * and then unindent and line break and print '}'.
191 * - Array value:
192 * - if empty then print [] without indent and line break
193 * - if the array contains no object value, empty array or some other value
194 *types,
195 * and all the values fit on one lines, then print the array on a single
196 *line.
197 * - otherwise, it the values do not fit on one line, or the array contains
198 * object or non empty array, then print one value per line.
199 *
200 * If the Value have comments then they are outputed according to their
201 *#CommentPlacement.
202 *
203 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600204 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 */
206class JSON_API StyledWriter : public Writer {
207public:
208 StyledWriter();
209 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000210
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211public: // overridden from Writer
212 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
213 * \param root Value to serialize.
214 * \return String containing the JSON document that represents the root value.
215 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000216 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000217
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000219 void writeValue(const Value& value);
220 void writeArrayValue(const Value& value);
221 bool isMultineArray(const Value& value);
222 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000223 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000224 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225 void indent();
226 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000227 void writeCommentBeforeValue(const Value& root);
228 void writeCommentAfterValueOnSameLine(const Value& root);
229 bool hasCommentForValue(const Value& value);
230 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000231
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000232 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000233
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000234 ChildValues childValues_;
235 std::string document_;
236 std::string indentString_;
237 int rightMargin_;
238 int indentSize_;
239 bool addChildValues_;
240};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000241
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
243 human friendly way,
244 to a stream rather than to a string.
245 *
246 * The rules for line break and indent are as follow:
247 * - Object value:
248 * - if empty then print {} without indent and line break
249 * - if not empty the print '{', line break & indent, print one value per
250 line
251 * and then unindent and line break and print '}'.
252 * - Array value:
253 * - if empty then print [] without indent and line break
254 * - if the array contains no object value, empty array or some other value
255 types,
256 * and all the values fit on one lines, then print the array on a single
257 line.
258 * - otherwise, it the values do not fit on one line, or the array contains
259 * object or non empty array, then print one value per line.
260 *
261 * If the Value have comments then they are outputed according to their
262 #CommentPlacement.
263 *
264 * \param indentation Each level will be indented by this amount extra.
265 * \sa Reader, Value, Value::setComment()
Christopher Dunn38042b32015-01-26 11:23:31 -0600266 * \deprecated Use StreamWriterBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000267 */
268class JSON_API StyledStreamWriter {
269public:
270 StyledStreamWriter(std::string indentation = "\t");
271 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000272
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000273public:
274 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
275 * \param out Stream to write to. (Can be ostringstream, e.g.)
276 * \param root Value to serialize.
277 * \note There is no point in deriving from Writer, since write() should not
278 * return a value.
279 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000280 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000281
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000282private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000283 void writeValue(const Value& value);
284 void writeArrayValue(const Value& value);
285 bool isMultineArray(const Value& value);
286 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000287 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000288 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000289 void indent();
290 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000291 void writeCommentBeforeValue(const Value& root);
292 void writeCommentAfterValueOnSameLine(const Value& root);
293 bool hasCommentForValue(const Value& value);
294 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000295
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000297
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000298 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000299 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000300 std::string indentString_;
301 int rightMargin_;
302 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600303 bool addChildValues_ : 1;
304 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000305};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000306
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000307#if defined(JSON_HAS_INT64)
308std::string JSON_API valueToString(Int value);
309std::string JSON_API valueToString(UInt value);
310#endif // if defined(JSON_HAS_INT64)
311std::string JSON_API valueToString(LargestInt value);
312std::string JSON_API valueToString(LargestUInt value);
313std::string JSON_API valueToString(double value);
314std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000315std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000316
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000317/// \brief Output using the StyledStreamWriter.
318/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000319JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000320
321} // namespace Json
322
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000323#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000324#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000325#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
326
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000327#endif // JSON_WRITER_H_INCLUDED