DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

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

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

JSON writer. More...

#include <writer.h>

Inherited by PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >.

Data Structures

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

Public Member Functions

 Writer (OutputStream &os, StackAllocator *stackAllocator=0, size_t levelDepth=kDefaultLevelDepth)
 Constructor.
void Reset (OutputStream &os)
 Reset the writer with a new stream.
bool IsComplete () const
 Checks whether the output is a complete JSON.
void SetMaxDecimalPlaces (int maxDecimalPlaces)
 Sets the maximum number of decimal places for double output.
bool RawValue (const Ch *json, size_t length, Type type)
 Write a raw JSON value.
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.
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 EndObject (SizeType memberCount=0)
bool StartArray ()
bool EndArray (SizeType elementCount=0)
Convenience extensions
bool String (const Ch *str)
 Simpler but slower overload.
bool Key (const Ch *str)

Detailed Description

template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
class 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

Definition at line 88 of file writer.h.


Constructor & Destructor Documentation

Writer ( OutputStream &  os,
StackAllocator *  stackAllocator = 0,
size_t  levelDepth = kDefaultLevelDepth 
) [explicit]

Constructor.

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

Definition at line 100 of file writer.h.


Member Function Documentation

bool Double ( double  d )

Writes the given double value to the stream.

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

Reimplemented in PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >.

Definition at line 192 of file writer.h.

bool IsComplete (  ) const

Checks whether the output is a complete JSON.

A complete JSON has a complete root object or array.

Definition at line 142 of file writer.h.

bool 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:
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.

Definition at line 262 of file writer.h.

void 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:
osNew output stream.

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

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

Definition at line 132 of file writer.h.

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

Definition at line 171 of file writer.h.

bool String ( const Ch *  str )

Simpler but slower overload.

Definition at line 249 of file writer.h.