Writer
Engine/source/persistence/rapidjson/writer.h
JSON writer.
Classes:
Information for each nested level.
Convenience extensions
bool
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
bool
bool
bool
bool
WriteRawValue(const Ch * json, size_t length)
bool
EndValue(bool ret)
Implementation of Handler
Handler
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)
int
bool
Checks whether the output is a complete JSON.
SetMaxDecimalPlaces(int maxDecimalPlaces)
Sets the maximum number of decimal places for double output.
Protected Functions
bool
WriteDouble(double d)
bool
WriteInt(int i)
bool
WriteInt64(int64_t i64)
bool
WriteUint(unsigned u)
bool
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
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
doublevalue to the stream.
Parameters:
d | The value to be written. |
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)