1 #ifndef RAPIDJSON_PRETTYWRITER_H_
2 #define RAPIDJSON_PRETTYWRITER_H_
8 RAPIDJSON_DIAG_OFF(effc++)
20 template<
typename OutputStream,
typename SourceEncoding = UTF8<>,
typename TargetEncoding = UTF8<>,
typename Allocator = MemoryPoolAllocator<> >
24 typedef typename Base::Ch Ch;
32 Base(os, allocator, levelDepth), indentChar_(
' '), indentCharCount_(4) {}
43 RAPIDJSON_ASSERT(indentChar ==
' ' || indentChar ==
'\t' || indentChar ==
'\n' || indentChar ==
'\r');
44 indentChar_ = indentChar;
45 indentCharCount_ = indentCharCount;
54 bool Null() { PrettyPrefix(
kNullType);
return Base::WriteNull(); }
56 bool Int(
int i) { PrettyPrefix(
kNumberType);
return Base::WriteInt(i); }
57 bool Uint(
unsigned u) { PrettyPrefix(
kNumberType);
return Base::WriteUint(u); }
58 bool Int64(int64_t i64) { PrettyPrefix(
kNumberType);
return Base::WriteInt64(i64); }
59 bool Uint64(uint64_t u64) { PrettyPrefix(
kNumberType);
return Base::WriteUint64(u64); }
62 bool String(
const Ch* str,
SizeType length,
bool copy =
false) {
65 return Base::WriteString(str, length);
70 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
false);
71 return Base::WriteStartObject();
74 bool EndObject(
SizeType memberCount = 0) {
76 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
77 RAPIDJSON_ASSERT(!Base::level_stack_.
template Top<typename Base::Level>()->inArray);
78 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
84 if (!Base::WriteEndObject())
86 if (Base::level_stack_.Empty())
93 new (Base::level_stack_.template Push<typename Base::Level>())
typename Base::Level(
true);
94 return Base::WriteStartArray();
97 bool EndArray(
SizeType memberCount = 0) {
99 RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >=
sizeof(
typename Base::Level));
100 RAPIDJSON_ASSERT(Base::level_stack_.
template Top<typename Base::Level>()->inArray);
101 bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
104 Base::os_->Put(
'\n');
107 if (!Base::WriteEndArray())
109 if (Base::level_stack_.Empty())
120 bool String(
const Ch* str) {
return String(str, internal::StrLen(str)); }
126 bool ret = Double(d);
133 void PrettyPrefix(
Type type) {
135 if (Base::level_stack_.GetSize() != 0) {
136 typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
138 if (level->inArray) {
139 if (level->valueCount > 0) {
141 Base::os_->Put(
'\n');
144 Base::os_->Put(
'\n');
148 if (level->valueCount > 0) {
149 if (level->valueCount % 2 == 0) {
151 Base::os_->Put(
'\n');
159 Base::os_->Put(
'\n');
161 if (level->valueCount % 2 == 0)
164 if (!level->inArray && level->valueCount % 2 == 0)
171 Base::hasRoot_ =
true;
176 size_t count = (Base::level_stack_.GetSize() /
sizeof(
typename Base::Level)) * indentCharCount_;
177 PutN(*Base::os_, indentChar_, count);
181 unsigned indentCharCount_;
195 #endif // RAPIDJSON_RAPIDJSON_H_
true
Definition: rapidjson.h:394
PrettyWriter & SetDoublePrecision(int p)
Overridden for fluent API, see Writer::SetDoublePrecision()
Definition: prettywriter.h:35
bool Double(double d, int precision)
Overridden for fluent API, see Writer::Double()
Definition: prettywriter.h:123
unsigned SizeType
Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
Definition: rapidjson.h:133
false
Definition: rapidjson.h:393
Writer with indentation and spacing.
Definition: prettywriter.h:21
Writer & SetDoublePrecision(int p=kDefaultDoublePrecision)
Set the number of significant digits for double values.
Definition: writer.h:92
int GetDoublePrecision() const
Definition: writer.h:99
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:146
Type
Type of JSON value.
Definition: rapidjson.h:391
object
Definition: rapidjson.h:395
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:120
array
Definition: rapidjson.h:396
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:71
JSON writer.
Definition: writer.h:36
main RapidJSON namespace
Definition: allocators.h:6
null
Definition: rapidjson.h:392
bool WriteDouble(double d)
Definition: writer.h:255
string
Definition: rapidjson.h:397
Concept for allocating, resizing and freeing memory block.
PrettyWriter(OutputStream &os, Allocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:31
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:42
number
Definition: rapidjson.h:398