21 #ifndef RAPIDJSON_PRETTYWRITER_H_
22 #define RAPIDJSON_PRETTYWRITER_H_
28 RAPIDJSON_DIAG_OFF(effc++)
40 template<
typename OutputStream,
typename SourceEncoding = UTF8<>,
typename TargetEncoding = UTF8<>,
typename Allocator = MemoryPoolAllocator<> >
44 typedef typename Base::Ch Ch;
52 Base(os, allocator, levelDepth), indentChar_(
' '), indentCharCount_(4) {}
60 RAPIDJSON_ASSERT(indentChar ==
' ' || indentChar ==
'\t' || indentChar ==
'\n' || indentChar ==
'\r');
61 indentChar_ = indentChar;
62 indentCharCount_ = indentCharCount;
71 bool Null() { PrettyPrefix(
kNullType);
return Base::WriteNull(); }
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); }
76 bool Uint64(uint64_t u64) { PrettyPrefix(
kNumberType);
return Base::WriteUint64(u64); }
77 bool Double(
double d) { PrettyPrefix(
kNumberType);
return Base::WriteDouble(d); }
79 bool String(
const Ch* str,
SizeType length,
bool copy =
false) {
82 return Base::WriteString(str, length);
87 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
false);
88 return Base::WriteStartObject();
91 bool EndObject(
SizeType memberCount = 0) {
93 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
94 RAPIDJSON_ASSERT(!Base::level_stack_.
template Top<typename Base::Level>()->inArray);
95 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
101 if (!Base::WriteEndObject())
103 if (Base::level_stack_.Empty())
110 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
true);
111 return Base::WriteStartArray();
114 bool EndArray(
SizeType memberCount = 0) {
116 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
117 RAPIDJSON_ASSERT(Base::level_stack_.
template Top<typename Base::Level>()->inArray);
118 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
121 Base::os_->Put(
'\n');
124 if (!Base::WriteEndArray())
126 if (Base::level_stack_.Empty())
137 bool String(
const Ch* str) {
return String(str, internal::StrLen(str)); }
141 void PrettyPrefix(
Type type) {
143 if (Base::level_stack_.GetSize() != 0) {
144 typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
146 if (level->inArray) {
147 if (level->valueCount > 0) {
149 Base::os_->Put(
'\n');
152 Base::os_->Put(
'\n');
156 if (level->valueCount > 0) {
157 if (level->valueCount % 2 == 0) {
159 Base::os_->Put(
'\n');
167 Base::os_->Put(
'\n');
169 if (level->valueCount % 2 == 0)
172 if (!level->inArray && level->valueCount % 2 == 0)
178 Base::hasRoot_ =
true;
183 size_t count = (Base::level_stack_.GetSize() /
sizeof(
typename Base::Level)) * indentCharCount_;
184 PutN(*Base::os_, indentChar_, count);
188 unsigned indentCharCount_;
202 #endif // RAPIDJSON_RAPIDJSON_H_
true
Definition: rapidjson.h:423
unsigned SizeType
Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
Definition: rapidjson.h:162
false
Definition: rapidjson.h:422
Writer with indentation and spacing.
Definition: prettywriter.h:41
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:175
Type
Type of JSON value.
Definition: rapidjson.h:420
object
Definition: rapidjson.h:424
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:137
array
Definition: rapidjson.h:425
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:91
JSON writer.
Definition: writer.h:56
null
Definition: rapidjson.h:421
string
Definition: rapidjson.h:426
Concept for allocating, resizing and freeing memory block.
PrettyWriter(OutputStream &os, Allocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:51
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:59
number
Definition: rapidjson.h:427