blob: 6e46cf1477c74d06aafa60858c5e3224bb78902f [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 Dunn489707f2015-01-22 15:25:30 -060044 enum class CommentStyle {None, Some, All};
45
Christopher Dunnfe3979c2015-01-24 13:54:28 -060046 /// Keep a reference, but do not take ownership of `sout`.
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060047 StreamWriter(std::ostream* sout);
48 virtual ~StreamWriter();
49 /// Write Value into document as configured in sub-class.
50 /// \return zero on success
51 /// \throw std::exception possibly, depending on configuration
Christopher Dunnbeb6f352015-01-23 07:51:40 -060052 virtual int write(Value const& root) = 0;
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060053
Christopher Dunn489707f2015-01-22 15:25:30 -060054 /// Because this Builder is non-virtual, we can safely add
55 /// methods without a major version bump.
56 /// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
57 class Builder {
58 StreamWriterBuilder* own_;
Christopher Dunnfe3979c2015-01-24 13:54:28 -060059 Builder(Builder const&); // noncopyable
60 void operator=(Builder const&); // noncopyable
Christopher Dunn489707f2015-01-22 15:25:30 -060061 public:
Christopher Dunnfe3979c2015-01-24 13:54:28 -060062 Builder();
Christopher Dunn489707f2015-01-22 15:25:30 -060063 ~Builder(); // delete underlying StreamWriterBuilder
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060064
Christopher Dunn489707f2015-01-22 15:25:30 -060065 void setCommentStyle(CommentStyle cs); /// default: All
Christopher Dunn4d649402015-01-22 16:08:21 -060066 /** \brief Write in human-friendly style.
67
68 If "", then skip all indentation, newlines, and comments,
69 which implies CommentStyle::None.
70 Default: "\t"
71 */
72 void setIndentation(std::string indentation);
Christopher Dunn489707f2015-01-22 15:25:30 -060073
74 /// Do not take ownership of sout, but maintain a reference.
Christopher Dunn9243d602015-01-23 08:38:32 -060075 StreamWriter* newStreamWriter(std::ostream* sout) const;
Christopher Dunn489707f2015-01-22 15:25:30 -060076 };
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060077};
78
79/// \brief Write into stringstream, then return string, for convenience.
Christopher Dunn9243d602015-01-23 08:38:32 -060080std::string writeString(Value const& root, StreamWriter::Builder const& builder);
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060081
82
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100083/** \brief Abstract class for writers.
84 */
85class JSON_API Writer {
86public:
87 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000088
Aaron Jacobs11086dd2014-09-15 10:15:29 +100089 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100090};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000091
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100092/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
93 *without formatting (not human friendly).
94 *
95 * The JSON document is written in a single line. It is not intended for 'human'
96 *consumption,
97 * but may be usefull to support feature such as RPC where bandwith is limited.
98 * \sa Reader, Value
99 */
100class JSON_API FastWriter : public Writer {
101public:
102 FastWriter();
103 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000104
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000105 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000106
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000107 /** \brief Drop the "null" string from the writer's output for nullValues.
108 * Strictly speaking, this is not valid JSON. But when the output is being
109 * fed to a browser's Javascript, it makes for smaller output and the
110 * browser can handle the output just fine.
111 */
112 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000113
Don Milham5bf16102014-09-02 17:09:07 -0600114 void omitEndingLineFeed();
115
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000116public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000117 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000118
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000119private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000120 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000121
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000122 std::string document_;
123 bool yamlCompatiblityEnabled_;
124 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600125 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000126};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000127
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
129 *human friendly way.
130 *
131 * The rules for line break and indent are as follow:
132 * - Object value:
133 * - if empty then print {} without indent and line break
134 * - if not empty the print '{', line break & indent, print one value per
135 *line
136 * and then unindent and line break and print '}'.
137 * - Array value:
138 * - if empty then print [] without indent and line break
139 * - if the array contains no object value, empty array or some other value
140 *types,
141 * and all the values fit on one lines, then print the array on a single
142 *line.
143 * - otherwise, it the values do not fit on one line, or the array contains
144 * object or non empty array, then print one value per line.
145 *
146 * If the Value have comments then they are outputed according to their
147 *#CommentPlacement.
148 *
149 * \sa Reader, Value, Value::setComment()
150 */
151class JSON_API StyledWriter : public Writer {
152public:
153 StyledWriter();
154 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000155
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000156public: // overridden from Writer
157 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
158 * \param root Value to serialize.
159 * \return String containing the JSON document that represents the root value.
160 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000161 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000162
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000163private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000164 void writeValue(const Value& value);
165 void writeArrayValue(const Value& value);
166 bool isMultineArray(const Value& value);
167 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000168 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000169 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000170 void indent();
171 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000172 void writeCommentBeforeValue(const Value& root);
173 void writeCommentAfterValueOnSameLine(const Value& root);
174 bool hasCommentForValue(const Value& value);
175 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000176
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000177 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000178
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 ChildValues childValues_;
180 std::string document_;
181 std::string indentString_;
182 int rightMargin_;
183 int indentSize_;
184 bool addChildValues_;
185};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000186
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000187/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
188 human friendly way,
189 to a stream rather than to a string.
190 *
191 * The rules for line break and indent are as follow:
192 * - Object value:
193 * - if empty then print {} without indent and line break
194 * - if not empty the print '{', line break & indent, print one value per
195 line
196 * and then unindent and line break and print '}'.
197 * - Array value:
198 * - if empty then print [] without indent and line break
199 * - if the array contains no object value, empty array or some other value
200 types,
201 * and all the values fit on one lines, then print the array on a single
202 line.
203 * - otherwise, it the values do not fit on one line, or the array contains
204 * object or non empty array, then print one value per line.
205 *
206 * If the Value have comments then they are outputed according to their
207 #CommentPlacement.
208 *
209 * \param indentation Each level will be indented by this amount extra.
210 * \sa Reader, Value, Value::setComment()
211 */
212class JSON_API StyledStreamWriter {
213public:
214 StyledStreamWriter(std::string indentation = "\t");
215 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000216
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000217public:
218 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
219 * \param out Stream to write to. (Can be ostringstream, e.g.)
220 * \param root Value to serialize.
221 * \note There is no point in deriving from Writer, since write() should not
222 * return a value.
223 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000224 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000225
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000226private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000227 void writeValue(const Value& value);
228 void writeArrayValue(const Value& value);
229 bool isMultineArray(const Value& value);
230 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000231 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000232 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000233 void indent();
234 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000235 void writeCommentBeforeValue(const Value& root);
236 void writeCommentAfterValueOnSameLine(const Value& root);
237 bool hasCommentForValue(const Value& value);
238 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000239
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000240 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000241
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000243 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 std::string indentString_;
245 int rightMargin_;
246 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600247 bool addChildValues_ : 1;
248 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000249};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000250
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000251#if defined(JSON_HAS_INT64)
252std::string JSON_API valueToString(Int value);
253std::string JSON_API valueToString(UInt value);
254#endif // if defined(JSON_HAS_INT64)
255std::string JSON_API valueToString(LargestInt value);
256std::string JSON_API valueToString(LargestUInt value);
257std::string JSON_API valueToString(double value);
258std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000259std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000260
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000261/// \brief Output using the StyledStreamWriter.
262/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000263JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000264
265} // namespace Json
266
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000267#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000268#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000269#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
270
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000271#endif // JSON_WRITER_H_INCLUDED