49class PrettyWriter :
public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> {
52 typedef typename Base::Ch Ch;
59 explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0,
size_t levelDepth = Base::kDefaultLevelDepth) :
60 Base(os, allocator, levelDepth), indentChar_(
' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
63 explicit PrettyWriter(StackAllocator* allocator = 0,
size_t levelDepth = Base::kDefaultLevelDepth) :
64 Base(allocator, levelDepth), indentChar_(
' '), indentCharCount_(4) {}
66#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
68 Base(std::forward<
PrettyWriter>(rhs)), indentChar_(rhs.indentChar_), indentCharCount_(rhs.indentCharCount_), formatOptions_(rhs.formatOptions_) {}
77 RAPIDJSON_ASSERT(indentChar ==
' ' || indentChar ==
'\t' || indentChar ==
'\n' || indentChar ==
'\r');
78 indentChar_ = indentChar;
79 indentCharCount_ = indentCharCount;
87 formatOptions_ = options;
96 bool Null() { PrettyPrefix(
kNullType);
return Base::EndValue(Base::WriteNull()); }
97 bool Bool(
bool b) { PrettyPrefix(b ?
kTrueType :
kFalseType);
return Base::EndValue(Base::WriteBool(b)); }
98 bool Int(
int i) { PrettyPrefix(
kNumberType);
return Base::EndValue(Base::WriteInt(i)); }
99 bool Uint(
unsigned u) { PrettyPrefix(
kNumberType);
return Base::EndValue(Base::WriteUint(u)); }
100 bool Int64(int64_t i64) { PrettyPrefix(
kNumberType);
return Base::EndValue(Base::WriteInt64(i64)); }
101 bool Uint64(
uint64_t u64) { PrettyPrefix(
kNumberType);
return Base::EndValue(Base::WriteUint64(u64)); }
102 bool Double(
double d) { PrettyPrefix(
kNumberType);
return Base::EndValue(Base::WriteDouble(d)); }
104 bool RawNumber(
const Ch* str,
SizeType length,
bool copy =
false) {
108 return Base::EndValue(Base::WriteString(str, length));
111 bool String(
const Ch* str,
SizeType length,
bool copy =
false) {
115 return Base::EndValue(Base::WriteString(str, length));
118#if RAPIDJSON_HAS_STDSTRING
119 bool String(
const std::basic_string<Ch>& str) {
120 return String(str.data(),
SizeType(str.size()));
126 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
false);
127 return Base::WriteStartObject();
130 bool Key(
const Ch* str,
SizeType length,
bool copy =
false) {
return String(str, length, copy); }
132#if RAPIDJSON_HAS_STDSTRING
133 bool Key(
const std::basic_string<Ch>& str) {
134 return Key(str.data(),
SizeType(str.size()));
138 bool EndObject(
SizeType memberCount = 0) {
140 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
141 RAPIDJSON_ASSERT(!Base::level_stack_.
template Top<typename Base::Level>()->inArray);
142 RAPIDJSON_ASSERT(0 == Base::level_stack_.
template Top<typename Base::Level>()->valueCount % 2);
144 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
147 Base::os_->Put(
'\n');
150 bool ret = Base::EndValue(Base::WriteEndObject());
153 if (Base::level_stack_.Empty())
160 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
true);
161 return Base::WriteStartArray();
164 bool EndArray(
SizeType memberCount = 0) {
166 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
167 RAPIDJSON_ASSERT(Base::level_stack_.
template Top<typename Base::Level>()->inArray);
168 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
170 if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
171 Base::os_->Put(
'\n');
174 bool ret = Base::EndValue(Base::WriteEndArray());
177 if (Base::level_stack_.Empty())
188 bool String(
const Ch* str) {
return String(str, internal::StrLen(str)); }
189 bool Key(
const Ch* str) {
return Key(str, internal::StrLen(str)); }
205 return Base::EndValue(Base::WriteRawValue(json, length));
209 void PrettyPrefix(
Type type) {
211 if (Base::level_stack_.GetSize() != 0) {
212 typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
214 if (level->inArray) {
215 if (level->valueCount > 0) {
217 if (formatOptions_ & kFormatSingleLineArray)
221 if (!(formatOptions_ & kFormatSingleLineArray)) {
222 Base::os_->Put(
'\n');
227 if (level->valueCount > 0) {
228 if (level->valueCount % 2 == 0) {
230 Base::os_->Put(
'\n');
238 Base::os_->Put(
'\n');
240 if (level->valueCount % 2 == 0)
243 if (!level->inArray && level->valueCount % 2 == 0)
249 Base::hasRoot_ =
true;
254 size_t count = (Base::level_stack_.GetSize() /
sizeof(
typename Base::Level)) * indentCharCount_;
255 PutN(*Base::os_,
static_cast<typename OutputStream::Ch
>(indentChar_), count);
259 unsigned indentCharCount_;
260 PrettyFormatOptions formatOptions_;