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