Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MaximInterface
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 | |
| |
| 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:
-
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
Definition at line 88 of file writer.h.
Constructor & Destructor Documentation
| Writer | ( | OutputStream & | os, |
| StackAllocator * | stackAllocator = 0, |
||
| size_t | levelDepth = kDefaultLevelDepth |
||
| ) | [explicit] |
Member Function Documentation
| bool Double | ( | double | d ) |
Writes the given double value to the stream.
- Parameters:
-
d The value to be written.
- Returns:
- Whether it is succeed.
Reimplemented in PrettyWriter< OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags >.
| bool IsComplete | ( | ) | const |
| bool RawValue | ( | const Ch * | json, |
| size_t | length, | ||
| Type | type | ||
| ) |
| 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:
-
os New output stream. Writer<OutputStream> writer(os1); writer.StartObject(); // ... writer.EndObject(); writer.Reset(os2); writer.StartObject(); // ... writer.EndObject();
| 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);
Generated on Tue Jul 12 2022 12:06:51 by
1.7.2