prettywriter.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_PRETTYWRITER_H_
16 #define RAPIDJSON_PRETTYWRITER_H_
17 
18 #include "writer.h"
19 
20 #if __GNUC__
21 RAPIDJSON_DIAG_PUSH
22 RAPIDJSON_DIAG_OFF(effc++)
23 #endif
24 
25 #if defined(__clang__)
26 RAPIDJSON_DIAG_PUSH
27 RAPIDJSON_DIAG_OFF(c++ 98 - compat)
28 #endif
29 
31 
33 
36 {
39 };
40 
42 
48 template <typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>,
49  typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
50 class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags>
51 {
52 public:
54  typedef typename Base::Ch Ch;
55 
57 
61  explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth)
62  : Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault)
63  {
64  }
65 
66  explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth)
67  : Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4)
68  {
69  }
70 
71 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
73  : Base(std::forward<PrettyWriter>(rhs))
74  , indentChar_(rhs.indentChar_)
75  , indentCharCount_(rhs.indentCharCount_)
76  , formatOptions_(rhs.formatOptions_)
77  {
78  }
79 #endif
80 
82 
86  PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount)
87  {
88  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r');
89  indentChar_ = indentChar;
90  indentCharCount_ = indentCharCount;
91  return *this;
92  }
93 
95 
98  {
99  formatOptions_ = options;
100  return *this;
101  }
102 
107 
108  bool Null()
109  {
112  }
113  bool Bool(bool b)
114  {
116  return Base::EndValue(Base::WriteBool(b));
117  }
118  bool Int(int i)
119  {
121  return Base::EndValue(Base::WriteInt(i));
122  }
123  bool Uint(unsigned u)
124  {
126  return Base::EndValue(Base::WriteUint(u));
127  }
128  bool Int64(int64_t i64)
129  {
131  return Base::EndValue(Base::WriteInt64(i64));
132  }
133  bool Uint64(uint64_t u64)
134  {
136  return Base::EndValue(Base::WriteUint64(u64));
137  }
138  bool Double(double d)
139  {
142  }
143 
144  bool RawNumber(const Ch* str, SizeType length, bool copy = false)
145  {
146  RAPIDJSON_ASSERT(str != 0);
147  (void)copy;
149  return Base::EndValue(Base::WriteString(str, length));
150  }
151 
152  bool String(const Ch* str, SizeType length, bool copy = false)
153  {
154  RAPIDJSON_ASSERT(str != 0);
155  (void)copy;
157  return Base::EndValue(Base::WriteString(str, length));
158  }
159 
160 #if RAPIDJSON_HAS_STDSTRING
161  bool String(const std::basic_string<Ch>& str)
162  {
163  return String(str.data(), SizeType(str.size()));
164  }
165 #endif
166 
167  bool StartObject()
168  {
170  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
171  return Base::WriteStartObject();
172  }
173 
174  bool Key(const Ch* str, SizeType length, bool copy = false)
175  {
176  return String(str, length, copy);
177  }
178 
179 #if RAPIDJSON_HAS_STDSTRING
180  bool Key(const std::basic_string<Ch>& str)
181  {
182  return Key(str.data(), SizeType(str.size()));
183  }
184 #endif
185 
186  bool EndObject(SizeType memberCount = 0)
187  {
188  (void)memberCount;
189  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); // not inside an Object
191  !Base::level_stack_.template Top<typename Base::Level>()->inArray); // currently inside an Array, not Object
192  RAPIDJSON_ASSERT(0 == Base::level_stack_.template Top<typename Base::Level>()->valueCount % 2); // Object has a Key
193  // without a Value
194 
195  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
196 
197  if (!empty)
198  {
199  Base::os_->Put('\n');
200  WriteIndent();
201  }
202  bool ret = Base::EndValue(Base::WriteEndObject());
203  (void)ret;
204  RAPIDJSON_ASSERT(ret == true);
205  if (Base::level_stack_.Empty()) // end of json text
206  Base::Flush();
207  return true;
208  }
209 
210  bool StartArray()
211  {
213  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
214  return Base::WriteStartArray();
215  }
216 
217  bool EndArray(SizeType memberCount = 0)
218  {
219  (void)memberCount;
220  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
221  RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
222  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
223 
224  if (!empty && !(formatOptions_ & kFormatSingleLineArray))
225  {
226  Base::os_->Put('\n');
227  WriteIndent();
228  }
229  bool ret = Base::EndValue(Base::WriteEndArray());
230  (void)ret;
231  RAPIDJSON_ASSERT(ret == true);
232  if (Base::level_stack_.Empty()) // end of json text
233  Base::Flush();
234  return true;
235  }
236 
238 
241 
243  bool String(const Ch* str)
244  {
245  return String(str, internal::StrLen(str));
246  }
247  bool Key(const Ch* str)
248  {
249  return Key(str, internal::StrLen(str));
250  }
251 
253 
255 
263  bool RawValue(const Ch* json, size_t length, Type type)
264  {
265  RAPIDJSON_ASSERT(json != 0);
266  PrettyPrefix(type);
267  return Base::EndValue(Base::WriteRawValue(json, length));
268  }
269 
270 protected:
271  void PrettyPrefix(Type type)
272  {
273  (void)type;
274  if (Base::level_stack_.GetSize() != 0)
275  { // this value is not at root
276  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
277 
278  if (level->inArray)
279  {
280  if (level->valueCount > 0)
281  {
282  Base::os_->Put(','); // add comma if it is not the first element in array
284  Base::os_->Put(' ');
285  }
286 
288  {
289  Base::os_->Put('\n');
290  WriteIndent();
291  }
292  }
293  else
294  { // in object
295  if (level->valueCount > 0)
296  {
297  if (level->valueCount % 2 == 0)
298  {
299  Base::os_->Put(',');
300  Base::os_->Put('\n');
301  }
302  else
303  {
304  Base::os_->Put(':');
305  Base::os_->Put(' ');
306  }
307  }
308  else
309  Base::os_->Put('\n');
310 
311  if (level->valueCount % 2 == 0)
312  WriteIndent();
313  }
314  if (!level->inArray && level->valueCount % 2 == 0)
315  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
316  level->valueCount++;
317  }
318  else
319  {
320  RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root.
321  Base::hasRoot_ = true;
322  }
323  }
324 
325  void WriteIndent()
326  {
327  size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
328  PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_), count);
329  }
330 
334 
335 private:
336  // Prohibit copy constructor & assignment operator.
337  PrettyWriter(const PrettyWriter&);
339 };
340 
342 
343 #if defined(__clang__)
344 RAPIDJSON_DIAG_POP
345 #endif
346 
347 #if __GNUC__
348 RAPIDJSON_DIAG_POP
349 #endif
350 
351 #endif // RAPIDJSON_RAPIDJSON_H_
PrettyFormatOptions
Combination of PrettyWriter format flags.
Definition: prettywriter.h:35
bool EndValue(bool ret)
Definition: writer.h:626
unsigned indentCharCount_
Definition: prettywriter.h:332
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
Definition: prettywriter.h:263
void WriteIndent()
Definition: prettywriter.h:325
size_t valueCount
number of values in this level
Definition: writer.h:359
void Flush()
Flush the output stream.
Definition: writer.h:347
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:394
bool inArray
true if in array, otherwise in object
Definition: writer.h:360
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:416
PrettyFormatOptions formatOptions_
Definition: prettywriter.h:333
bool WriteInt64(int64_t i64)
Definition: writer.h:417
JSON writer.
Definition: fwd.h:105
object
Definition: rapidjson.h:671
void PrettyPrefix(Type type)
Definition: prettywriter.h:271
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:86
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:243
bool WriteUint64(uint64_t u64)
Definition: writer.h:427
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
static const size_t kDefaultLevelDepth
Definition: writer.h:363
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
array
Definition: rapidjson.h:672
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:174
bool WriteRawValue(const Ch *json, size_t length)
Definition: writer.h:586
bool StartObject()
Definition: prettywriter.h:167
bool hasRoot_
Definition: writer.h:636
false
Definition: rapidjson.h:669
bool WriteStartObject()
Definition: writer.h:565
PrettyWriter & operator=(const PrettyWriter &)
bool Int(int i)
Definition: prettywriter.h:118
PrettyWriter(StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Definition: prettywriter.h:66
bool Bool(bool b)
Definition: prettywriter.h:113
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
Default pretty formatting.
Definition: prettywriter.h:37
Base::Ch Ch
Definition: prettywriter.h:54
bool RawNumber(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:144
bool WriteUint(unsigned u)
Definition: writer.h:407
OutputStream * os_
Definition: writer.h:633
SourceEncoding::Ch Ch
Definition: writer.h:95
Information for each nested level.
Definition: writer.h:354
string
Definition: rapidjson.h:673
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:186
bool WriteNull()
Definition: writer.h:365
unsigned __int64 uint64_t
Definition: stdint.h:136
number
Definition: rapidjson.h:674
size_t GetSize() const
Definition: stack.h:212
bool WriteEndObject()
Definition: writer.h:570
bool Int64(int64_t i64)
Definition: prettywriter.h:128
bool Key(const Ch *str)
Definition: prettywriter.h:247
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:477
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:217
signed __int64 int64_t
Definition: stdint.h:135
bool WriteEndArray()
Definition: writer.h:580
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:61
true
Definition: rapidjson.h:670
bool WriteStartArray()
Definition: writer.h:575
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:31
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:152
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:634
bool WriteDouble(double d)
Definition: writer.h:437
Format arrays on a single line.
Definition: prettywriter.h:38
Type
Type of JSON value.
Definition: rapidjson.h:666
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags > Base
Definition: prettywriter.h:53
bool StartArray()
Definition: prettywriter.h:210
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
Definition: prettywriter.h:97
bool Uint64(uint64_t u64)
Definition: prettywriter.h:133
bool WriteInt(int i)
Definition: writer.h:397
Writer with indentation and spacing.
Definition: fwd.h:111
bool Uint(unsigned u)
Definition: prettywriter.h:123
null
Definition: rapidjson.h:668
bool WriteBool(bool b)
Definition: writer.h:375
bool Double(double d)
Definition: prettywriter.h:138


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53