blob: 41984983eb21998a170051895a18134c649e4c1d [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 Dunn6d135cb2007-06-13 15:51:04 +000025
Christopher Dunn5fbfe3c2015-01-22 14:31:32 -060026class JSON_API StreamWriter {
27protected:
28 std::ostream& sout_; // not owned; will not delete
29public:
30 StreamWriter(std::ostream* sout);
31 virtual ~StreamWriter();
32 /// Write Value into document as configured in sub-class.
33 /// \return zero on success
34 /// \throw std::exception possibly, depending on configuration
35 virtual int write(Value const& root) const = 0;
36};
37
38class JSON_API StreamWriterBuilder {
39public:
40 virtual ~StreamWriterBuilder();
41 /// Do not delete stream (i.e. not owned), but keep a reference.
42 virtual StreamWriter* newStreamWriter(std::ostream* stream) const;
43};
44
45class JSON_API StreamWriterBuilderFactory {
46public:
47 virtual ~StreamWriterBuilderFactory();
48 virtual StreamWriterBuilder* newStreamWriterBuilder();
49};
50
51/// \brief Write into stringstream, then return string, for convenience.
52std::string writeString(Value const& root, StreamWriterBuilder const& builder);
53
54
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100055/** \brief Abstract class for writers.
56 */
57class JSON_API Writer {
58public:
59 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000060
Aaron Jacobs11086dd2014-09-15 10:15:29 +100061 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100062};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000063
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100064/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
65 *without formatting (not human friendly).
66 *
67 * The JSON document is written in a single line. It is not intended for 'human'
68 *consumption,
69 * but may be usefull to support feature such as RPC where bandwith is limited.
70 * \sa Reader, Value
71 */
72class JSON_API FastWriter : public Writer {
73public:
74 FastWriter();
75 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +000076
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100077 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000078
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100079 /** \brief Drop the "null" string from the writer's output for nullValues.
80 * Strictly speaking, this is not valid JSON. But when the output is being
81 * fed to a browser's Javascript, it makes for smaller output and the
82 * browser can handle the output just fine.
83 */
84 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +000085
Don Milham5bf16102014-09-02 17:09:07 -060086 void omitEndingLineFeed();
87
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100088public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +100089 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +000090
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100091private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +100092 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +000093
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100094 std::string document_;
95 bool yamlCompatiblityEnabled_;
96 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -060097 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100098};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000099
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000100/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
101 *human friendly way.
102 *
103 * The rules for line break and indent are as follow:
104 * - Object value:
105 * - if empty then print {} without indent and line break
106 * - if not empty the print '{', line break & indent, print one value per
107 *line
108 * and then unindent and line break and print '}'.
109 * - Array value:
110 * - if empty then print [] without indent and line break
111 * - if the array contains no object value, empty array or some other value
112 *types,
113 * and all the values fit on one lines, then print the array on a single
114 *line.
115 * - otherwise, it the values do not fit on one line, or the array contains
116 * object or non empty array, then print one value per line.
117 *
118 * If the Value have comments then they are outputed according to their
119 *#CommentPlacement.
120 *
121 * \sa Reader, Value, Value::setComment()
122 */
123class JSON_API StyledWriter : public Writer {
124public:
125 StyledWriter();
126 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000127
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000128public: // overridden from Writer
129 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
130 * \param root Value to serialize.
131 * \return String containing the JSON document that represents the root value.
132 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000133 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000134
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000135private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000136 void writeValue(const Value& value);
137 void writeArrayValue(const Value& value);
138 bool isMultineArray(const Value& value);
139 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000141 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000142 void indent();
143 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000144 void writeCommentBeforeValue(const Value& root);
145 void writeCommentAfterValueOnSameLine(const Value& root);
146 bool hasCommentForValue(const Value& value);
147 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000148
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000149 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000150
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000151 ChildValues childValues_;
152 std::string document_;
153 std::string indentString_;
154 int rightMargin_;
155 int indentSize_;
156 bool addChildValues_;
157};
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000158
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
160 human friendly way,
161 to a stream rather than to a string.
162 *
163 * The rules for line break and indent are as follow:
164 * - Object value:
165 * - if empty then print {} without indent and line break
166 * - if not empty the print '{', line break & indent, print one value per
167 line
168 * and then unindent and line break and print '}'.
169 * - Array value:
170 * - if empty then print [] without indent and line break
171 * - if the array contains no object value, empty array or some other value
172 types,
173 * and all the values fit on one lines, then print the array on a single
174 line.
175 * - otherwise, it the values do not fit on one line, or the array contains
176 * object or non empty array, then print one value per line.
177 *
178 * If the Value have comments then they are outputed according to their
179 #CommentPlacement.
180 *
181 * \param indentation Each level will be indented by this amount extra.
182 * \sa Reader, Value, Value::setComment()
183 */
184class JSON_API StyledStreamWriter {
185public:
186 StyledStreamWriter(std::string indentation = "\t");
187 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000188
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189public:
190 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
191 * \param out Stream to write to. (Can be ostringstream, e.g.)
192 * \param root Value to serialize.
193 * \note There is no point in deriving from Writer, since write() should not
194 * return a value.
195 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000196 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000197
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000198private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000199 void writeValue(const Value& value);
200 void writeArrayValue(const Value& value);
201 bool isMultineArray(const Value& value);
202 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000203 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000204 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 void indent();
206 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000207 void writeCommentBeforeValue(const Value& root);
208 void writeCommentAfterValueOnSameLine(const Value& root);
209 bool hasCommentForValue(const Value& value);
210 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000211
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000212 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000213
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000214 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000215 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216 std::string indentString_;
217 int rightMargin_;
218 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600219 bool addChildValues_ : 1;
220 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000222
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000223#if defined(JSON_HAS_INT64)
224std::string JSON_API valueToString(Int value);
225std::string JSON_API valueToString(UInt value);
226#endif // if defined(JSON_HAS_INT64)
227std::string JSON_API valueToString(LargestInt value);
228std::string JSON_API valueToString(LargestUInt value);
229std::string JSON_API valueToString(double value);
230std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000231std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000232
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000233/// \brief Output using the StyledStreamWriter.
234/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000235JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000236
237} // namespace Json
238
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000239#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000240#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000241#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
242
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000243#endif // JSON_WRITER_H_INCLUDED