blob: 2aff642145122e6a198695a62afedc8d5d51ca32 [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);
36 std::shared_ptr<StreamWriter> writer(builder.newStreamWriter(&std::cout));
37 writer.write(value);
38*/
39class JSON_API StreamWriterBuilderFactory {
40public:
41 virtual ~StreamWriterBuilderFactory();
42 virtual StreamWriterBuilder* newStreamWriterBuilder() const;
43};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000044
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060045class JSON_API StreamWriter {
46protected:
47 std::ostream& sout_; // not owned; will not delete
48public:
Christopher Dunn489707f2015-01-22 15:25:30 -060049 enum class CommentStyle {None, Some, All};
50
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
56 virtual int write(Value const& root) const = 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_;
63 public:
64 Builder(StreamWriterBuilderFactory const*);
65 ~Builder(); // delete underlying StreamWriterBuilder
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060066
Christopher Dunn489707f2015-01-22 15:25:30 -060067 void setCommentStyle(CommentStyle cs); /// default: All
Christopher Dunn4d649402015-01-22 16:08:21 -060068 /** \brief Write in human-friendly style.
69
70 If "", then skip all indentation, newlines, and comments,
71 which implies CommentStyle::None.
72 Default: "\t"
73 */
74 void setIndentation(std::string indentation);
Christopher Dunn489707f2015-01-22 15:25:30 -060075
76 /// Do not take ownership of sout, but maintain a reference.
77 StreamWriter* newStreamWriter(std::ostream* sout);
78 };
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060079};
80
81/// \brief Write into stringstream, then return string, for convenience.
82std::string writeString(Value const& root, StreamWriterBuilder const& builder);
83
84
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100085/** \brief Abstract class for writers.
86 */
87class JSON_API Writer {
88public:
89 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000090
Aaron Jacobs11086dd2014-09-15 10:15:29 +100091 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100092};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000093
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100094/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
95 *without formatting (not human friendly).
96 *
97 * The JSON document is written in a single line. It is not intended for 'human'
98 *consumption,
99 * but may be usefull to support feature such as RPC where bandwith is limited.
100 * \sa Reader, Value
101 */
102class JSON_API FastWriter : public Writer {
103public:
104 FastWriter();
105 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000106
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000107 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000108
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000109 /** \brief Drop the "null" string from the writer's output for nullValues.
110 * Strictly speaking, this is not valid JSON. But when the output is being
111 * fed to a browser's Javascript, it makes for smaller output and the
112 * browser can handle the output just fine.
113 */
114 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000115
Don Milham5bf16102014-09-02 17:09:07 -0600116 void omitEndingLineFeed();
117
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000118public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000119 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000120
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000121private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000122 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000123
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000124 std::string document_;
125 bool yamlCompatiblityEnabled_;
126 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600127 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000129
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000130/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
131 *human friendly way.
132 *
133 * The rules for line break and indent are as follow:
134 * - Object value:
135 * - if empty then print {} without indent and line break
136 * - if not empty the print '{', line break & indent, print one value per
137 *line
138 * and then unindent and line break and print '}'.
139 * - Array value:
140 * - if empty then print [] without indent and line break
141 * - if the array contains no object value, empty array or some other value
142 *types,
143 * and all the values fit on one lines, then print the array on a single
144 *line.
145 * - otherwise, it the values do not fit on one line, or the array contains
146 * object or non empty array, then print one value per line.
147 *
148 * If the Value have comments then they are outputed according to their
149 *#CommentPlacement.
150 *
151 * \sa Reader, Value, Value::setComment()
152 */
153class JSON_API StyledWriter : public Writer {
154public:
155 StyledWriter();
156 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000157
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000158public: // overridden from Writer
159 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
160 * \param root Value to serialize.
161 * \return String containing the JSON document that represents the root value.
162 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000163 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000164
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000166 void writeValue(const Value& value);
167 void writeArrayValue(const Value& value);
168 bool isMultineArray(const Value& value);
169 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000170 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000171 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000172 void indent();
173 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000174 void writeCommentBeforeValue(const Value& root);
175 void writeCommentAfterValueOnSameLine(const Value& root);
176 bool hasCommentForValue(const Value& value);
177 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000178
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000179 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000180
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 ChildValues childValues_;
182 std::string document_;
183 std::string indentString_;
184 int rightMargin_;
185 int indentSize_;
186 bool addChildValues_;
187};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000188
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
190 human friendly way,
191 to a stream rather than to a string.
192 *
193 * The rules for line break and indent are as follow:
194 * - Object value:
195 * - if empty then print {} without indent and line break
196 * - if not empty the print '{', line break & indent, print one value per
197 line
198 * and then unindent and line break and print '}'.
199 * - Array value:
200 * - if empty then print [] without indent and line break
201 * - if the array contains no object value, empty array or some other value
202 types,
203 * and all the values fit on one lines, then print the array on a single
204 line.
205 * - otherwise, it the values do not fit on one line, or the array contains
206 * object or non empty array, then print one value per line.
207 *
208 * If the Value have comments then they are outputed according to their
209 #CommentPlacement.
210 *
211 * \param indentation Each level will be indented by this amount extra.
212 * \sa Reader, Value, Value::setComment()
213 */
214class JSON_API StyledStreamWriter {
215public:
216 StyledStreamWriter(std::string indentation = "\t");
217 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000218
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219public:
220 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
221 * \param out Stream to write to. (Can be ostringstream, e.g.)
222 * \param root Value to serialize.
223 * \note There is no point in deriving from Writer, since write() should not
224 * return a value.
225 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000226 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000227
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000228private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000229 void writeValue(const Value& value);
230 void writeArrayValue(const Value& value);
231 bool isMultineArray(const Value& value);
232 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000233 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000234 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 void indent();
236 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000237 void writeCommentBeforeValue(const Value& root);
238 void writeCommentAfterValueOnSameLine(const Value& root);
239 bool hasCommentForValue(const Value& value);
240 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000241
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000243
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000245 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 std::string indentString_;
247 int rightMargin_;
248 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600249 bool addChildValues_ : 1;
250 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000251};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000252
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253#if defined(JSON_HAS_INT64)
254std::string JSON_API valueToString(Int value);
255std::string JSON_API valueToString(UInt value);
256#endif // if defined(JSON_HAS_INT64)
257std::string JSON_API valueToString(LargestInt value);
258std::string JSON_API valueToString(LargestUInt value);
259std::string JSON_API valueToString(double value);
260std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000261std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000262
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263/// \brief Output using the StyledStreamWriter.
264/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000265JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000266
267} // namespace Json
268
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000269#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000270#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000271#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
272
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000273#endif // JSON_WRITER_H_INCLUDED