15 #ifndef RAPIDJSON_PRETTYWRITER_H_
16 #define RAPIDJSON_PRETTYWRITER_H_
22 RAPIDJSON_DIAG_OFF(effc++)
25 RAPIDJSON_NAMESPACE_BEGIN
42 template<
typename OutputStream,
typename SourceEncoding = UTF8<>,
typename TargetEncoding = UTF8<>,
typename StackAllocator = CrtAllocator,
unsigned writeFlags = kWriteDefaultFlags>
43 class PrettyWriter :
public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> {
45 typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> Base;
46 typedef typename Base::Ch Ch;
53 explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0,
size_t levelDepth = Base::kDefaultLevelDepth) :
54 Base(os, allocator, levelDepth), indentChar_(
' '), indentCharCount_(4), formatOptions_(
kFormatDefault) {}
57 explicit PrettyWriter(StackAllocator* allocator = 0,
size_t levelDepth = Base::kDefaultLevelDepth) :
58 Base(allocator, levelDepth), indentChar_(
' '), indentCharCount_(4) {}
66 RAPIDJSON_ASSERT(indentChar ==
' ' || indentChar ==
'\t' || indentChar ==
'\n' || indentChar ==
'\r');
67 indentChar_ = indentChar;
68 indentCharCount_ = indentCharCount;
76 formatOptions_ = options;
85 bool Null() { PrettyPrefix(
kNullType);
return Base::WriteNull(); }
87 bool Int(
int i) { PrettyPrefix(
kNumberType);
return Base::WriteInt(i); }
88 bool Uint(
unsigned u) { PrettyPrefix(
kNumberType);
return Base::WriteUint(u); }
89 bool Int64(int64_t i64) { PrettyPrefix(
kNumberType);
return Base::WriteInt64(i64); }
90 bool Uint64(uint64_t u64) { PrettyPrefix(
kNumberType);
return Base::WriteUint64(u64); }
91 bool Double(
double d) { PrettyPrefix(
kNumberType);
return Base::WriteDouble(d); }
93 bool RawNumber(
const Ch* str,
SizeType length,
bool copy =
false) {
96 return Base::WriteString(str, length);
99 bool String(
const Ch* str,
SizeType length,
bool copy =
false) {
102 return Base::WriteString(str, length);
105 #if RAPIDJSON_HAS_STDSTRING
106 bool String(
const std::basic_string<Ch>& str) {
107 return String(str.data(),
SizeType(str.size()));
113 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
false);
114 return Base::WriteStartObject();
117 bool Key(
const Ch* str,
SizeType length,
bool copy =
false) {
return String(str, length, copy); }
119 #if RAPIDJSON_HAS_STDSTRING
120 bool Key(
const std::basic_string<Ch>& str) {
121 return Key(str.data(),
SizeType(str.size()));
125 bool EndObject(
SizeType memberCount = 0) {
127 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
128 RAPIDJSON_ASSERT(!Base::level_stack_.
template Top<typename Base::Level>()->inArray);
129 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
132 Base::os_->Put(
'\n');
135 bool ret = Base::WriteEndObject();
138 if (Base::level_stack_.Empty())
145 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
true);
146 return Base::WriteStartArray();
149 bool EndArray(
SizeType memberCount = 0) {
151 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
152 RAPIDJSON_ASSERT(Base::level_stack_.
template Top<typename Base::Level>()->inArray);
153 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
155 if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
156 Base::os_->Put(
'\n');
159 bool ret = Base::WriteEndArray();
162 if (Base::level_stack_.Empty())
173 bool String(
const Ch* str) {
return String(str, internal::StrLen(str)); }
174 bool Key(
const Ch* str) {
return Key(str, internal::StrLen(str)); }
187 bool RawValue(
const Ch* json,
size_t length,
Type type) { PrettyPrefix(type);
return Base::WriteRawValue(json, length); }
190 void PrettyPrefix(
Type type) {
192 if (Base::level_stack_.GetSize() != 0) {
193 typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
195 if (level->inArray) {
196 if (level->valueCount > 0) {
198 if (formatOptions_ & kFormatSingleLineArray)
202 if (!(formatOptions_ & kFormatSingleLineArray)) {
203 Base::os_->Put(
'\n');
208 if (level->valueCount > 0) {
209 if (level->valueCount % 2 == 0) {
211 Base::os_->Put(
'\n');
219 Base::os_->Put(
'\n');
221 if (level->valueCount % 2 == 0)
224 if (!level->inArray && level->valueCount % 2 == 0)
230 Base::hasRoot_ =
true;
235 size_t count = (Base::level_stack_.GetSize() /
sizeof(
typename Base::Level)) * indentCharCount_;
236 PutN(*Base::os_,
static_cast<typename TargetEncoding::Ch
>(indentChar_), count);
240 unsigned indentCharCount_;
245 PrettyWriter(
const PrettyWriter&);
246 PrettyWriter& operator=(
const PrettyWriter&);
249 RAPIDJSON_NAMESPACE_END
255 #endif // RAPIDJSON_RAPIDJSON_H_