blob: bef756cf005e5e50334b8c0439a426668eb53779 [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;
33 StreamWriterBuilderFactory f;
34 StreamWriter::Builder builder(&f);
35 builder.setCommentStyle(StreamWriter::CommentStyle::None);
Christopher Dunn77ce0572015-01-23 07:11:06 -060036 std::shared_ptr<StreamWriter> writer(
37 builder.newStreamWriter(&std::cout));
38 writer->write(value);
39 std::cout.flush();
Christopher Dunn489707f2015-01-22 15:25:30 -060040*/
41class JSON_API StreamWriterBuilderFactory {
42public:
43 virtual ~StreamWriterBuilderFactory();
44 virtual StreamWriterBuilder* newStreamWriterBuilder() const;
45};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000046
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060047class JSON_API StreamWriter {
48protected:
49 std::ostream& sout_; // not owned; will not delete
50public:
Christopher Dunn489707f2015-01-22 15:25:30 -060051 enum class CommentStyle {None, Some, All};
52
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060053 StreamWriter(std::ostream* sout);
54 virtual ~StreamWriter();
55 /// Write Value into document as configured in sub-class.
56 /// \return zero on success
57 /// \throw std::exception possibly, depending on configuration
58 virtual int write(Value const& root) const = 0;
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060059
Christopher Dunn489707f2015-01-22 15:25:30 -060060 /// Because this Builder is non-virtual, we can safely add
61 /// methods without a major version bump.
62 /// \see http://stackoverflow.com/questions/14875052/pure-virtual-functions-and-binary-compatibility
63 class Builder {
64 StreamWriterBuilder* own_;
65 public:
66 Builder(StreamWriterBuilderFactory const*);
67 ~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 Dunn489707f2015-01-22 15:25:30 -060077
78 /// Do not take ownership of sout, but maintain a reference.
79 StreamWriter* newStreamWriter(std::ostream* sout);
80 };
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060081};
82
83/// \brief Write into stringstream, then return string, for convenience.
84std::string writeString(Value const& root, StreamWriterBuilder const& builder);
85
86
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100087/** \brief Abstract class for writers.
88 */
89class JSON_API Writer {
90public:
91 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000092
Aaron Jacobs11086dd2014-09-15 10:15:29 +100093 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100094};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000095
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100096/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
97 *without formatting (not human friendly).
98 *
99 * The JSON document is written in a single line. It is not intended for 'human'
100 *consumption,
101 * but may be usefull to support feature such as RPC where bandwith is limited.
102 * \sa Reader, Value
103 */
104class JSON_API FastWriter : public Writer {
105public:
106 FastWriter();
107 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000108
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000109 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000110
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000111 /** \brief Drop the "null" string from the writer's output for nullValues.
112 * Strictly speaking, this is not valid JSON. But when the output is being
113 * fed to a browser's Javascript, it makes for smaller output and the
114 * browser can handle the output just fine.
115 */
116 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000117
Don Milham5bf16102014-09-02 17:09:07 -0600118 void omitEndingLineFeed();
119
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000120public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000121 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000122
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000123private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000124 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000125
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000126 std::string document_;
127 bool yamlCompatiblityEnabled_;
128 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600129 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000130};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000131
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000132/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
133 *human friendly way.
134 *
135 * The rules for line break and indent are as follow:
136 * - Object value:
137 * - if empty then print {} without indent and line break
138 * - if not empty the print '{', line break & indent, print one value per
139 *line
140 * and then unindent and line break and print '}'.
141 * - Array value:
142 * - if empty then print [] without indent and line break
143 * - if the array contains no object value, empty array or some other value
144 *types,
145 * and all the values fit on one lines, then print the array on a single
146 *line.
147 * - otherwise, it the values do not fit on one line, or the array contains
148 * object or non empty array, then print one value per line.
149 *
150 * If the Value have comments then they are outputed according to their
151 *#CommentPlacement.
152 *
153 * \sa Reader, Value, Value::setComment()
154 */
155class JSON_API StyledWriter : public Writer {
156public:
157 StyledWriter();
158 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000159
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160public: // overridden from Writer
161 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
162 * \param root Value to serialize.
163 * \return String containing the JSON document that represents the root value.
164 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000165 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000166
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000167private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000168 void writeValue(const Value& value);
169 void writeArrayValue(const Value& value);
170 bool isMultineArray(const Value& value);
171 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000172 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000173 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 void indent();
175 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000176 void writeCommentBeforeValue(const Value& root);
177 void writeCommentAfterValueOnSameLine(const Value& root);
178 bool hasCommentForValue(const Value& value);
179 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000180
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000182
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 ChildValues childValues_;
184 std::string document_;
185 std::string indentString_;
186 int rightMargin_;
187 int indentSize_;
188 bool addChildValues_;
189};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000190
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000191/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
192 human friendly way,
193 to a stream rather than to a string.
194 *
195 * The rules for line break and indent are as follow:
196 * - Object value:
197 * - if empty then print {} without indent and line break
198 * - if not empty the print '{', line break & indent, print one value per
199 line
200 * and then unindent and line break and print '}'.
201 * - Array value:
202 * - if empty then print [] without indent and line break
203 * - if the array contains no object value, empty array or some other value
204 types,
205 * and all the values fit on one lines, then print the array on a single
206 line.
207 * - otherwise, it the values do not fit on one line, or the array contains
208 * object or non empty array, then print one value per line.
209 *
210 * If the Value have comments then they are outputed according to their
211 #CommentPlacement.
212 *
213 * \param indentation Each level will be indented by this amount extra.
214 * \sa Reader, Value, Value::setComment()
215 */
216class JSON_API StyledStreamWriter {
217public:
218 StyledStreamWriter(std::string indentation = "\t");
219 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000220
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221public:
222 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
223 * \param out Stream to write to. (Can be ostringstream, e.g.)
224 * \param root Value to serialize.
225 * \note There is no point in deriving from Writer, since write() should not
226 * return a value.
227 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000228 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000229
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000230private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000231 void writeValue(const Value& value);
232 void writeArrayValue(const Value& value);
233 bool isMultineArray(const Value& value);
234 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000236 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237 void indent();
238 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000239 void writeCommentBeforeValue(const Value& root);
240 void writeCommentAfterValueOnSameLine(const Value& root);
241 bool hasCommentForValue(const Value& value);
242 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000243
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000245
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000247 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000248 std::string indentString_;
249 int rightMargin_;
250 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600251 bool addChildValues_ : 1;
252 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000254
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000255#if defined(JSON_HAS_INT64)
256std::string JSON_API valueToString(Int value);
257std::string JSON_API valueToString(UInt value);
258#endif // if defined(JSON_HAS_INT64)
259std::string JSON_API valueToString(LargestInt value);
260std::string JSON_API valueToString(LargestUInt value);
261std::string JSON_API valueToString(double value);
262std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000263std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000264
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000265/// \brief Output using the StyledStreamWriter.
266/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000267JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000268
269} // namespace Json
270
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000271#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000272#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000273#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
274
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000275#endif // JSON_WRITER_H_INCLUDED