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

JSON writer. More...

#include <writer.h>

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

Classes

struct  Level
 Information for each nested level. More...
 

Public Types

typedef SourceEncoding::Ch Ch
 

Public Member Functions

 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...
 
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)
 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)
 
Convenience extensions
bool String (const Ch *str)
 Simpler but slower overload. More...
 
bool Key (const Ch *str)
 

Protected Member Functions

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

OutputStream * os_
 
internal::Stack< StackAllocator > level_stack_
 
bool hasRoot_
 

Static Protected Attributes

static const size_t kDefaultLevelDepth = 32
 

Private Member Functions

 Writer (const Writer &)
 
Writeroperator= (const Writer &)
 

Detailed Description

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

JSON writer.

Writer implements the concept Handler. It generates JSON text by events to an output os.

User may programmatically calls the functions of a writer to generate JSON text.

On the other side, a writer can also be passed to objects that generates events,

for example Reader::Parse() and Document::Accept().

Template Parameters
OutputStreamType of output stream.
SourceEncodingEncoding of source string.
TargetEncodingEncoding of output stream.
StackAllocatorType of allocator for allocating memory of stack.
Note
implements Handler concept

Definition at line 56 of file writer.h.

Member Typedef Documentation

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

Definition at line 58 of file writer.h.

Constructor & Destructor Documentation

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Writer ( OutputStream &  os,
StackAllocator *  stackAllocator = 0,
size_t  levelDepth = 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 65 of file writer.h.

65  :
66  os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), hasRoot_(false) {}
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>
rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Writer ( StackAllocator *  allocator = 0,
size_t  levelDepth = kDefaultLevelDepth 
)
inline

Definition at line 68 of file writer.h.

68  :
69  os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), hasRoot_(false) {}
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>
rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Writer ( const Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > &  )
private

Member Function Documentation

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

Definition at line 109 of file writer.h.

109 { Prefix(b ? kTrueType : kFalseType); return WriteBool(b); }
void Prefix(Type type)
Definition: writer.h:311
bool WriteBool(bool b)
Definition: writer.h:188
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Double ( double  d)
inline

Writes the given double value to the stream.

Parameters
dThe value to be written.
Returns
Whether it is succeed.

Definition at line 120 of file writer.h.

120 { Prefix(kNumberType); return WriteDouble(d); }
bool WriteDouble(double d)
Definition: writer.h:230
void Prefix(Type type)
Definition: writer.h:311
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::EndArray ( SizeType  elementCount = 0)
inline

Definition at line 153 of file writer.h.

