All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > Class Template Reference

Writer with indentation and spacing. More...

#include <prettywriter.h>

Inheritance diagram for rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >:
rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >

Public Types

typedef Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > Base
 
typedef Base::Ch Ch
 
- Public Types inherited from rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >
typedef SourceEncoding::Ch Ch
 

Public Member Functions

 PrettyWriter (OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
 Constructor. More...
 
PrettyWriterSetIndent (Ch indentChar, unsigned indentCharCount)
 Set custom indentation. More...
 
Implementation of Handler
See also
Handler
bool Null ()
 
bool Bool (bool b)
 
bool Int (int i)
 
bool Uint (unsigned u)
 
bool Int64 (int64_t i64)
 
bool Uint64 (uint64_t u64)
 
bool Double (double d)
 
bool String (const Ch *str, SizeType length, bool copy=false)
 
bool StartObject ()
 
bool Key (const Ch *str, SizeType length, bool copy=false)
 
bool EndObject (SizeType memberCount=0)
 
bool StartArray ()
 
bool EndArray (SizeType memberCount=0)
 
Convenience extensions
bool String (const Ch *str)
 Simpler but slower overload. More...
 
bool Key (const Ch *str)
 
- Public Member Functions inherited from rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >
 Writer (OutputStream &os, StackAllocator *stackAllocator=0, size_t levelDepth=kDefaultLevelDepth)
 Constructor. More...
 
 Writer (StackAllocator *allocator=0, size_t levelDepth=kDefaultLevelDepth)
 
void Reset (OutputStream &os)
 Reset the writer with a new stream. More...
 
bool IsComplete () const
 Checks whether the output is a complete JSON. More...
 
bool Null ()
 
bool Bool (bool b)
 
bool Int (int i)
 
bool Uint (unsigned u)
 
bool Int64 (int64_t i64)
 
bool Uint64 (uint64_t u64)
 
bool Double (double d)
 Writes the given double value to the stream. More...
 
bool String (const Ch *str, SizeType length, bool copy=false)
 
bool StartObject ()
 
bool Key (const Ch *str, SizeType length, bool copy=false)
 
bool EndObject (SizeType memberCount=0)
 
bool StartArray ()
 
bool EndArray (SizeType elementCount=0)
 
bool String (const Ch *str)
 Simpler but slower overload. More...
 
bool Key (const Ch *str)
 

Protected Member Functions

void PrettyPrefix (Type type)
 
void WriteIndent ()
 
- Protected Member Functions inherited from rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >
bool WriteNull ()
 
bool WriteBool (bool b)
 
bool WriteInt (int i)
 
bool WriteUint (unsigned u)
 
bool WriteInt64 (int64_t i64)
 
bool WriteUint64 (uint64_t u64)
 
bool WriteDouble (double d)
 
bool WriteString (const Ch *str, SizeType length)
 
bool WriteStartObject ()
 
bool WriteEndObject ()
 
bool WriteStartArray ()
 
bool WriteEndArray ()
 
void Prefix (Type type)
 
template<>
bool WriteInt (int i)
 
template<>
bool WriteUint (unsigned u)
 
template<>
bool WriteInt64 (int64_t i64)
 
template<>
bool WriteUint64 (uint64_t u)
 
template<>
bool WriteDouble (double d)
 

Protected Attributes

Ch indentChar_
 
unsigned indentCharCount_
 
- Protected Attributes inherited from rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >
OutputStream * os_
 
internal::Stack< StackAllocator > level_stack_
 
bool hasRoot_
 

Private Member Functions

 PrettyWriter (const PrettyWriter &)
 
PrettyWriteroperator= (const PrettyWriter &)
 

Additional Inherited Members

- Static Protected Attributes inherited from rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >
static const size_t kDefaultLevelDepth = 32
 

Detailed Description

template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
class rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >

Writer with indentation and spacing.

Template Parameters
OutputStreamType of ouptut os.
SourceEncodingEncoding of source string.
TargetEncodingEncoding of output stream.
StackAllocatorType of allocator for allocating memory of stack.

Definition at line 41 of file prettywriter.h.

Member Typedef Documentation

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
typedef Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator> rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Base

Definition at line 43 of file prettywriter.h.

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
typedef Base::Ch rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Ch

Definition at line 44 of file prettywriter.h.

Constructor & Destructor Documentation

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::PrettyWriter ( OutputStream &  os,
StackAllocator *  allocator = 0,
size_t  levelDepth = Base::kDefaultLevelDepth 
)
inline

Constructor.

Parameters
osOutput stream.
allocatorUser supplied allocator. If it is null, it will create a private one.
levelDepthInitial capacity of stack.

Definition at line 51 of file prettywriter.h.

51  :
52  Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > Base
Definition: prettywriter.h:43
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::PrettyWriter ( const PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > &  )
private

Member Function Documentation

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Bool ( bool  b)
inline

Definition at line 72 of file prettywriter.h.

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Double ( double  d)
inline

Definition at line 77 of file prettywriter.h.

void PrettyPrefix(Type type)
Definition: prettywriter.h:144
bool WriteDouble(double d)
Definition: writer.h:230
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::EndArray ( SizeType  memberCount = 0)
inline

Definition at line 116 of file prettywriter.h.

116  {
117  (void)memberCount;
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;
121 
122  if (!empty) {
123  Base::os_->Put('\n');
124  WriteIndent();
125  }
126  if (!Base::WriteEndArray())
127  return false;
128  if (Base::level_stack_.Empty()) // end of json text
129  Base::os_->Flush();
130  return true;
131  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
Level
Definition: Level.h:13
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
OutputStream * os_
Definition: writer.h:331
bool WriteEndArray()
Definition: writer.h:309
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::EndObject ( SizeType  memberCount = 0)
inline

Definition at line 93 of file prettywriter.h.

93  {
94  (void)memberCount;
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;
98 
99  if (!empty) {
100  Base::os_->Put('\n');
101  WriteIndent();
102  }
103  if (!Base::WriteEndObject())
104  return false;
105  if (Base::level_stack_.Empty()) // end of json text
106  Base::os_->Flush();
107  return true;
108  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
Level
Definition: Level.h:13
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
OutputStream * os_
Definition: writer.h:331
bool WriteEndObject()
Definition: writer.h:307
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Int ( int  i)
inline

Definition at line 73 of file prettywriter.h.

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Int64 ( int64_t  i64)
inline

Definition at line 75 of file prettywriter.h.

sqlite_int64 i64
Definition: tkeyvfs.cc:34
void PrettyPrefix(Type type)
Definition: prettywriter.h:144
bool WriteInt64(int64_t i64)
Definition: writer.h:214
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Key ( const Ch str,
SizeType  length,
bool  copy = false 
)
inline

Definition at line 91 of file prettywriter.h.

91 { return String(str, length, copy); }
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:79
T copy(T const &v)
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Key ( const Ch str)
inline

Definition at line 140 of file prettywriter.h.

140 { return Key(str, internal::StrLen(str)); }
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:34
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:91
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Null ( )
inline

Definition at line 71 of file prettywriter.h.

void PrettyPrefix(Type type)
Definition: prettywriter.h:144
bool WriteNull()
Definition: writer.h:184
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
PrettyWriter& rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::operator= ( const PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > &  )
private
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
void rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::PrettyPrefix ( Type  type)
inlineprotected

Definition at line 144 of file prettywriter.h.

144  {
145  (void)type;
146  if (Base::level_stack_.GetSize() != 0) { // this value is not at root
147  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
148 
149  if (level->inArray) {
150  if (level->valueCount > 0) {
151  Base::os_->Put(','); // add comma if it is not the first element in array
152  Base::os_->Put('\n');
153  }
154  else
155  Base::os_->Put('\n');
156  WriteIndent();
157  }
158  else { // in object
159  if (level->valueCount > 0) {
160  if (level->valueCount % 2 == 0) {
161  Base::os_->Put(',');
162  Base::os_->Put('\n');
163  }
164  else {
165  Base::os_->Put(':');
166  Base::os_->Put(' ');
167  }
168  }
169  else
170  Base::os_->Put('\n');
171 
172  if (level->valueCount % 2 == 0)
173  WriteIndent();
174  }
175  if (!level->inArray && level->valueCount % 2 == 0)
176  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
177  level->valueCount++;
178  }
179  else {
180  RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root.
181  Base::hasRoot_ = true;
182  }
183  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
Level
Definition: Level.h:13
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
OutputStream * os_
Definition: writer.h:331
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
PrettyWriter& rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::SetIndent ( Ch  indentChar,
unsigned  indentCharCount 
)
inline

Set custom indentation.

Parameters
indentCharCharacter for indentation. Must be whitespace character (' ', '\t', '\n', '\r').
indentCharCountNumber of indent characters for each indentation level.
Note
The default indentation is 4 spaces.

Definition at line 59 of file prettywriter.h.

59  {
60  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r');
61  indentChar_ = indentChar;
62  indentCharCount_ = indentCharCount;
63  return *this;
64  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::StartArray ( )
inline

Definition at line 110 of file prettywriter.h.

110  {
112  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
113  return Base::WriteStartArray();
114  }
Level
Definition: Level.h:13
void PrettyPrefix(Type type)
Definition: prettywriter.h:144
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
bool WriteStartArray()
Definition: writer.h:308
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::StartObject ( )
inline

Definition at line 85 of file prettywriter.h.

85  {
87  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
88  return Base::WriteStartObject();
89  }
Level
Definition: Level.h:13
void PrettyPrefix(Type type)
Definition: prettywriter.h:144
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
bool WriteStartObject()
Definition: writer.h:306
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::String ( const Ch str,
SizeType  length,
bool  copy = false 
)
inline

Definition at line 79 of file prettywriter.h.

79  {
80  (void)copy;
82  return Base::WriteString(str, length);
83  }
void PrettyPrefix(Type type)
Definition: prettywriter.h:144
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:238
T copy(T const &v)
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::String ( const Ch str)
inline

Simpler but slower overload.

Definition at line 139 of file prettywriter.h.

139 { return String(str, internal::StrLen(str)); }
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:79
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:34
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Uint ( unsigned  u)
inline

Definition at line 74 of file prettywriter.h.

bool WriteUint(unsigned u)
Definition: writer.h:206
void PrettyPrefix(Type type)
Definition: prettywriter.h:144
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Uint64 ( uint64_t  u64)
inline

Definition at line 76 of file prettywriter.h.

bool WriteUint64(uint64_t u64)
Definition: writer.h:222
void PrettyPrefix(Type type)
Definition: prettywriter.h:144
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
void rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteIndent ( )
inlineprotected

Definition at line 185 of file prettywriter.h.

185  {
186  size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
187  PutN(*Base::os_, indentChar_, count);
188  }
Level
Definition: Level.h:13
size_t GetSize() const
Definition: stack.h:133
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
OutputStream * os_
Definition: writer.h:331

Member Data Documentation

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
Ch rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::indentChar_
protected

Definition at line 190 of file prettywriter.h.

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
unsigned rapidjson::PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::indentCharCount_
protected

Definition at line 191 of file prettywriter.h.


The documentation for this class was generated from the following file: