blob: dc9e46f4bd637db37a4db75eda6ee414cb86eaad [file] [log] [blame]
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04001// 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
6#ifndef JSON_WRITER_H_INCLUDED
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -05007#define JSON_WRITER_H_INCLUDED
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04008
9#if !defined(JSON_IS_AMALGAMATION)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050010#include "value.h"
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040011#endif // if !defined(JSON_IS_AMALGAMATION)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050012#include <vector>
13#include <string>
14
15// Disable warning C4251: <data member>: <type> needs to have dll-interface to
16// be used by...
17#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
18#pragma warning(push)
19#pragma warning(disable : 4251)
20#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040021
22namespace Json {
23
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050024class Value;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040025
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050026/** \brief Abstract class for writers.
27 */
28class JSON_API Writer {
29public:
30 virtual ~Writer();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040031
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050032 virtual std::string write(const Value& root) = 0;
33};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040034
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050035/** \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() {}
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040047
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050048 void enableYAMLCompatibility();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040049
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050050 /** \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();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040056
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050057 void omitEndingLineFeed();
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040058
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050059public: // overridden from Writer
60 virtual std::string write(const Value& root);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040061
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050062private:
63 void writeValue(const Value& value);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040064
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050065 std::string document_;
66 bool yamlCompatiblityEnabled_;
67 bool dropNullPlaceholders_;
68 bool omitEndingLineFeed_;
69};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040070
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050071/** \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() {}
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040098
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050099public: // 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 */
104 virtual std::string write(const Value& root);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400105
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500106private:
107 void writeValue(const Value& value);
108 void writeArrayValue(const Value& value);
109 bool isMultineArray(const Value& value);
110 void pushValue(const std::string& value);
111 void writeIndent();
112 void writeWithIndent(const std::string& value);
113 void indent();
114 void unindent();
115 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);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400119
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500120 typedef std::vector<std::string> ChildValues;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400121
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500122 ChildValues childValues_;
123 std::string document_;
124 std::string indentString_;
125 int rightMargin_;
126 int indentSize_;
127 bool addChildValues_;
128};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400129
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500130/** \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() {}
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400159
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500160public:
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 */
167 void write(std::ostream& out, const Value& root);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400168
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500169private:
170 void writeValue(const Value& value);
171 void writeArrayValue(const Value& value);
172 bool isMultineArray(const Value& value);
173 void pushValue(const std::string& value);
174 void writeIndent();
175 void writeWithIndent(const std::string& value);
176 void indent();
177 void unindent();
178 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);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400182
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500183 typedef std::vector<std::string> ChildValues;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400184
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500185 ChildValues childValues_;
186 std::ostream* document_;
187 std::string indentString_;
188 int rightMargin_;
189 std::string indentation_;
190 bool addChildValues_;
191};
192
193#if defined(JSON_HAS_INT64)
194std::string JSON_API valueToString(Int value);
195std::string JSON_API valueToString(UInt value);
196#endif // if defined(JSON_HAS_INT64)
197std::string JSON_API valueToString(LargestInt value);
198std::string JSON_API valueToString(LargestUInt value);
199std::string JSON_API valueToString(double value);
200std::string JSON_API valueToString(bool value);
201std::string JSON_API valueToQuotedString(const char* value);
202
203/// \brief Output using the StyledStreamWriter.
204/// \see Json::operator>>()
205JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400206
207} // namespace Json
208
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500209#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
210#pragma warning(pop)
211#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400212
213#endif // JSON_WRITER_H_INCLUDED