Writer

Engine/source/persistence/rapidjson/writer.h

JSON writer.

More...

Classes:

class

Information for each nested level.

Convenience extensions

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.

Flush the output stream.

bool
bool
WriteBool(bool b)
bool
WriteInt(int i)
bool
WriteUint(unsigned u)
bool
bool
WriteDouble(double d)
bool
WriteString(const Ch * str, SizeType length)
bool
ScanWriteUnescapedString(GenericStringStream< SourceEncoding > & is, size_t length)
bool
WriteRawValue(const Ch * json, size_t length)
bool
EndValue(bool ret)
OutputStream *
internal::Stack< StackAllocator >
bool

Implementation of Handler

see:

Handler

bool
Null()
bool
Bool(bool b)
bool
Int(int i)
bool
Uint(unsigned u)
bool
bool
bool
Double(double d)

Writes the given

double
value to the stream.

bool
RawNumber(const Ch * str, SizeType length, bool copy)
bool
String(const Ch * str, SizeType length, bool copy)
bool
bool
Key(const Ch * str, SizeType length, bool copy)
bool
EndObject(SizeType memberCount)
bool
bool
EndArray(SizeType elementCount)

Public Types

SourceEncoding::Ch
Ch 

Public Static Attributes

Public Functions

Writer(OutputStream & os, StackAllocator * stackAllocator, size_t levelDepth)

Constructor.

Writer(StackAllocator * allocator, size_t levelDepth)
bool

Checks whether the output is a complete JSON.

Reset(OutputStream & os)

Reset the writer with a new stream.

SetMaxDecimalPlaces(int maxDecimalPlaces)

Sets the maximum number of decimal places for double output.

Protected Functions

bool
WriteDouble(double d)
bool
WriteInt(int i)
bool
bool
WriteUint(unsigned u)

Detailed Description

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().

Parameters:

OutputStream

Type of output stream.

SourceEncoding

Encoding of source string.

TargetEncoding

Encoding of output stream.

StackAllocator

Type of allocator for allocating memory of stack.

note:

implements Handler concept

Convenience extensions

String(const Ch *const & str)

Simpler but slower overload.

Key(const Ch *const & str)

RawValue(const Ch * json, size_t length, Type type)

Write a raw JSON value.

For user to write a stringified JSON as a value.

Parameters:

json

A well-formed JSON value. It should not contain null character within [0, length - 1] range.

length

Length of the json.

type

Type of the root of json.

Flush()

Flush the output stream.

Allows the user to flush the output stream immediately.

WriteNull()

WriteBool(bool b)

WriteInt(int i)

WriteUint(unsigned u)

WriteInt64(int64_t i64)

WriteUint64(uint64_t u64)

WriteDouble(double d)

WriteString(const Ch * str, SizeType length)

ScanWriteUnescapedString(GenericStringStream< SourceEncoding > & is, size_t length)

WriteStartObject()

WriteEndObject()

WriteStartArray()

WriteEndArray()

WriteRawValue(const Ch * json, size_t length)

Prefix(Type type)

EndValue(bool ret)

Writer(const Writer & )

operator=(const Writer & )

const size_t kDefaultLevelDepth 
OutputStream * os_ 
internal::Stack< StackAllocator > level_stack_ 
int maxDecimalPlaces_ 
bool hasRoot_ 

Implementation of Handler

see:

Handler

Null()

Bool(bool b)

Int(int i)

Uint(unsigned u)

Int64(int64_t i64)

Uint64(uint64_t u64)

Double(double d)

Writes the given

double
value to the stream.

Parameters:

d

The value to be written.

return:

Whether it is succeed.

RawNumber(const Ch * str, SizeType length, bool copy)

String(const Ch * str, SizeType length, bool copy)

StartObject()

Key(const Ch * str, SizeType length, bool copy)

EndObject(SizeType memberCount)

StartArray()

EndArray(SizeType elementCount)

Public Types

typedef SourceEncoding::Ch Ch 

Public Static Attributes

const int kDefaultMaxDecimalPlaces 

Public Functions

Writer(OutputStream & os, StackAllocator * stackAllocator, size_t levelDepth)

Constructor.

Parameters:

os

Output stream.

stackAllocator

User supplied allocator. If it is null, it will create a private one.

levelDepth

Initial capacity of stack.

Writer(StackAllocator * allocator, size_t levelDepth)

GetMaxDecimalPlaces()

IsComplete()

Checks whether the output is a complete JSON.

A complete JSON has a complete root object or array.

Reset(OutputStream & os)

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:

os

New output stream.

Writer<OutputStream> writer(os1);
writer.StartObject();
// ...
writer.EndObject();

writer.Reset(os2);
writer.StartObject();
// ...
writer.EndObject();

SetMaxDecimalPlaces(int maxDecimalPlaces)

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);

Protected Functions

WriteDouble(double d)

WriteInt(int i)

WriteInt64(int64_t i64)

WriteUint(unsigned u)

WriteUint64(uint64_t u)