The JSON document writer. More...
Public Member Functions | |
wxJSONWriter (int style=wxJSONWRITER_STYLED, int indent=0, int step=3) | |
Ctor. More... | |
~wxJSONWriter () | |
Dtor - does nothing. | |
void | Write (const wxJSONValue &value, wxString &str) |
Write the JSONvalue object to a JSON text. More... | |
void | Write (const wxJSONValue &value, wxOutputStream &os) |
void | SetDoubleFmtString (const char *fmt) |
Set the format string for double values. More... | |
Protected Member Functions | |
int | DoWrite (wxOutputStream &os, const wxJSONValue &value, const wxString *key, bool comma) |
Perform the real write operation. More... | |
int | WriteIndent (wxOutputStream &os) |
Writes the indentation to the JSON text. More... | |
int | WriteIndent (wxOutputStream &os, int num) |
Write the specified number of indentation (spaces or tabs) More... | |
bool | IsSpace (wxChar ch) |
Returns TRUE if the character is a space character. | |
bool | IsPunctuation (wxChar ch) |
Returns TRUE if the character if a puctuation character. | |
int | WriteString (wxOutputStream &os, const wxString &str) |
Write a generic string. More... | |
int | WriteStringValue (wxOutputStream &os, const wxString &str) |
Write the provided string to the output object. More... | |
int | WriteNullValue (wxOutputStream &os) |
Write the NULL JSON value to the output stream. More... | |
int | WriteIntValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type INT. More... | |
int | WriteUIntValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type UNSIGNED INT. More... | |
int | WriteBoolValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type BOOL. More... | |
int | WriteDoubleValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type DOUBLE. More... | |
int | WriteMemoryBuff (wxOutputStream &os, const wxMemoryBuffer &buff) |
Write a JSON value of type memory buffer. More... | |
int | WriteInvalid (wxOutputStream &os) |
Write the invalid JSON value to the output stream. More... | |
int | WriteSeparator (wxOutputStream &os) |
Writes the separator between values. More... | |
int | WriteKey (wxOutputStream &os, const wxString &key) |
Write the key of a key/value element to the output stream. | |
int | WriteComment (wxOutputStream &os, const wxJSONValue &value, bool indent) |
Write the comment strings, if any. | |
int | WriteError (const wxString &err) |
The JSON document writer.
This class is a JSON document writer and it is used to write a wxJSONValue object to an output stream or to a string object. The ctor accepts some parameters which can be used to change the style of the output. The default output is in human-readable format that uses a three-space indentation for object / array sub-items and separates every value with a linefeed character.
Using the default writer constructor
To write a JSON value object using a four-spaces indentation and forcing all comment strings to apear before the value they refer to, use the following code:
The following code construct a JSON writer that produces the most compact text output but it is hard to read by humans:
You can write JSON text to two different kind of objects:
When writing to a string object, the output is platform- and mode-dependent. In ANSI builds, the JSON text output in the string object will contain one-byte characters: the actual characters represented is locale dependent. In Unicode builds, the JSON text output in the string contains wide characters which encoding format is platform dependent: UCS-2 in Windows, UCS-4 in GNU/Linux. Starting from wxWidgets version 2.9 the internal encoding for Unicode builds in linux/unix systems is UTF-8.
When writing to a stream object, the JSON text output is always encoded in UTF-8 in both ANSI and Unicode builds. In ANSI builds the user may want to suppress UTF-8 encoding so that the JSON text can be stored in ANSI format. Note that this is not valid JSON text unless all characters written to the JSON text document are in the US-ASCII character ser (0x00..0x7F). To know more read wxjson_tutorial_unicode_ansi
In versions up to 1.0 the JSON writer wrote every character to the output object (the string or the stream). This is very inefficient becuase the writer converted each char to UTF-8 when writing to streams but we have to note that only string values have to be actually converted. Special JSON characters, numbers and literals do not need the conversion because they lay in the US-ASCII plane (0x00-0x7F) and no conversion is needed as the UTF-8 encoding is the same as US-ASCII.
For more info about the unicode topic see wxjson_tutorial_unicode.
You can customize the ouput of doubles by specifing the format string that has to be used by the JSON writer class. To know more about this issue read wxjson_tutorial_write_doubles
wxJSONWriter::wxJSONWriter | ( | int | style = wxJSONWRITER_STYLED , |
int | indent = 0 , |
||
int | step = 3 |
||
) |
Ctor.
Construct the JSON writer object with the specified parameters. Note that if styled
is FALSE the indentation is totally suppressed and the values of the other two parameters are simply ignored.
indent | the initial indentation in number of spaces. Default is ZERO. If you specify the wxJSONWRITER_TAB_INDENT flag for the style, this value referes to the number of TABs in the initial indentation |
step | the indentation increment for new objects/arrays in number of spaces (default is 3). This value is ignored if you specify the wxJSONWRITER_TAB_INDENT flag for the style: the indentation increment is only one TAB character. |
style | this is a combination of the following constants OR'ed togheter:
|
|
protected |
Perform the real write operation.
This is a recursive function that gets the type of the value
object and calls several protected functions depending on the type:
WriteNullvalue
for type NULL WriteStringValue()
for STRING and CSTRING types WriteIntValue
for INT types WriteUIntValue
for UINT types WriteBoolValue
for BOOL types WriteDoubleValue
for DOUBLE types WriteMemoryBuff
for MEMORYBUFF typesIf the value is an array or key/value map (types ARRAY and OBJECT), the function iterates through all JSON value object in the array/map and calls itself for every item in the container.
void wxJSONWriter::SetDoubleFmtString | ( | const char * | fmt | ) |
Set the format string for double values.
This function sets the format string used for printing double values. Double values are outputted to JSON text using the snprintf function with a default format string of:
which prints doubles with a precision of 10 decimal digits and suppressing trailing ZEROes.
Note that the parameter is a pointer to char and not to wxChar. This is because the JSON writer always procudes UTF-8 encoded text and decimal digits in UTF-8 are made of only one UTF-8 code-unit (1 byte).
void wxJSONWriter::Write | ( | const wxJSONValue & | value, |
wxString & | str | ||
) |
Write the JSONvalue object to a JSON text.
The two overloaded versions of this function let the user choose the output object which can be:
The two types of output object are very different because the text outputted is encoded in different formats depending on the build mode. When writing to a string object, the JSON text output is encoded differently depending on the build mode and the platform. Writing to a stream always produce UTF-8 encoded text. To know more about this topic read wxjson_tutorial_unicode.
Also note that the Write() function does not return a status code. If you are writing to a string, you do not have to warry about this issue: no errors can occur when writing to strings. On the other hand, wehn writing to a stream there could be errors in the write operation. If an error occurs, the Write
(9 function immediatly returns without trying further output operations. You have to check the status of the stream by calling the stream's memberfunctions. Example:
void wxJSONWriter::Write | ( | const wxJSONValue & | value, |
wxOutputStream & | os | ||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
protected |
Writes a value of type BOOL.
This function is called for every value objects of BOOL type. This function prints the literals true or false depending on the value in value
. Returns -1 on stream errors or ZERO if no errors.
|
protected |
Writes a value of type DOUBLE.
This function is called for every value objects of DOUBLE type. This function uses the
snprintf function to get the US-ASCII representation of the integer and simply copy it to the output stream. Returns -1 on stream errors or ZERO if no errors.
Note that writing a double to a decimal ASCII representation could lay to unexpected results depending on the format string used in the conversion. See SetDoubleFmtString for details.
|
protected |
Writes the indentation to the JSON text.
The two functions write the indentation as spaces in the JSON output text. When called with a int parameter, the function writes the specified number of spaces. If no parameter is given, the function computes the number of spaces using the following formula: If the wxJSONWRITER_TAB_INDENT flag is used in the writer's cnstructor, the function calls WriteTabIndent().
The function also checks that wxJSONWRITER_STYLED is set and the wxJSONWRITER_NO_INDENTATION is not set.
|
protected |
Write the specified number of indentation (spaces or tabs)
The function is called by WriteIndent() and other writer's functions. It writes the indentation as specified in the num
parameter which is the actual level of annidation. The function checks if wxJSONWRITER_STYLED is set: if not, no indentation is performed. Also, the function checks if wxJSONWRITER_TAB_INDENT is set: if it is, indentation is done by writing num TAB characters otherwise, it is performed by writing a number of spaces computed as:
|
protected |
Writes a value of type INT.
This function is called for every value objects of INT type. This function uses the
snprintf function to get the US-ASCII representation of the integer and simply copy it to the output stream. Returns -1 on stream errors or ZERO if no errors.
|
protected |
Write the invalid JSON value to the output stream.
An invalid wxJSONValue is a value that was not initialized and it is an error. You should never write invalid values to JSON text because the output is not valid JSON text. Note that the NULL value is a legal JSON text and it is written:
This function writes a non-JSON text to the output stream:
In debug mode, the function always fails with an wxFAIL_MSG failure.
|
protected |
Write a JSON value of type memory buffer.
The type wxJSONTYPE_MEMORYBUFF is a wxJSON extension that is not correctly read by other JSON implementations. By default, the function writes such a type as an array of INTs as follows:
If the writer object was constructed using the wxJSONWRITER_MEMORYBUFF
flag, then the output is much more compact and recognized by the wxJSON reader as a memory buffer type:
|
protected |
Write the NULL JSON value to the output stream.
The function writes the null literal string to the output stream.
|
protected |
Writes the separator between values.
The function is called when a value has been written to the JSON text output and it writes the separator character: LF. The LF char is actually written only if the wxJSONWRITER_STYLED flag is specified and wxJSONWRITER_NO_LINEFEEDS is not set.
Returns the last character written which is LF itself or -1 in case of errors. Note that LF is returned even if the character is not actually written.
|
protected |
Write a generic string.
The function writes the wxString object str
to the output object. The string is written as is; you cannot use it to write JSON strings to the output text. The function converts the string str
to UTF-8 and writes the buffer..
|
protected |
Write the provided string to the output object.
The function writes the string str
to the output object that was specified in the wxJSONWriter::Write() function. The function may split strings in two or more lines if the string contains LF characters if the m_style
data member contains the wxJSONWRITER_SPLIT_STRING flag.
The function does not actually write the string: for every character in the provided string the function calls WriteChar() which does the actual character output.
The function returns ZERO on success or -1 in case of errors.
|
protected |
Writes a value of type UNSIGNED INT.
This function is called for every value objects of UINT type. This function uses the
snprintf function to get the US-ASCII representation of the integer and simply copy it to the output stream. The function prepends a plus sign if the wxJSONWRITER_RECOGNIZE_UNSIGNED
flag is set in the m_flags
data member. Returns -1 on stream errors or ZERO if no errors.