153  {
154  (void)elementCount;
156  RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
157  level_stack_.template Pop<Level>(1);
158  bool ret = WriteEndArray();
159  if (level_stack_.Empty()) // end of json text
160  os_->Flush();
161  return ret;
162  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
Level
Definition: Level.h:13
size_t GetSize() const
Definition: stack.h:133
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
bool Empty() const
Definition: stack.h:132
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::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::EndObject ( SizeType  memberCount = 0)
inline

Definition at line 136 of file writer.h.

136  {
137  (void)memberCount;
139  RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray);
140  level_stack_.template Pop<Level>(1);
141  bool ret = WriteEndObject();
142  if (level_stack_.Empty()) // end of json text
143  os_->Flush();
144  return ret;
145  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
Level
Definition: Level.h:13
size_t GetSize() const
Definition: stack.h:133
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
bool Empty() const
Definition: stack.h:132
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::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Int ( int  i)
inline

Definition at line 110 of file writer.h.

110 { Prefix(kNumberType); return WriteInt(i); }
bool WriteInt(int i)
Definition: writer.h:198
void Prefix(Type type)
Definition: writer.h:311
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Int64 ( int64_t  i64)
inline

Definition at line 112 of file writer.h.

112 { Prefix(kNumberType); return WriteInt64(i64); }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
bool WriteInt64(int64_t i64)
Definition: writer.h:214
void Prefix(Type type)
Definition: writer.h:311
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::IsComplete ( ) const
inline

Checks whether the output is a complete JSON.

A complete JSON has a complete root object or array.

Definition at line 99 of file writer.h.

99  {
100  return hasRoot_ && level_stack_.Empty();
101  }
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
bool Empty() const
Definition: stack.h:132
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Key ( const Ch str,
SizeType  length,
bool  copy = false 
)
inline

Definition at line 134 of file writer.h.

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

Definition at line 170 of file writer.h.

170 { 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: writer.h:134
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Null ( )
inline

Definition at line 108 of file writer.h.

108 { Prefix(kNullType); return WriteNull(); }
bool WriteNull()
Definition: writer.h:184
void Prefix(Type type)
Definition: writer.h:311
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
Writer& rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::operator= ( const Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > &  )
private
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
void rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Prefix ( Type  type)
inlineprotected

Definition at line 311 of file writer.h.

311  {
312  (void)type;
313  if (level_stack_.GetSize() != 0) { // this value is not at root
314  Level* level = level_stack_.template Top<Level>();
315  if (level->valueCount > 0) {
316  if (level->inArray)
317  os_->Put(','); // add comma if it is not the first element in array
318  else // in object
319  os_->Put((level->valueCount % 2 == 0) ? ',' : ':');
320  }
321  if (!level->inArray && level->valueCount % 2 == 0)
322  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
323  level->valueCount++;
324  }
325  else {
326  RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root.
327  hasRoot_ = true;
328  }
329  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269
Level
Definition: Level.h:13
size_t GetSize() const
Definition: stack.h:133
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>
void rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Reset ( OutputStream &  os)
inline

Reset the writer with a new stream.

This function reset the writer with a new stream and default settings, in order to make a Writer object reusable for output multiple JSONs.

Parameters
osNew output stream.
Writer<OutputStream> writer(os1);
writer.StartObject();
// ...
writer.EndObject();
writer.Reset(os2);
writer.StartObject();
// ...
writer.EndObject();

Definition at line 89 of file writer.h.

89  {
90  os_ = &os;
91  hasRoot_ = false;
93  }
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>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::StartArray ( )
inline

Definition at line 147 of file writer.h.

147  {
149  new (level_stack_.template Push<Level>()) Level(true);
150  return WriteStartArray();
151  }
Level
Definition: Level.h:13
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
void Prefix(Type type)
Definition: writer.h:311
bool WriteStartArray()
Definition: writer.h:308
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::StartObject ( )
inline

Definition at line 128 of file writer.h.

128  {
130  new (level_stack_.template Push<Level>()) Level(false);
131  return WriteStartObject();
132  }
Level
Definition: Level.h:13
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:332
void Prefix(Type type)
Definition: writer.h:311
bool WriteStartObject()
Definition: writer.h:306
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::String ( const Ch str,
SizeType  length,
bool  copy = false 
)
inline

Definition at line 122 of file writer.h.

122  {
123  (void)copy;
125  return WriteString(str, length);
126  }
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:238
void Prefix(Type type)
Definition: writer.h:311
T copy(T const &v)
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::String ( const Ch str)
inline

Simpler but slower overload.

Definition at line 169 of file writer.h.

169 { return String(str, internal::StrLen(str)); }
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: writer.h:122
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::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Uint ( unsigned  u)
inline

Definition at line 111 of file writer.h.

111 { Prefix(kNumberType); return WriteUint(u); }
bool WriteUint(unsigned u)
Definition: writer.h:206
void Prefix(Type type)
Definition: writer.h:311
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::Uint64 ( uint64_t  u64)
inline

Definition at line 113 of file writer.h.

113 { Prefix(kNumberType); return WriteUint64(u64); }
bool WriteUint64(uint64_t u64)
Definition: writer.h:222
void Prefix(Type type)
Definition: writer.h:311
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteBool ( bool  b)
inlineprotected

Definition at line 188 of file writer.h.

188  {
189  if (b) {
190  os_->Put('t'); os_->Put('r'); os_->Put('u'); os_->Put('e');
191  }
192  else {
193  os_->Put('f'); os_->Put('a'); os_->Put('l'); os_->Put('s'); os_->Put('e');
194  }
195  return true;
196  }
OutputStream * os_
Definition: writer.h:331
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteDouble ( double  d)
inlineprotected

Definition at line 230 of file writer.h.

230  {
231  char buffer[25];
232  char* end = internal::dtoa(d, buffer);
233  for (char* p = buffer; p != end; ++p)
234  os_->Put(*p);
235  return true;
236  }
char * dtoa(double value, char *buffer)
Definition: dtoa.h:393
p
Definition: test.py:228
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<>
bool rapidjson::Writer< StringBuffer >::WriteDouble ( double  d)
inlineprotected

Definition at line 376 of file writer.h.

376  {
377  char *buffer = os_->Push(25);
378  char* end = internal::dtoa(d, buffer);
379  os_->Pop(25 - (end - buffer));
380  return true;
381 }
char * dtoa(double value, char *buffer)
Definition: dtoa.h:393
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteEndArray ( )
inlineprotected

Definition at line 309 of file writer.h.

309 { os_->Put(']'); return true; }
OutputStream * os_
Definition: writer.h:331
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteEndObject ( )
inlineprotected

Definition at line 307 of file writer.h.

307 { os_->Put('}'); return true; }
OutputStream * os_
Definition: writer.h:331
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteInt ( int  i)
inlineprotected

Definition at line 198 of file writer.h.

198  {
199  char buffer[11];
200  const char* end = internal::i32toa(i, buffer);
201  for (const char* p = buffer; p != end; ++p)
202  os_->Put(*p);
203  return true;
204  }
char * i32toa(int32_t value, char *buffer)
Definition: itoa.h:117
p
Definition: test.py:228
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<>
bool rapidjson::Writer< StringBuffer >::WriteInt ( int  i)
inlineprotected

Definition at line 344 of file writer.h.

344  {
345  char *buffer = os_->Push(11);
346  const char* end = internal::i32toa(i, buffer);
347  os_->Pop(11 - (end - buffer));
348  return true;
349 }
char * i32toa(int32_t value, char *buffer)
Definition: itoa.h:117
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteInt64 ( int64_t  i64)
inlineprotected

Definition at line 214 of file writer.h.

214  {
215  char buffer[21];
216  const char* end = internal::i64toa(i64, buffer);
217  for (const char* p = buffer; p != end; ++p)
218  os_->Put(*p);
219  return true;
220  }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
char * i64toa(int64_t value, char *buffer)
Definition: itoa.h:294
p
Definition: test.py:228
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<>
bool rapidjson::Writer< StringBuffer >::WriteInt64 ( int64_t  i64)
inlineprotected

Definition at line 360 of file writer.h.

360  {
361  char *buffer = os_->Push(21);
362  const char* end = internal::i64toa(i64, buffer);
363  os_->Pop(21 - (end - buffer));
364  return true;
365 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
char * i64toa(int64_t value, char *buffer)
Definition: itoa.h:294
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteNull ( )
inlineprotected

Definition at line 184 of file writer.h.

184  {
185  os_->Put('n'); os_->Put('u'); os_->Put('l'); os_->Put('l'); return true;
186  }
OutputStream * os_
Definition: writer.h:331
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteStartArray ( )
inlineprotected

Definition at line 308 of file writer.h.

308 { os_->Put('['); return true; }
OutputStream * os_
Definition: writer.h:331
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteStartObject ( )
inlineprotected

Definition at line 306 of file writer.h.

306 { os_->Put('{'); return true; }
OutputStream * os_
Definition: writer.h:331
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteString ( const Ch str,
SizeType  length 
)
inlineprotected

Definition at line 238 of file writer.h.

238  {
239  static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
240  static const char escape[256] = {
241 #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
242  //0 1 2 3 4 5 6 7 8 9 A B C D E F
243  'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00
244  'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10
245  0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20
246  Z16, Z16, // 30~4F
247  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50
248  Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF
249 #undef Z16
250  };
251 
252  os_->Put('\"');
253  GenericStringStream<SourceEncoding> is(str);
254  while (is.Tell() < length) {
255  const Ch c = is.Peek();
256  if (!TargetEncoding::supportUnicode && (unsigned)c >= 0x80) {
257  // Unicode escaping
258  unsigned codepoint;
259  if (!SourceEncoding::Decode(is, &codepoint))
260  return false;
261  os_->Put('\\');
262  os_->Put('u');
263  if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) {
264  os_->Put(hexDigits[(codepoint >> 12) & 15]);
265  os_->Put(hexDigits[(codepoint >> 8) & 15]);
266  os_->Put(hexDigits[(codepoint >> 4) & 15]);
267  os_->Put(hexDigits[(codepoint ) & 15]);
268  }
269  else if (codepoint >= 0x010000 && codepoint <= 0x10FFFF) {
270  // Surrogate pair
271  unsigned s = codepoint - 0x010000;
272  unsigned lead = (s >> 10) + 0xD800;
273  unsigned trail = (s & 0x3FF) + 0xDC00;
274  os_->Put(hexDigits[(lead >> 12) & 15]);
275  os_->Put(hexDigits[(lead >> 8) & 15]);
276  os_->Put(hexDigits[(lead >> 4) & 15]);
277  os_->Put(hexDigits[(lead ) & 15]);
278  os_->Put('\\');
279  os_->Put('u');
280  os_->Put(hexDigits[(trail >> 12) & 15]);
281  os_->Put(hexDigits[(trail >> 8) & 15]);
282  os_->Put(hexDigits[(trail >> 4) & 15]);
283  os_->Put(hexDigits[(trail ) & 15]);
284  }
285  else
286  return false; // invalid code point
287  }
288  else if ((sizeof(Ch) == 1 || (unsigned)c < 256) && escape[(unsigned char)c]) {
289  is.Take();
290  os_->Put('\\');
291  os_->Put(escape[(unsigned char)c]);
292  if (escape[(unsigned char)c] == 'u') {
293  os_->Put('0');
294  os_->Put('0');
295  os_->Put(hexDigits[(unsigned char)c >> 4]);
296  os_->Put(hexDigits[(unsigned char)c & 0xF]);
297  }
298  }
299  else
301  }
302  os_->Put('\"');
303  return true;
304  }
SourceEncoding::Ch Ch
Definition: writer.h:58
#define Z16
OutputStream * os_
Definition: writer.h:331
static const double s
Definition: Units.h:99
static RAPIDJSON_FORCEINLINE bool Transcode(InputStream &is, OutputStream &os)
Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the outp...
Definition: encodings.h:594
std::string escape(std::string const &str)
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteUint ( unsigned  u)
inlineprotected

Definition at line 206 of file writer.h.

206  {
207  char buffer[10];
208  const char* end = internal::u32toa(u, buffer);
209  for (const char* p = buffer; p != end; ++p)
210  os_->Put(*p);
211  return true;
212  }
char * u32toa(uint32_t value, char *buffer)
Definition: itoa.h:43
p
Definition: test.py:228
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<>
bool rapidjson::Writer< StringBuffer >::WriteUint ( unsigned  u)
inlineprotected

Definition at line 352 of file writer.h.

352  {
353  char *buffer = os_->Push(10);
354  const char* end = internal::u32toa(u, buffer);
355  os_->Pop(10 - (end - buffer));
356  return true;
357 }
char * u32toa(uint32_t value, char *buffer)
Definition: itoa.h:43
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::WriteUint64 ( uint64_t  u64)
inlineprotected

Definition at line 222 of file writer.h.

222  {
223  char buffer[20];
224  char* end = internal::u64toa(u64, buffer);
225  for (char* p = buffer; p != end; ++p)
226  os_->Put(*p);
227  return true;
228  }
char * u64toa(uint64_t value, char *buffer)
Definition: itoa.h:126
p
Definition: test.py:228
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8
template<>
bool rapidjson::Writer< StringBuffer >::WriteUint64 ( uint64_t  u)
inlineprotected

Definition at line 368 of file writer.h.

368  {
369  char *buffer = os_->Push(20);
370  const char* end = internal::u64toa(u, buffer);
371  os_->Pop(20 - (end - buffer));
372  return true;
373 }
char * u64toa(uint64_t value, char *buffer)
Definition: itoa.h:126
OutputStream * os_
Definition: writer.h:331
end
Definition: test.py:8

Member Data Documentation

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::hasRoot_
protected

Definition at line 333 of file writer.h.

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
const size_t rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::kDefaultLevelDepth = 32
staticprotected

Definition at line 182 of file writer.h.

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
internal::Stack<StackAllocator> rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::level_stack_
protected

Definition at line 332 of file writer.h.

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator>
OutputStream* rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator >::os_
protected

Definition at line 331 of file writer.h.


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