prettywriter.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON
2 // available.
3 //
4 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
5 // rights reserved.
6 //
7 // Licensed under the MIT License (the "License"); you may not use this file
8 // except in compliance with the License. You may obtain a copy of the License
9 // at
10 //
11 // http://opensource.org/licenses/MIT
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 // License for the specific language governing permissions and limitations under
17 // the License.
18 
19 #ifndef RAPIDJSON_PRETTYWRITER_H_
20 #define RAPIDJSON_PRETTYWRITER_H_
21 
22 #include "writer.h"
23 
24 #ifdef __GNUC__
25 RAPIDJSON_DIAG_PUSH
26 RAPIDJSON_DIAG_OFF(effc++)
27 #endif
28 
29 #if defined(__clang__)
30 RAPIDJSON_DIAG_PUSH
31 RAPIDJSON_DIAG_OFF(c++ 98 - compat)
32 #endif
33 
35 
37 
42 };
43 
45 
51 template <typename OutputStream, typename SourceEncoding = UTF8<>,
52  typename TargetEncoding = UTF8<>,
53  typename StackAllocator = CrtAllocator,
54  unsigned writeFlags = kWriteDefaultFlags>
55 class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
56  StackAllocator, writeFlags> {
57  public:
58  typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator,
59  writeFlags>
61  typedef typename Base::Ch Ch;
62 
64 
68  explicit PrettyWriter(OutputStream &os, StackAllocator *allocator = 0,
69  size_t levelDepth = Base::kDefaultLevelDepth)
70  : Base(os, allocator, levelDepth),
71  indentChar_(' '),
74 
75  explicit PrettyWriter(StackAllocator *allocator = 0,
76  size_t levelDepth = Base::kDefaultLevelDepth)
77  : Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
78 
79 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
81  : Base(std::forward<PrettyWriter>(rhs)),
82  indentChar_(rhs.indentChar_),
83  indentCharCount_(rhs.indentCharCount_),
84  formatOptions_(rhs.formatOptions_) {}
85 #endif
86 
88 
93  PrettyWriter &SetIndent(Ch indentChar, unsigned indentCharCount) {
94  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' ||
95  indentChar == '\n' || indentChar == '\r');
96  indentChar_ = indentChar;
97  indentCharCount_ = indentCharCount;
98  return *this;
99  }
100 
102 
105  formatOptions_ = options;
106  return *this;
107  }
108 
113 
114  bool Null() {
117  }
118  bool Bool(bool b) {
120  return Base::EndValue(Base::WriteBool(b));
121  }
122  bool Int(int i) {
124  return Base::EndValue(Base::WriteInt(i));
125  }
126  bool Uint(unsigned u) {
128  return Base::EndValue(Base::WriteUint(u));
129  }
130  bool Int64(int64_t i64) {
132  return Base::EndValue(Base::WriteInt64(i64));
133  }
134  bool Uint64(uint64_t u64) {
136  return Base::EndValue(Base::WriteUint64(u64));
137  }
138  bool Double(double d) {
141  }
142 
143  bool RawNumber(const Ch *str, SizeType length, bool copy = false) {
144  RAPIDJSON_ASSERT(str != 0);
145  (void)copy;
147  return Base::EndValue(Base::WriteString(str, length));
148  }
149 
150  bool String(const Ch *str, SizeType length, bool copy = false) {
151  RAPIDJSON_ASSERT(str != 0);
152  (void)copy;
154  return Base::EndValue(Base::WriteString(str, length));
155  }
156 
157 #if RAPIDJSON_HAS_STDSTRING
158  bool String(const std::basic_string<Ch> &str) {
159  return String(str.data(), SizeType(str.size()));
160  }
161 #endif
162 
163  bool StartObject() {
165  new (Base::level_stack_.template Push<typename Base::Level>())
166  typename Base::Level(false);
167  return Base::WriteStartObject();
168  }
169 
170  bool Key(const Ch *str, SizeType length, bool copy = false) {
171  return String(str, length, copy);
172  }
173 
174 #if RAPIDJSON_HAS_STDSTRING
175  bool Key(const std::basic_string<Ch> &str) {
176  return Key(str.data(), SizeType(str.size()));
177  }
178 #endif
179 
180  bool EndObject(SizeType memberCount = 0) {
181  (void)memberCount;
183  sizeof(typename Base::Level)); // not inside an Object
184  RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()
185  ->inArray); // currently inside an Array, not Object
187  0 ==
188  Base::level_stack_.template Top<typename Base::Level>()->valueCount %
189  2); // Object has a Key without a Value
190 
191  bool empty =
192  Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount ==
193  0;
194 
195  if (!empty) {
196  Base::os_->Put('\n');
197  WriteIndent();
198  }
199  bool ret = Base::EndValue(Base::WriteEndObject());
200  (void)ret;
201  RAPIDJSON_ASSERT(ret == true);
202  if (Base::level_stack_.Empty()) // end of json text
203  Base::Flush();
204  return true;
205  }
206 
207  bool StartArray() {
209  new (Base::level_stack_.template Push<typename Base::Level>())
210  typename Base::Level(true);
211  return Base::WriteStartArray();
212  }
213 
214  bool EndArray(SizeType memberCount = 0) {
215  (void)memberCount;
217  sizeof(typename Base::Level));
219  Base::level_stack_.template Top<typename Base::Level>()->inArray);
220  bool empty =
221  Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount ==
222  0;
223 
224  if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
225  Base::os_->Put('\n');
226  WriteIndent();
227  }
228  bool ret = Base::EndValue(Base::WriteEndArray());
229  (void)ret;
230  RAPIDJSON_ASSERT(ret == true);
231  if (Base::level_stack_.Empty()) // end of json text
232  Base::Flush();
233  return true;
234  }
235 
237 
240 
242  bool String(const Ch *str) { return String(str, internal::StrLen(str)); }
243  bool Key(const Ch *str) { return Key(str, internal::StrLen(str)); }
244 
246 
248 
256  bool RawValue(const Ch *json, size_t length, Type type) {
257  RAPIDJSON_ASSERT(json != 0);
258  PrettyPrefix(type);
259  return Base::EndValue(Base::WriteRawValue(json, length));
260  }
261 
262  protected:
263  void PrettyPrefix(Type type) {
264  (void)type;
265  if (Base::level_stack_.GetSize() != 0) { // this value is not at root
266  typename Base::Level *level =
267  Base::level_stack_.template Top<typename Base::Level>();
268 
269  if (level->inArray) {
270  if (level->valueCount > 0) {
271  Base::os_->Put(
272  ','); // add comma if it is not the first element in array
274  }
275 
277  Base::os_->Put('\n');
278  WriteIndent();
279  }
280  } else { // in object
281  if (level->valueCount > 0) {
282  if (level->valueCount % 2 == 0) {
283  Base::os_->Put(',');
284  Base::os_->Put('\n');
285  } else {
286  Base::os_->Put(':');
287  Base::os_->Put(' ');
288  }
289  } else
290  Base::os_->Put('\n');
291 
292  if (level->valueCount % 2 == 0) WriteIndent();
293  }
294  if (!level->inArray && level->valueCount % 2 == 0)
295  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even
296  // number should be a name
297  level->valueCount++;
298  } else {
300  !Base::hasRoot_); // Should only has one and only one root.
301  Base::hasRoot_ = true;
302  }
303  }
304 
305  void WriteIndent() {
306  size_t count =
307  (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) *
309  PutN(*Base::os_, static_cast<typename OutputStream::Ch>(indentChar_),
310  count);
311  }
312 
316 
317  private:
318  // Prohibit copy constructor & assignment operator.
319  PrettyWriter(const PrettyWriter &);
321 };
322 
324 
325 #if defined(__clang__)
326 RAPIDJSON_DIAG_POP
327 #endif
328 
329 #ifdef __GNUC__
330 RAPIDJSON_DIAG_POP
331 #endif
332 
333 #endif // RAPIDJSON_RAPIDJSON_H_
PrettyFormatOptions
Combination of PrettyWriter format flags.
Definition: prettywriter.h:39
bool EndValue(bool ret)
Definition: writer.h:582
unsigned indentCharCount_
Definition: prettywriter.h:314
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
Definition: prettywriter.h:256
void WriteIndent()
Definition: prettywriter.h:305
size_t valueCount
number of values in this level
Definition: writer.h:334
void Flush()
Flush the output stream.
Definition: writer.h:328
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:411
bool inArray
true if in array, otherwise in object
Definition: writer.h:335
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:433
PrettyFormatOptions formatOptions_
Definition: prettywriter.h:315
bool WriteInt64(int64_t i64)
Definition: writer.h:385
JSON writer.
Definition: fwd.h:110
object
Definition: rapidjson.h:711
void PrettyPrefix(Type type)
Definition: prettywriter.h:263
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:93
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:242
bool WriteUint64(uint64_t u64)
Definition: writer.h:394
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:131
static const size_t kDefaultLevelDepth
Definition: writer.h:338
void PutN(FileWriteStream &stream, char c, size_t n)
array
Definition: rapidjson.h:712
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:170
bool WriteRawValue(const Ch *json, size_t length)
Definition: writer.h:544
bool StartObject()
Definition: prettywriter.h:163
bool hasRoot_
Definition: writer.h:591
false
Definition: rapidjson.h:709
bool WriteStartObject()
Definition: writer.h:527
PrettyWriter & operator=(const PrettyWriter &)
bool Int(int i)
Definition: prettywriter.h:122
PrettyWriter(StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Definition: prettywriter.h:75
bool Bool(bool b)
Definition: prettywriter.h:118
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:128
Default pretty formatting.
Definition: prettywriter.h:40
Base::Ch Ch
Definition: prettywriter.h:61
bool RawNumber(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:143
bool WriteUint(unsigned u)
Definition: writer.h:376
OutputStream * os_
Definition: writer.h:588
SourceEncoding::Ch Ch
Definition: writer.h:104
Information for each nested level.
Definition: writer.h:332
string
Definition: rapidjson.h:713
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:180
bool WriteNull()
Definition: writer.h:340
unsigned __int64 uint64_t
Definition: stdint.h:137
number
Definition: rapidjson.h:714
size_t GetSize() const
Definition: stack.h:191
bool WriteEndObject()
Definition: writer.h:531
bool Int64(int64_t i64)
Definition: prettywriter.h:130
bool Key(const Ch *str)
Definition: prettywriter.h:243
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:437
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:214
signed __int64 int64_t
Definition: stdint.h:136
bool WriteEndArray()
Definition: writer.h:539
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:68
true
Definition: rapidjson.h:710
bool WriteStartArray()
Definition: writer.h:535
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:36
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:150
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:589
bool WriteDouble(double d)
Definition: writer.h:403
Format arrays on a single line.
Definition: prettywriter.h:41
Type
Type of JSON value.
Definition: rapidjson.h:707
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags > Base
Definition: prettywriter.h:60
bool StartArray()
Definition: prettywriter.h:207
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
Definition: prettywriter.h:104
bool Uint64(uint64_t u64)
Definition: prettywriter.h:134
bool WriteInt(int i)
Definition: writer.h:367
Writer with indentation and spacing.
Definition: fwd.h:116
bool Uint(unsigned u)
Definition: prettywriter.h:126
null
Definition: rapidjson.h:708
bool WriteBool(bool b)
Definition: writer.h:349
bool Double(double d)
Definition: prettywriter.h:138


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46