rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags > Class Template Reference

JSON writer. More...

#include <writer.h>

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

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...
 
int GetMaxDecimalPlaces () const
 
void SetMaxDecimalPlaces (int maxDecimalPlaces)
 Sets the maximum number of decimal places for double output. 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 RawNumber (const Ch *str, SizeType length, bool copy=false)
 
bool String (const Ch *str, SizeType length, bool copy=false)
 
bool String (const std::basic_string< Ch > &str)
 
bool StartObject ()
 
bool Key (const Ch *str, SizeType length, bool copy=false)
 
bool Key (const std::basic_string< Ch > &str)
 
bool EndObject (SizeType memberCount=0)
 
bool StartArray ()
 
bool EndArray (SizeType elementCount=0)
 

Static Public Attributes

static const int kDefaultMaxDecimalPlaces = 324
 

Protected Member Functions

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)
 

Convenience extensions

static const size_t kDefaultLevelDepth = 32
 
OutputStream * os_
 
internal::Stack< StackAllocator > level_stack_
 
int maxDecimalPlaces_
 
bool hasRoot_
 
bool String (const Ch *const &str)
 Simpler but slower overload.
 
bool Key (const Ch *const &str)
 
bool RawValue (const Ch *json, size_t length, Type type)
 Write a raw JSON value. More...
 
void Flush ()
 Flush the output stream. More...
 
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 ScanWriteUnescapedString (GenericStringStream< SourceEncoding > &is, size_t length)
 
bool WriteStartObject ()
 
bool WriteEndObject ()
 
bool WriteStartArray ()
 
bool WriteEndArray ()
 
bool WriteRawValue (const Ch *json, size_t length)
 
void Prefix (Type type)
 
bool EndValue (bool ret)
 

Detailed Description

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

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

Constructor & Destructor Documentation

◆ Writer()

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >::Writer ( OutputStream &  os,
StackAllocator *  stackAllocator = 0,
size_t  levelDepth = kDefaultLevelDepth 
)
inlineexplicit

Constructor.

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

Member Function Documentation

◆ Double()

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

Writes the given double value to the stream.

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

◆ Flush()

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
void rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >::Flush ( )
inline

Flush the output stream.

Allows the user to flush the output stream immediately.

◆ IsComplete()

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >::IsComplete ( ) const
inline

Checks whether the output is a complete JSON.

A complete JSON has a complete root object or array.

◆ RawValue()

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
bool rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >::RawValue ( const Ch *  json,
size_t  length,
Type  type 
)
inline

Write a raw JSON value.

For user to write a stringified JSON as a value.

Parameters
jsonA well-formed JSON value. It should not contain null character within [0, length - 1] range.
lengthLength of the json.
typeType of the root of json.

◆ Reset()

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
void rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >::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();

◆ SetMaxDecimalPlaces()

template<typename OutputStream , typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
void rapidjson::Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >::SetMaxDecimalPlaces ( int  maxDecimalPlaces)
inline

Sets the maximum number of decimal places for double output.

This setting truncates the output with specified number of decimal places.

For example,

writer.SetMaxDecimalPlaces(3);
writer.StartArray();
writer.Double(0.12345); // "0.123"
writer.Double(0.0001); // "0.0"
writer.Double(1.234567890123456e30); // "1.234567890123456e30" (do not truncate significand for positive exponent)
writer.Double(1.23e-4); // "0.0" (do truncate significand for negative exponent)
writer.EndArray();

The default setting does not truncate any decimal places. You can restore to this setting by calling

writer.SetMaxDecimalPlaces(Writer::kDefaultMaxDecimalPlaces);

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