blob: 4d74f934c492178eb97f8d1ad5118765f0b801f2 [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
9# include "value.h"
10# include <vector>
11# include <string>
12# include <iostream>
13
14namespace Json {
15
16 class Value;
17
18 /** \brief Abstract class for writers.
19 */
20 class JSON_API Writer
21 {
22 public:
23 virtual ~Writer();
24
25 virtual std::string write( const Value &root ) = 0;
26 };
27
28 /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
29 *
30 * The JSON document is written in a single line. It is not intended for 'human' consumption,
31 * but may be usefull to support feature such as RPC where bandwith is limited.
32 * \sa Reader, Value
33 */
34 class JSON_API FastWriter : public Writer
35 {
36 public:
37 FastWriter();
Christopher Dunn605cd7e2007-06-13 15:55:50 +000038 virtual ~FastWriter(){}
Christopher Dunn6d135cb2007-06-13 15:51:04 +000039
40 void enableYAMLCompatibility();
41
42 public: // overridden from Writer
43 virtual std::string write( const Value &root );
44
45 private:
46 void writeValue( const Value &value );
47
48 std::string document_;
49 bool yamlCompatiblityEnabled_;
50 };
51
52 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
53 *
54 * The rules for line break and indent are as follow:
55 * - Object value:
56 * - if empty then print {} without indent and line break
57 * - if not empty the print '{', line break & indent, print one value per line
58 * and then unindent and line break and print '}'.
59 * - Array value:
60 * - if empty then print [] without indent and line break
61 * - if the array contains no object value, empty array or some other value types,
62 * and all the values fit on one lines, then print the array on a single line.
63 * - otherwise, it the values do not fit on one line, or the array contains
64 * object or non empty array, then print one value per line.
65 *
66 * If the Value have comments then they are outputed according to their #CommentPlacement.
67 *
68 * \sa Reader, Value, Value::setComment()
69 */
Christopher Dunn605cd7e2007-06-13 15:55:50 +000070 class JSON_API StyledWriter: public Writer
Christopher Dunn6d135cb2007-06-13 15:51:04 +000071 {
72 public:
73 StyledWriter();
74 virtual ~StyledWriter(){}
75
76 public: // overridden from Writer
77 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
78 * \param root Value to serialize.
79 * \return String containing the JSON document that represents the root value.
80 */
81 virtual std::string write( const Value &root );
82
83 private:
84 void writeValue( const Value &value );
85 void writeArrayValue( const Value &value );
86 bool isMultineArray( const Value &value );
87 void pushValue( const std::string &value );
88 void writeIndent();
89 void writeWithIndent( const std::string &value );
90 void indent();
91 void unindent();
92 void writeCommentBeforeValue( const Value &root );
93 void writeCommentAfterValueOnSameLine( const Value &root );
94 bool hasCommentForValue( const Value &value );
95 static std::string normalizeEOL( const std::string &text );
96
97 typedef std::vector<std::string> ChildValues;
98
99 ChildValues childValues_;
100 std::string document_;
101 std::string indentString_;
102 int rightMargin_;
103 int indentSize_;
104 bool addChildValues_;
105 };
106
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000107 /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
108 to a stream rather than to a string.
109 *
110 * The rules for line break and indent are as follow:
111 * - Object value:
112 * - if empty then print {} without indent and line break
113 * - if not empty the print '{', line break & indent, print one value per line
114 * and then unindent and line break and print '}'.
115 * - Array value:
116 * - if empty then print [] without indent and line break
117 * - if the array contains no object value, empty array or some other value types,
118 * and all the values fit on one lines, then print the array on a single line.
119 * - otherwise, it the values do not fit on one line, or the array contains
120 * object or non empty array, then print one value per line.
121 *
122 * If the Value have comments then they are outputed according to their #CommentPlacement.
123 *
124 * \param indentation Each level will be indented by this amount extra.
125 * \sa Reader, Value, Value::setComment()
126 */
127 class JSON_API StyledStreamWriter
128 {
129 public:
130 StyledStreamWriter( std::string indentation="\t" );
131 ~StyledStreamWriter(){}
132
133 public:
134 /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
135 * \param out Stream to write to. (Can be ostringstream, e.g.)
136 * \param root Value to serialize.
137 * \note There is no point in deriving from Writer, since write() should not return a value.
138 */
139 void write( std::ostream &out, const Value &root );
140
141 private:
142 void writeValue( const Value &value );
143 void writeArrayValue( const Value &value );
144 bool isMultineArray( const Value &value );
145 void pushValue( const std::string &value );
146 void writeIndent();
147 void writeWithIndent( const std::string &value );
148 void indent();
149 void unindent();
150 void writeCommentBeforeValue( const Value &root );
151 void writeCommentAfterValueOnSameLine( const Value &root );
152 bool hasCommentForValue( const Value &value );
153 static std::string normalizeEOL( const std::string &text );
154
155 typedef std::vector<std::string> ChildValues;
156
157 ChildValues childValues_;
158 std::ostream* document_;
159 std::string indentString_;
160 int rightMargin_;
161 std::string indentation_;
162 bool addChildValues_;
163 };
164
Baptiste Lepilleur3a1b93b2010-02-21 14:08:17 +0000165 std::string JSON_API valueToString( Int value );
166 std::string JSON_API valueToString( UInt value );
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000167 std::string JSON_API valueToString( double value );
168 std::string JSON_API valueToString( bool value );
169 std::string JSON_API valueToQuotedString( const char *value );
170
Christopher Dunn605cd7e2007-06-13 15:55:50 +0000171 /// \brief Output using the StyledStreamWriter.
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000172 /// \see Json::operator>>()
173 std::ostream& operator<<( std::ostream&, const Value &root );
174
175} // namespace Json
176
177
178
179#endif // JSON_WRITER_H_INCLUDED