blob: d5306c9eaf155560dfb322ae549d22d6c86bdccb [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
68
69 /// Do not take ownership of sout, but maintain a reference.
70 StreamWriter* newStreamWriter(std::ostream* sout);
71 };
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060072};
73
74/// \brief Write into stringstream, then return string, for convenience.
75std::string writeString(Value const& root, StreamWriterBuilder const& builder);
76
77
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100078/** \brief Abstract class for writers.
79 */
80class JSON_API Writer {
81public:
82 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000083
Aaron Jacobs11086dd2014-09-15 10:15:29 +100084 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100085};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000086
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100087/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
88 *without formatting (not human friendly).
89 *
90 * The JSON document is written in a single line. It is not intended for 'human'
91 *consumption,
92 * but may be usefull to support feature such as RPC where bandwith is limited.
93 * \sa Reader, Value
94 */
95class JSON_API FastWriter : public Writer {
96public:
97 FastWriter();
98 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +000099
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000100 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000101
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000102 /** \brief Drop the "null" string from the writer's output for nullValues.
103 * Strictly speaking, this is not valid JSON. But when the output is being
104 * fed to a browser's Javascript, it makes for smaller output and the
105 * browser can handle the output just fine.
106 */
107 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +0000108
Don Milham5bf16102014-09-02 17:09:07 -0600109 void omitEndingLineFeed();
110
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000111public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000112 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000113
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000114private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000115 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000116
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000117 std::string document_;
118 bool yamlCompatiblityEnabled_;
119 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -0600120 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000121};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000122
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000123/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
124 *human friendly way.
125 *
126 * The rules for line break and indent are as follow:
127 * - Object value:
128 * - if empty then print {} without indent and line break
129 * - if not empty the print '{', line break & indent, print one value per
130 *line
131 * and then unindent and line break and print '}'.
132 * - Array value:
133 * - if empty then print [] without indent and line break
134 * - if the array contains no object value, empty array or some other value
135 *types,
136 * and all the values fit on one lines, then print the array on a single
137 *line.
138 * - otherwise, it the values do not fit on one line, or the array contains
139 * object or non empty array, then print one value per line.
140 *
141 * If the Value have comments then they are outputed according to their
142 *#CommentPlacement.
143 *
144 * \sa Reader, Value, Value::setComment()
145 */
146class JSON_API StyledWriter : public Writer {
147public:
148 StyledWriter();
149 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000150
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000151public: // overridden from Writer
152 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
153 * \param root Value to serialize.
154 * \return String containing the JSON document that represents the root value.
155 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000156 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000157
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000158private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000159 void writeValue(const Value& value);
160 void writeArrayValue(const Value& value);
161 bool isMultineArray(const Value& value);
162 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000163 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000164 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000165 void indent();
166 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000167 void writeCommentBeforeValue(const Value& root);
168 void writeCommentAfterValueOnSameLine(const Value& root);
169 bool hasCommentForValue(const Value& value);
170 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000171
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000172 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000173
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 ChildValues childValues_;
175 std::string document_;
176 std::string indentString_;
177 int rightMargin_;
178 int indentSize_;
179 bool addChildValues_;
180};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000181
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000182/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
183 human friendly way,
184 to a stream rather than to a string.
185 *
186 * The rules for line break and indent are as follow:
187 * - Object value:
188 * - if empty then print {} without indent and line break
189 * - if not empty the print '{', line break & indent, print one value per
190 line
191 * and then unindent and line break and print '}'.
192 * - Array value:
193 * - if empty then print [] without indent and line break
194 * - if the array contains no object value, empty array or some other value
195 types,
196 * and all the values fit on one lines, then print the array on a single
197 line.
198 * - otherwise, it the values do not fit on one line, or the array contains
199 * object or non empty array, then print one value per line.
200 *
201 * If the Value have comments then they are outputed according to their
202 #CommentPlacement.
203 *
204 * \param indentation Each level will be indented by this amount extra.
205 * \sa Reader, Value, Value::setComment()
206 */
207class JSON_API StyledStreamWriter {
208public:
209 StyledStreamWriter(std::string indentation = "\t");
210 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000211
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000212public:
213 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
214 * \param out Stream to write to. (Can be ostringstream, e.g.)
215 * \param root Value to serialize.
216 * \note There is no point in deriving from Writer, since write() should not
217 * return a value.
218 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000219 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000220
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000222 void writeValue(const Value& value);
223 void writeArrayValue(const Value& value);
224 bool isMultineArray(const Value& value);
225 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000226 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000227 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000228 void indent();
229 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000230 void writeCommentBeforeValue(const Value& root);
231 void writeCommentAfterValueOnSameLine(const Value& root);
232 bool hasCommentForValue(const Value& value);
233 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000234
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000235 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000236
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000237 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000238 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239 std::string indentString_;
240 int rightMargin_;
241 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600242 bool addChildValues_ : 1;
243 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000245
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246#if defined(JSON_HAS_INT64)
247std::string JSON_API valueToString(Int value);
248std::string JSON_API valueToString(UInt value);
249#endif // if defined(JSON_HAS_INT64)
250std::string JSON_API valueToString(LargestInt value);
251std::string JSON_API valueToString(LargestUInt value);
252std::string JSON_API valueToString(double value);
253std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000254std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000255
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000256/// \brief Output using the StyledStreamWriter.
257/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000258JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000259
260} // namespace Json
261
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000262#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000264#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
265
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000266#endif // JSON_WRITER_H_INCLUDED