15 #ifndef RAPIDJSON_PRETTYWRITER_H_
16 #define RAPIDJSON_PRETTYWRITER_H_
22 RAPIDJSON_DIAG_OFF(effc++)
25 RAPIDJSON_NAMESPACE_BEGIN
34 template<
typename OutputStream,
typename SourceEncoding = UTF8<>,
typename TargetEncoding = UTF8<>,
typename StackAllocator = CrtAllocator>
35 class PrettyWriter :
public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> {
38 typedef typename Base::Ch Ch;
45 PrettyWriter(OutputStream& os, StackAllocator* allocator = 0,
size_t levelDepth = Base::kDefaultLevelDepth) :
46 Base(os, allocator, levelDepth), indentChar_(
' '), indentCharCount_(4) {}
54 RAPIDJSON_ASSERT(indentChar ==
' ' || indentChar ==
'\t' || indentChar ==
'\n' || indentChar ==
'\r');
55 indentChar_ = indentChar;
56 indentCharCount_ = indentCharCount;
65 bool Null() { PrettyPrefix(
kNullType);
return Base::WriteNull(); }
67 bool Int(
int i) { PrettyPrefix(
kNumberType);
return Base::WriteInt(i); }
68 bool Uint(
unsigned u) { PrettyPrefix(
kNumberType);
return Base::WriteUint(u); }
69 bool Int64(int64_t i64) { PrettyPrefix(
kNumberType);
return Base::WriteInt64(i64); }
70 bool Uint64(uint64_t u64) { PrettyPrefix(
kNumberType);
return Base::WriteUint64(u64); }
71 bool Double(
double d) { PrettyPrefix(
kNumberType);
return Base::WriteDouble(d); }
73 bool String(
const Ch* str,
SizeType length,
bool copy =
false) {
76 return Base::WriteString(str, length);
79 #if RAPIDJSON_HAS_STDSTRING
80 bool String(
const std::basic_string<Ch>& str) {
81 return String(str.data(),
SizeType(str.size()));
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 bool ret = Base::WriteEndObject();
106 if (Base::level_stack_.Empty())
113 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
true);
114 return Base::WriteStartArray();
117 bool EndArray(
SizeType memberCount = 0) {
119 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
120 RAPIDJSON_ASSERT(Base::level_stack_.
template Top<typename Base::Level>()->inArray);
121 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
124 Base::os_->Put(
'\n');
127 bool ret = Base::WriteEndArray();
130 if (Base::level_stack_.Empty())
141 bool String(
const Ch* str) {
return String(str, internal::StrLen(str)); }
142 bool Key(
const Ch* str) {
return Key(str, internal::StrLen(str)); }
146 void PrettyPrefix(
Type type) {
148 if (Base::level_stack_.GetSize() != 0) {
149 typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
151 if (level->inArray) {
152 if (level->valueCount > 0) {
154 Base::os_->Put(
'\n');
157 Base::os_->Put(
'\n');
161 if (level->valueCount > 0) {
162 if (level->valueCount % 2 == 0) {
164 Base::os_->Put(
'\n');
172 Base::os_->Put(
'\n');
174 if (level->valueCount % 2 == 0)
177 if (!level->inArray && level->valueCount % 2 == 0)
183 Base::hasRoot_ =
true;
188 size_t count = (Base::level_stack_.GetSize() /
sizeof(
typename Base::Level)) * indentCharCount_;
189 PutN(*Base::os_, indentChar_, count);
193 unsigned indentCharCount_;
197 PrettyWriter(
const PrettyWriter&);
198 PrettyWriter& operator=(
const PrettyWriter&);
201 RAPIDJSON_NAMESPACE_END
207 #endif // RAPIDJSON_RAPIDJSON_H_
true
Definition: rapidjson.h:645
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:141
unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:322
false
Definition: rapidjson.h:644
Writer with indentation and spacing.
Definition: prettywriter.h:35
Type
Type of JSON value.
Definition: rapidjson.h:642
object
Definition: rapidjson.h:646
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:53
array
Definition: rapidjson.h:647
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:45
JSON writer.
Definition: writer.h:54
null
Definition: rapidjson.h:643
string
Definition: rapidjson.h:648
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
number
Definition: rapidjson.h:649
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: rapidjson.h:559