blob: 46d5ccc841cb9fcec580eafb8b1bc370ae870c6b [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
7# define JSON_WRITER_H_INCLUDED
8
Baptiste Lepilleureadc4782011-05-02 21:09:30 +00009#if !defined(JSON_IS_AMALGAMATION)
Christopher Dunn6d135cb2007-06-13 15:51:04 +000010# include "value.h"
Baptiste Lepilleureadc4782011-05-02 21:09:30 +000011#endif // if !defined(JSON_IS_AMALGAMATION)
Christopher Dunn6d135cb2007-06-13 15:51:04 +000012# include <vector>
13# include <string>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000014
15namespace Json {
16
17 class Value;
18
19 /** \brief Abstract class for writers.
20 */
21 class JSON_API Writer
22 {
23 public:
24 virtual ~Writer();
25
26 virtual std::string write( const Value &root ) = 0;
27 };
28
29 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
30 *
31 * The JSON document is written in a single line. It is not intended for 'human' consumption,
32 * but may be usefull to support feature such as RPC where bandwith is limited.
33 * \sa Reader, Value
34 */
35 class JSON_API FastWriter : public Writer
36 {
37 public:
38 FastWriter();
Christopher Dunn605cd7e2007-06-13 15:55:50 +000039 virtual ~FastWriter(){}
Christopher Dunn6d135cb2007-06-13 15:51:04 +000040
41 void enableYAMLCompatibility();
42
Aaron Jacobsae3c7a72012-03-12 04:53:57 +000043 /** \brief Drop the "null" string from the writer's output for nullValues.
44 * Strictly speaking, this is not valid JSON. But when the output is being
45 * fed to a browser's Javascript, it makes for smaller output and the
46 * browser can handle the output just fine.
47 */
48 void dropNullPlaceholders();
49
Christopher Dunn6d135cb2007-06-13 15:51:04 +000050 public: // overridden from Writer
51 virtual std::string write( const Value &root );
52
53 private:
54 void writeValue( const Value &value );
55
56 std::string document_;
57 bool yamlCompatiblityEnabled_;
Aaron Jacobsae3c7a72012-03-12 04:53:57 +000058 bool dropNullPlaceholders_;
Christopher Dunn6d135cb2007-06-13 15:51:04 +000059 };
60
61 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
62 *
63 * The rules for line break and indent are as follow:
64 * - Object value:
65 * - if empty then print {} without indent and line break
66 * - if not empty the print '{', line break & indent, print one value per line
67 * and then unindent and line break and print '}'.
68 * - Array value:
69 * - if empty then print [] without indent and line break
70 * - if the array contains no object value, empty array or some other value types,
71 * and all the values fit on one lines, then print the array on a single line.
72 * - otherwise, it the values do not fit on one line, or the array contains
73 * object or non empty array, then print one value per line.
74 *
75 * If the Value have comments then they are outputed according to their #CommentPlacement.
76 *
77 * \sa Reader, Value, Value::setComment()
78 */
Christopher Dunn605cd7e2007-06-13 15:55:50 +000079 class JSON_API StyledWriter: public Writer
Christopher Dunn6d135cb2007-06-13 15:51:04 +000080 {
81 public:
82 StyledWriter();
83 virtual ~StyledWriter(){}
84
85 public: // overridden from Writer
86 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
87 * \param root Value to serialize.
88 * \return String containing the JSON document that represents the root value.
89 */
90 virtual std::string write( const Value &root );
91
92 private:
93 void writeValue( const Value &value );
94 void writeArrayValue( const Value &value );
95 bool isMultineArray( const Value &value );
96 void pushValue( const std::string &value );
97 void writeIndent();
98 void writeWithIndent( const std::string &value );
99 void indent();
100 void unindent();
101 void writeCommentBeforeValue( const Value &root );
102 void writeCommentAfterValueOnSameLine( const Value &root );
103 bool hasCommentForValue( const Value &value );
104 static std::string normalizeEOL( const std::string &text );
105
106 typedef std::vector<std::string> ChildValues;
107
108 ChildValues childValues_;
109 std::string document_;
110 std::string indentString_;
111 int rightMargin_;
112 int indentSize_;
113 bool addChildValues_;
114 };
115
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000116 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
117 to a stream rather than to a string.
118 *
119 * The rules for line break and indent are as follow:
120 * - Object value:
121 * - if empty then print {} without indent and line break
122 * - if not empty the print '{', line break & indent, print one value per line
123 * and then unindent and line break and print '}'.
124 * - Array value:
125 * - if empty then print [] without indent and line break
126 * - if the array contains no object value, empty array or some other value types,
127 * and all the values fit on one lines, then print the array on a single line.
128 * - otherwise, it the values do not fit on one line, or the array contains
129 * object or non empty array, then print one value per line.
130 *
131 * If the Value have comments then they are outputed according to their #CommentPlacement.
132 *
133 * \param indentation Each level will be indented by this amount extra.
134 * \sa Reader, Value, Value::setComment()
135 */
136 class JSON_API StyledStreamWriter
137 {
138 public:
139 StyledStreamWriter( std::string indentation="\t" );
140 ~StyledStreamWriter(){}
141
142 public:
143 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
144 * \param out Stream to write to. (Can be ostringstream, e.g.)
145 * \param root Value to serialize.
146 * \note There is no point in deriving from Writer, since write() should not return a value.
147 */
148 void write( std::ostream &out, const Value &root );
149
150 private:
151 void writeValue( const Value &value );
152 void writeArrayValue( const Value &value );
153 bool isMultineArray( const Value &value );
154 void pushValue( const std::string &value );
155 void writeIndent();
156 void writeWithIndent( const std::string &value );
157 void indent();
158 void unindent();
159 void writeCommentBeforeValue( const Value &root );
160 void writeCommentAfterValueOnSameLine( const Value &root );
161 bool hasCommentForValue( const Value &value );
162 static std::string normalizeEOL( const std::string &text );
163
164 typedef std::vector<std::string> ChildValues;
165
166 ChildValues childValues_;
167 std::ostream* document_;
168 std::string indentString_;
169 int rightMargin_;
170 std::string indentation_;
171 bool addChildValues_;
172 };
173
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000174# if defined(JSON_HAS_INT64)
Baptiste Lepilleur3a1b93b2010-02-21 14:08:17 +0000175 std::string JSON_API valueToString( Int value );
176 std::string JSON_API valueToString( UInt value );
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000177# endif // if defined(JSON_HAS_INT64)
178 std::string JSON_API valueToString( LargestInt value );
179 std::string JSON_API valueToString( LargestUInt value );
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000180 std::string JSON_API valueToString( double value );
181 std::string JSON_API valueToString( bool value );
182 std::string JSON_API valueToQuotedString( const char *value );
183
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000184 /// \brief Output using the StyledStreamWriter.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000185 /// \see Json::operator>>()
186 std::ostream& operator<<( std::ostream&, const Value &root );
187
188} // namespace Json
189
190
191
192#endif // JSON_WRITER_H_INCLUDED