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