blob: 763a949646aa31629823ea616b6af2aadc5f37f0 [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 Dunn489707f2015-01-22 15:25:30 -060034 builder.setCommentStyle(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 Dunn489707f2015-01-22 15:25:30 -060069 void setCommentStyle(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 */
76 void setIndentation(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 */
82 void setDropNullPlaceholders(bool v);
83 /** \brief Do not add \n at end of document.
84 * Normally, we add an extra newline, just because.
85 */
86 void setOmitEndingLineFeed(bool v);
87 /** \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 */
94 void setEnableYAMLCompatibility(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.
106 */
107class JSON_API Writer {
108public:
109 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000110
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000111 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000112};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000113
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000114/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
115 *without formatting (not human friendly).
116 *
117 * The JSON document is written in a single line. It is not intended for 'human'
118 *consumption,
119 * but may be usefull to support feature such as RPC where bandwith is limited.
120 * \sa Reader, Value
121 */
122class JSON_API FastWriter : public Writer {
123public:
124 FastWriter();
125 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000126
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000127 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000128
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000129 /** \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 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000135
Don Milham5bf16102014-09-02 17:09:07 -0600136 void omitEndingLineFeed();
137
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000138public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000139 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000140
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000141private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000142 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000143
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000144 std::string document_;
145 bool yamlCompatiblityEnabled_;
146 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600147 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000148};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000149
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000150/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
151 *human friendly way.
152 *
153 * The rules for line break and indent are as follow:
154 * - Object value:
155 * - if empty then print {} without indent and line break
156 * - if not empty the print '{', line break & indent, print one value per
157 *line
158 * and then unindent and line break and print '}'.
159 * - Array value:
160 * - if empty then print [] without indent and line break
161 * - if the array contains no object value, empty array or some other value
162 *types,
163 * and all the values fit on one lines, then print the array on a single
164 *line.
165 * - otherwise, it the values do not fit on one line, or the array contains
166 * object or non empty array, then print one value per line.
167 *
168 * If the Value have comments then they are outputed according to their
169 *#CommentPlacement.
170 *
171 * \sa Reader, Value, Value::setComment()
172 */
173class JSON_API StyledWriter : public Writer {
174public:
175 StyledWriter();
176 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000177
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178public: // overridden from Writer
179 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
180 * \param root Value to serialize.
181 * \return String containing the JSON document that represents the root value.
182 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000183 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000184
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000185private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000186 void writeValue(const Value& value);
187 void writeArrayValue(const Value& value);
188 bool isMultineArray(const Value& value);
189 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000190 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000191 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000192 void indent();
193 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000194 void writeCommentBeforeValue(const Value& root);
195 void writeCommentAfterValueOnSameLine(const Value& root);
196 bool hasCommentForValue(const Value& value);
197 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000198
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000199 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000200
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000201 ChildValues childValues_;
202 std::string document_;
203 std::string indentString_;
204 int rightMargin_;
205 int indentSize_;
206 bool addChildValues_;
207};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000208
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
210 human friendly way,
211 to a stream rather than to a string.
212 *
213 * The rules for line break and indent are as follow:
214 * - Object value:
215 * - if empty then print {} without indent and line break
216 * - if not empty the print '{', line break & indent, print one value per
217 line
218 * and then unindent and line break and print '}'.
219 * - Array value:
220 * - if empty then print [] without indent and line break
221 * - if the array contains no object value, empty array or some other value
222 types,
223 * and all the values fit on one lines, then print the array on a single
224 line.
225 * - otherwise, it the values do not fit on one line, or the array contains
226 * object or non empty array, then print one value per line.
227 *
228 * If the Value have comments then they are outputed according to their
229 #CommentPlacement.
230 *
231 * \param indentation Each level will be indented by this amount extra.
232 * \sa Reader, Value, Value::setComment()
233 */
234class JSON_API StyledStreamWriter {
235public:
236 StyledStreamWriter(std::string indentation = "\t");
237 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000238
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239public:
240 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
241 * \param out Stream to write to. (Can be ostringstream, e.g.)
242 * \param root Value to serialize.
243 * \note There is no point in deriving from Writer, since write() should not
244 * return a value.
245 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000246 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000247
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000249 void writeValue(const Value& value);
250 void writeArrayValue(const Value& value);
251 bool isMultineArray(const Value& value);
252 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000254 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255 void indent();
256 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000257 void writeCommentBeforeValue(const Value& root);
258 void writeCommentAfterValueOnSameLine(const Value& root);
259 bool hasCommentForValue(const Value& value);
260 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000261
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000262 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000263
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000264 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000265 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000266 std::string indentString_;
267 int rightMargin_;
268 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600269 bool addChildValues_ : 1;
270 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000271};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000272
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000273#if defined(JSON_HAS_INT64)
274std::string JSON_API valueToString(Int value);
275std::string JSON_API valueToString(UInt value);
276#endif // if defined(JSON_HAS_INT64)
277std::string JSON_API valueToString(LargestInt value);
278std::string JSON_API valueToString(LargestUInt value);
279std::string JSON_API valueToString(double value);
280std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000281std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000282
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000283/// \brief Output using the StyledStreamWriter.
284/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000285JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000286
287} // namespace Json
288
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000289#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000290#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000291#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
292
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000293#endif // JSON_WRITER_H_INCLUDED