blob: fb824e335fc9559fe7b7251739662bbf5d15fa1c [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 Dunn6d135cb2007-06-13 15:51:04 +000014
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100015// Disable warning C4251: <data member>: <type> needs to have dll-interface to
16// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000017#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100018#pragma warning(push)
19#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000020#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
21
Christopher Dunn6d135cb2007-06-13 15:51:04 +000022namespace Json {
23
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100024class Value;
Christopher Dunn489707f2015-01-22 15:25:30 -060025class StreamWriterBuilder;
26
27/**
28
29Usage:
30
31 using namespace Json;
32 Value value;
Christopher Dunnfe3979c2015-01-24 13:54:28 -060033 StreamWriter::Builder builder;
Christopher Dunnc7b39c22015-01-25 18:45:59 -060034 builder.withCommentStyle(StreamWriter::CommentStyle::None);
Christopher Dunn77ce0572015-01-23 07:11:06 -060035 std::shared_ptr<StreamWriter> writer(
36 builder.newStreamWriter(&std::cout));
37 writer->write(value);
38 std::cout.flush();
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 Dunn648843d2015-01-24 13:57:29 -060044 /// `All`: Keep all comments.
45 /// `None`: Drop all comments.
46 /// Use `Most` to recover the odd behavior of previous versions.
47 /// Only `All` is currently implemented.
48 enum class CommentStyle {None, Most, All};
Christopher Dunn489707f2015-01-22 15:25:30 -060049
Christopher Dunnfe3979c2015-01-24 13:54:28 -060050 /// Keep a reference, but do not take ownership of `sout`.
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060051 StreamWriter(std::ostream* sout);
52 virtual ~StreamWriter();
53 /// Write Value into document as configured in sub-class.
54 /// \return zero on success
55 /// \throw std::exception possibly, depending on configuration
Christopher Dunnbeb6f352015-01-23 07:51:40 -060056 virtual int write(Value const& root) = 0;
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060057
Christopher Dunn489707f2015-01-22 15:25:30 -060058 /// Because this Builder is non-virtual, we can safely add
59 /// methods without a major version bump.
60 /// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
61 class Builder {
62 StreamWriterBuilder* own_;
Christopher Dunnfe3979c2015-01-24 13:54:28 -060063 Builder(Builder const&); // noncopyable
64 void operator=(Builder const&); // noncopyable
Christopher Dunn489707f2015-01-22 15:25:30 -060065 public:
Christopher Dunnfe3979c2015-01-24 13:54:28 -060066 Builder();
Christopher Dunn489707f2015-01-22 15:25:30 -060067 ~Builder(); // delete underlying StreamWriterBuilder
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060068
Christopher Dunnc7b39c22015-01-25 18:45:59 -060069 Builder& withCommentStyle(CommentStyle cs); /// default: All
Christopher Dunn4d649402015-01-22 16:08:21 -060070 /** \brief Write in human-friendly style.
71
72 If "", then skip all indentation, newlines, and comments,
73 which implies CommentStyle::None.
74 Default: "\t"
75 */
Christopher Dunnc7b39c22015-01-25 18:45:59 -060076 Builder& withIndentation(std::string indentation);
Christopher Dunnd78caa32015-01-25 18:15:54 -060077 /** \brief Drop the "null" string from the writer's output for nullValues.
78 * Strictly speaking, this is not valid JSON. But when the output is being
79 * fed to a browser's Javascript, it makes for smaller output and the
80 * browser can handle the output just fine.
81 */
Christopher Dunnc7b39c22015-01-25 18:45:59 -060082 Builder& withDropNullPlaceholders(bool v);
Christopher Dunnd78caa32015-01-25 18:15:54 -060083 /** \brief Do not add \n at end of document.
84 * Normally, we add an extra newline, just because.
85 */
Christopher Dunnc7b39c22015-01-25 18:45:59 -060086 Builder& withOmitEndingLineFeed(bool v);
Christopher Dunnd78caa32015-01-25 18:15:54 -060087 /** \brief Add a space after ':'.
88 * If indentation is non-empty, we surround colon with whitespace,
89 * e.g. " : "
90 * This will add back the trailing space when there is no indentation.
91 * This seems dubious when the entire document is on a single line,
92 * but we leave this here to repduce the behavior of the old `FastWriter`.
93 */
Christopher Dunnc7b39c22015-01-25 18:45:59 -060094 Builder& withEnableYAMLCompatibility(bool v);
Christopher Dunn489707f2015-01-22 15:25:30 -060095
96 /// Do not take ownership of sout, but maintain a reference.
Christopher Dunn9243d602015-01-23 08:38:32 -060097 StreamWriter* newStreamWriter(std::ostream* sout) const;
Christopher Dunn489707f2015-01-22 15:25:30 -060098 };
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060099};
100
101/// \brief Write into stringstream, then return string, for convenience.
Christopher Dunn9243d602015-01-23 08:38:32 -0600102std::string writeString(Value const& root, StreamWriter::Builder const& builder);
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -0600103
104
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000105/** \brief Abstract class for writers.
Christopher Dunnc7b39c22015-01-25 18:45:59 -0600106 * \deprecated Use StreamWriter::Builder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000107 */
108class JSON_API Writer {
109public:
110 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000111
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000112 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000113};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000114
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000115/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
116 *without formatting (not human friendly).
117 *
118 * The JSON document is written in a single line. It is not intended for 'human'
119 *consumption,
120 * but may be usefull to support feature such as RPC where bandwith is limited.
121 * \sa Reader, Value
Christopher Dunnc7b39c22015-01-25 18:45:59 -0600122 * \deprecated Use StreamWriter::Builder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000123 */
124class JSON_API FastWriter : public Writer {
125public:
126 FastWriter();
127 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000128
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000129 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000130
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000131 /** \brief Drop the "null" string from the writer's output for nullValues.
132 * Strictly speaking, this is not valid JSON. But when the output is being
133 * fed to a browser's Javascript, it makes for smaller output and the
134 * browser can handle the output just fine.
135 */
136 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000137
Don Milham5bf16102014-09-02 17:09:07 -0600138 void omitEndingLineFeed();
139
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000141 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000142
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000143private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000144 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000145
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000146 std::string document_;
147 bool yamlCompatiblityEnabled_;
148 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600149 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000150};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000151
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000152/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
153 *human friendly way.
154 *
155 * The rules for line break and indent are as follow:
156 * - Object value:
157 * - if empty then print {} without indent and line break
158 * - if not empty the print '{', line break & indent, print one value per
159 *line
160 * and then unindent and line break and print '}'.
161 * - Array value:
162 * - if empty then print [] without indent and line break
163 * - if the array contains no object value, empty array or some other value
164 *types,
165 * and all the values fit on one lines, then print the array on a single
166 *line.
167 * - otherwise, it the values do not fit on one line, or the array contains
168 * object or non empty array, then print one value per line.
169 *
170 * If the Value have comments then they are outputed according to their
171 *#CommentPlacement.
172 *
173 * \sa Reader, Value, Value::setComment()
Christopher Dunnc7b39c22015-01-25 18:45:59 -0600174 * \deprecated Use StreamWriter::Builder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 */
176class JSON_API StyledWriter : public Writer {
177public:
178 StyledWriter();
179 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000180
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181public: // overridden from Writer
182 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
183 * \param root Value to serialize.
184 * \return String containing the JSON document that represents the root value.
185 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000186 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000187
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000188private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000189 void writeValue(const Value& value);
190 void writeArrayValue(const Value& value);
191 bool isMultineArray(const Value& value);
192 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000193 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000194 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000195 void indent();
196 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000197 void writeCommentBeforeValue(const Value& root);
198 void writeCommentAfterValueOnSameLine(const Value& root);
199 bool hasCommentForValue(const Value& value);
200 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000201
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000203
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204 ChildValues childValues_;
205 std::string document_;
206 std::string indentString_;
207 int rightMargin_;
208 int indentSize_;
209 bool addChildValues_;
210};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000211
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000212/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
213 human friendly way,
214 to a stream rather than to a string.
215 *
216 * The rules for line break and indent are as follow:
217 * - Object value:
218 * - if empty then print {} without indent and line break
219 * - if not empty the print '{', line break & indent, print one value per
220 line
221 * and then unindent and line break and print '}'.
222 * - Array value:
223 * - if empty then print [] without indent and line break
224 * - if the array contains no object value, empty array or some other value
225 types,
226 * and all the values fit on one lines, then print the array on a single
227 line.
228 * - otherwise, it the values do not fit on one line, or the array contains
229 * object or non empty array, then print one value per line.
230 *
231 * If the Value have comments then they are outputed according to their
232 #CommentPlacement.
233 *
234 * \param indentation Each level will be indented by this amount extra.
235 * \sa Reader, Value, Value::setComment()
Christopher Dunnc7b39c22015-01-25 18:45:59 -0600236 * \deprecated Use StreamWriter::Builder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237 */
238class JSON_API StyledStreamWriter {
239public:
240 StyledStreamWriter(std::string indentation = "\t");
241 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000242
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243public:
244 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
245 * \param out Stream to write to. (Can be ostringstream, e.g.)
246 * \param root Value to serialize.
247 * \note There is no point in deriving from Writer, since write() should not
248 * return a value.
249 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000250 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000251
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000252private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000253 void writeValue(const Value& value);
254 void writeArrayValue(const Value& value);
255 bool isMultineArray(const Value& value);
256 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000257 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000258 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000259 void indent();
260 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000261 void writeCommentBeforeValue(const Value& root);
262 void writeCommentAfterValueOnSameLine(const Value& root);
263 bool hasCommentForValue(const Value& value);
264 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000265
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000266 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000267
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000269 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000270 std::string indentString_;
271 int rightMargin_;
272 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600273 bool addChildValues_ : 1;
274 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000275};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000276
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000277#if defined(JSON_HAS_INT64)
278std::string JSON_API valueToString(Int value);
279std::string JSON_API valueToString(UInt value);
280#endif // if defined(JSON_HAS_INT64)
281std::string JSON_API valueToString(LargestInt value);
282std::string JSON_API valueToString(LargestUInt value);
283std::string JSON_API valueToString(double value);
284std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000285std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000286
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000287/// \brief Output using the StyledStreamWriter.
288/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000289JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000290
291} // namespace Json
292
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000293#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000294#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000295#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
296
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000297#endif // JSON_WRITER_H_INCLUDED