webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
prettywriter.h
Go to the documentation of this file.
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_PRETTYWRITER_H_
22 #define RAPIDJSON_PRETTYWRITER_H_
23 
24 #include "writer.h"
25 
26 #ifdef __GNUC__
27 RAPIDJSON_DIAG_PUSH
28 RAPIDJSON_DIAG_OFF(effc++)
29 #endif
30 
32 
34 
40 template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
41 class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> {
42 public:
44  typedef typename Base::Ch Ch;
45 
47 
51  PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
52  Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
53 
55 
59  PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) {
60  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r');
61  indentChar_ = indentChar;
62  indentCharCount_ = indentCharCount;
63  return *this;
64  }
65 
70 
71  bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); }
72  bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); }
73  bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); }
74  bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); }
75  bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); }
77  bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); }
78 
79  bool String(const Ch* str, SizeType length, bool copy = false) {
80  (void)copy;
82  return Base::WriteString(str, length);
83  }
84 
85  bool StartObject() {
87  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
88  return Base::WriteStartObject();
89  }
90 
91  bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
92 
93  bool EndObject(SizeType memberCount = 0) {
94  (void)memberCount;
95  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
96  RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray);
97  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
98 
99  if (!empty) {
100  Base::os_->Put('\n');
101  WriteIndent();
102  }
103  if (!Base::WriteEndObject())
104  return false;
105  if (Base::level_stack_.Empty()) // end of json text
106  Base::os_->Flush();
107  return true;
108  }
109 
110  bool StartArray() {
112  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
113  return Base::WriteStartArray();
114  }
115 
116  bool EndArray(SizeType memberCount = 0) {
117  (void)memberCount;
118  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
119  RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
120  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
121 
122  if (!empty) {
123  Base::os_->Put('\n');
124  WriteIndent();
125  }
126  if (!Base::WriteEndArray())
127  return false;
128  if (Base::level_stack_.Empty()) // end of json text
129  Base::os_->Flush();
130  return true;
131  }
132 
134 
137 
139  bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
140  bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
141 
143 protected:
145  (void)type;
146  if (Base::level_stack_.GetSize() != 0) { // this value is not at root
147  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
148 
149  if (level->inArray) {
150  if (level->valueCount > 0) {
151  Base::os_->Put(','); // add comma if it is not the first element in array
152  Base::os_->Put('\n');
153  }
154  else
155  Base::os_->Put('\n');
156  WriteIndent();
157  }
158  else { // in object
159  if (level->valueCount > 0) {
160  if (level->valueCount % 2 == 0) {
161  Base::os_->Put(',');
162  Base::os_->Put('\n');
163  }
164  else {
165  Base::os_->Put(':');
166  Base::os_->Put(' ');
167  }
168  }
169  else
170  Base::os_->Put('\n');
171 
172  if (level->valueCount % 2 == 0)
173  WriteIndent();
174  }
175  if (!level->inArray && level->valueCount % 2 == 0)
176  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
177  level->valueCount++;
178  }
179  else {
180  RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root.
181  Base::hasRoot_ = true;
182  }
183  }
184 
185  void WriteIndent() {
186  size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
187  PutN(*Base::os_, indentChar_, count);
188  }
189 
192 
193 private:
194  // Prohibit copy constructor & assignment operator.
195  PrettyWriter(const PrettyWriter&);
196  PrettyWriter& operator=(const PrettyWriter&);
197 };
198 
200 
201 #ifdef __GNUC__
202 RAPIDJSON_DIAG_POP
203 #endif
204 
205 #endif // RAPIDJSON_RAPIDJSON_H_
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:79
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:59
bool Int(int i)
Definition: prettywriter.h:73
GLint GLsizei count
Definition: gl2.h:421
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:334
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:139
bool StartArray()
Definition: prettywriter.h:110
unsigned long long uint64_t
Definition: ptypes.h:120
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:293
bool WriteInt(int i)
Definition: writer.h:200
object
Definition: rapidjson.h:620
bool WriteEndArray()
Definition: writer.h:311
bool WriteInt64(int64_t i64)
Definition: writer.h:216
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > Base
Definition: prettywriter.h:43
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:91
array
Definition: rapidjson.h:621
bool WriteStartObject()
Definition: writer.h:308
Ch indentChar_
Definition: prettywriter.h:190
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:398
bool Uint(unsigned u)
Definition: prettywriter.h:74
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:116
Base::Ch Ch
Definition: prettywriter.h:44
void WriteIndent()
Definition: prettywriter.h:185
false
Definition: rapidjson.h:618
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:240
bool StartObject()
Definition: prettywriter.h:85
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:91
void PrettyPrefix(Type type)
Definition: prettywriter.h:144
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
JSON writer.
Definition: writer.h:56
signed long long int64_t
Definition: ptypes.h:112
bool WriteStartArray()
Definition: writer.h:310
static const size_t kDefaultLevelDepth
Definition: writer.h:184
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:93
size_t GetSize() const
Definition: stack.h:133
Level
Definition: profile_level_id.h:28
bool Double(double d)
Definition: prettywriter.h:77
bool WriteBool(bool b)
Definition: writer.h:190
bool Key(const Ch *str)
Definition: prettywriter.h:140
GLint level
Definition: gl2.h:402
void
Definition: AVFoundationCFSoftLinking.h:81
bool Uint64(uint64_t u64)
Definition: prettywriter.h:76
string
Definition: rapidjson.h:622
bool Int64(int64_t i64)
Definition: prettywriter.h:75
bool WriteUint64(uint64_t u64)
Definition: writer.h:224
size_t valueCount
number of values in this level
Definition: writer.h:180
unsigned indentCharCount_
Definition: prettywriter.h:191
Writer with indentation and spacing.
Definition: prettywriter.h:41
number
Definition: rapidjson.h:623
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
bool WriteUint(unsigned u)
Definition: writer.h:208
bool Null()
Definition: prettywriter.h:71
EGLenum type
Definition: eglext.h:63
str
Definition: make-dist.py:305
bool WriteNull()
Definition: writer.h:186
Information for each nested level.
Definition: writer.h:178
bool hasRoot_
Definition: writer.h:335
for i
Definition: complexityMeasures.m:24
SourceEncoding::Ch Ch
Definition: writer.h:58
bool Bool(bool b)
Definition: prettywriter.h:72
bool WriteDouble(double d)
Definition: writer.h:232
true
Definition: rapidjson.h:619
bool WriteEndObject()
Definition: writer.h:309
GLboolean GLboolean GLboolean b
Definition: gl2ext.h:306
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:34
OutputStream * os_
Definition: writer.h:333
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:51
Type
Type of JSON value.
Definition: rapidjson.h:616
bool inArray
true if in array, otherwise in object
Definition: writer.h:181
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:315
#define d
Definition: float-mm.c:30
GLuint GLsizei GLsizei * length
Definition: gl2.h:435
null
Definition: rapidjson.h:617