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 StackAllocator = CrtAllocator>
41 class PrettyWriter :
public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> {
44 typedef typename Base::Ch Ch;
51 PrettyWriter(OutputStream& os, StackAllocator* allocator = 0,
size_t levelDepth = Base::kDefaultLevelDepth) :
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 Key(
const Ch* str,
SizeType length,
bool copy =
false) {
return String(str, length, copy); }
93 bool EndObject(
SizeType memberCount = 0) {
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;
100 Base::os_->Put(
'\n');
103 if (!Base::WriteEndObject())
105 if (Base::level_stack_.Empty())
112 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
true);
113 return Base::WriteStartArray();
116 bool EndArray(
SizeType memberCount = 0) {
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;
123 Base::os_->Put(
'\n');
126 if (!Base::WriteEndArray())
128 if (Base::level_stack_.Empty())
139 bool String(
const Ch* str) {
return String(str, internal::StrLen(str)); }
140 bool Key(
const Ch* str) {
return Key(str, internal::StrLen(str)); }
144 void PrettyPrefix(
Type type) {
146 if (Base::level_stack_.GetSize() != 0) {
147 typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
149 if (level->inArray) {
150 if (level->valueCount > 0) {
152 Base::os_->Put(
'\n');
155 Base::os_->Put(
'\n');
159 if (level->valueCount > 0) {
160 if (level->valueCount % 2 == 0) {
162 Base::os_->Put(
'\n');
170 Base::os_->Put(
'\n');
172 if (level->valueCount % 2 == 0)
175 if (!level->inArray && level->valueCount % 2 == 0)
181 Base::hasRoot_ =
true;
186 size_t count = (Base::level_stack_.GetSize() /
sizeof(
typename Base::Level)) * indentCharCount_;
187 PutN(*Base::os_, indentChar_, count);
191 unsigned indentCharCount_;
205 #endif // RAPIDJSON_RAPIDJSON_H_
true
Definition: rapidjson.h:570
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:139
unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:247
false
Definition: rapidjson.h:569
Writer with indentation and spacing.
Definition: prettywriter.h:41
Type
Type of JSON value.
Definition: rapidjson.h:567
object
Definition: rapidjson.h:571
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:59
array
Definition: rapidjson.h:572
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:51
JSON writer.
Definition: writer.h:56
main RapidJSON namespace
Definition: rapidjson.h:241
null
Definition: rapidjson.h:568
string
Definition: rapidjson.h:573
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
number
Definition: rapidjson.h:574
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: rapidjson.h:484