blob: cacb10e29e52b510175feaf7ad33ccb3e5adb884 [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
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100026/** \brief Abstract class for writers.
27 */
28class JSON_API Writer {
29public:
30 virtual ~Writer();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000031
Aaron Jacobs11086dd2014-09-15 10:15:29 +100032 virtual std::string write(const Value& root) = 0;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100033};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000034
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100035/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
36 *without formatting (not human friendly).
37 *
38 * The JSON document is written in a single line. It is not intended for 'human'
39 *consumption,
40 * but may be usefull to support feature such as RPC where bandwith is limited.
41 * \sa Reader, Value
42 */
43class JSON_API FastWriter : public Writer {
44public:
45 FastWriter();
46 virtual ~FastWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +000047
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100048 void enableYAMLCompatibility();
Christopher Dunn6d135cb2007-06-13 15:51:04 +000049
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100050 /** \brief Drop the "null" string from the writer's output for nullValues.
51 * Strictly speaking, this is not valid JSON. But when the output is being
52 * fed to a browser's Javascript, it makes for smaller output and the
53 * browser can handle the output just fine.
54 */
55 void dropNullPlaceholders();
Aaron Jacobsae3c7a72012-03-12 04:53:57 +000056
Don Milham5bf16102014-09-02 17:09:07 -060057 void omitEndingLineFeed();
58
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100059public: // overridden from Writer
Aaron Jacobs11086dd2014-09-15 10:15:29 +100060 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +000061
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100062private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +100063 void writeValue(const Value& value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +000064
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100065 std::string document_;
66 bool yamlCompatiblityEnabled_;
67 bool dropNullPlaceholders_;
Don Milham5bf16102014-09-02 17:09:07 -060068 bool omitEndingLineFeed_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100069};
Christopher Dunn6d135cb2007-06-13 15:51:04 +000070
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100071/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
72 *human friendly way.
73 *
74 * The rules for line break and indent are as follow:
75 * - Object value:
76 * - if empty then print {} without indent and line break
77 * - if not empty the print '{', line break & indent, print one value per
78 *line
79 * and then unindent and line break and print '}'.
80 * - Array value:
81 * - if empty then print [] without indent and line break
82 * - if the array contains no object value, empty array or some other value
83 *types,
84 * and all the values fit on one lines, then print the array on a single
85 *line.
86 * - otherwise, it the values do not fit on one line, or the array contains
87 * object or non empty array, then print one value per line.
88 *
89 * If the Value have comments then they are outputed according to their
90 *#CommentPlacement.
91 *
92 * \sa Reader, Value, Value::setComment()
93 */
94class JSON_API StyledWriter : public Writer {
95public:
96 StyledWriter();
97 virtual ~StyledWriter() {}
Christopher Dunn6d135cb2007-06-13 15:51:04 +000098
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100099public: // overridden from Writer
100 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
101 * \param root Value to serialize.
102 * \return String containing the JSON document that represents the root value.
103 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000104 virtual std::string write(const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000105
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000106private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000107 void writeValue(const Value& value);
108 void writeArrayValue(const Value& value);
109 bool isMultineArray(const Value& value);
110 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000111 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000112 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000113 void indent();
114 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000115 void writeCommentBeforeValue(const Value& root);
116 void writeCommentAfterValueOnSameLine(const Value& root);
117 bool hasCommentForValue(const Value& value);
118 static std::string normalizeEOL(const std::string& text);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000119
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000120 typedef std::vector<std::string> ChildValues;
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000121
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000122 ChildValues childValues_;
123 std::string document_;
124 std::string indentString_;
125 int rightMargin_;
126 int indentSize_;
127 bool addChildValues_;
128};
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 to a stream rather than to a string.
133 *
134 * The rules for line break and indent are as follow:
135 * - Object value:
136 * - if empty then print {} without indent and line break
137 * - if not empty the print '{', line break & indent, print one value per
138 line
139 * and then unindent and line break and print '}'.
140 * - Array value:
141 * - if empty then print [] without indent and line break
142 * - if the array contains no object value, empty array or some other value
143 types,
144 * and all the values fit on one lines, then print the array on a single
145 line.
146 * - otherwise, it the values do not fit on one line, or the array contains
147 * object or non empty array, then print one value per line.
148 *
149 * If the Value have comments then they are outputed according to their
150 #CommentPlacement.
151 *
152 * \param indentation Each level will be indented by this amount extra.
153 * \sa Reader, Value, Value::setComment()
154 */
155class JSON_API StyledStreamWriter {
156public:
157 StyledStreamWriter(std::string indentation = "\t");
158 ~StyledStreamWriter() {}
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000159
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160public:
161 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
162 * \param out Stream to write to. (Can be ostringstream, e.g.)
163 * \param root Value to serialize.
164 * \note There is no point in deriving from Writer, since write() should not
165 * return a value.
166 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000167 void write(std::ostream& out, const Value& root);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000168
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000169private:
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000170 void writeValue(const Value& value);
171 void writeArrayValue(const Value& value);
172 bool isMultineArray(const Value& value);
173 void pushValue(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000174 void writeIndent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000175 void writeWithIndent(const std::string& value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000176 void indent();
177 void unindent();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000178 void writeCommentBeforeValue(const Value& root);
179 void writeCommentAfterValueOnSameLine(const Value& root);
180 bool hasCommentForValue(const Value& value);
181 static std::string normalizeEOL(const std::string& text);
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000182
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 typedef std::vector<std::string> ChildValues;
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000184
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000185 ChildValues childValues_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000186 std::ostream* document_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000187 std::string indentString_;
188 int rightMargin_;
189 std::string indentation_;
Christopher Dunnd3830562015-01-23 13:09:43 -0600190 bool addChildValues_ : 1;
191 bool indented_ : 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000192};
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000193
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194#if defined(JSON_HAS_INT64)
195std::string JSON_API valueToString(Int value);
196std::string JSON_API valueToString(UInt value);
197#endif // if defined(JSON_HAS_INT64)
198std::string JSON_API valueToString(LargestInt value);
199std::string JSON_API valueToString(LargestUInt value);
200std::string JSON_API valueToString(double value);
201std::string JSON_API valueToString(bool value);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000202std::string JSON_API valueToQuotedString(const char* value);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000203
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204/// \brief Output using the StyledStreamWriter.
205/// \see Json::operator>>()
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000206JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000207
208} // namespace Json
209
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000210#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000212#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
213
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000214#endif // JSON_WRITER_H_INCLUDED