#include <jsonwriter.h>
Public Member Functions | |
wxJSONWriter (int style=wxJSONWRITER_STYLED, int indent=0, int step=3) | |
Ctor. | |
~wxJSONWriter () | |
Dtor - does nothing. | |
void | Write (const wxJSONValue &value, wxString &str) |
Write the JSONvalue object to a JSON text. | |
void | Write (const wxJSONValue &value, wxOutputStream &os) |
Protected Member Functions | |
int | DoWrite (wxOutputStream &os, const wxJSONValue &value, const wxString *key, bool comma) |
Perform the real write operation. | |
int | WriteIndent (wxOutputStream &os) |
Writes the indentation to the JSON text. | |
int | WriteIndent (wxOutputStream &os, int num) |
Write the specified number of indentation (spaces or tabs). | |
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. | |
int | WriteStringValue (wxOutputStream &os, const wxString &str) |
Write the provided string to the output object. | |
int | WriteNullValue (wxOutputStream &os) |
Write the NULL JSON value to the output stream. | |
int | WriteIntValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type INT. | |
int | WriteUIntValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type UNSIGNED INT. | |
int | WriteBoolValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type BOOL. | |
int | WriteDoubleValue (wxOutputStream &os, const wxJSONValue &v) |
Writes a value of type DOUBLE. | |
int | WriteInvalid (wxOutputStream &os) |
Write the invalid JSON value to the output stream. | |
int | WriteSeparator (wxOutputStream &os) |
Writes the separator between values. | |
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) |
Writes an error string to the output JSON text (not used). |
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.
Here is an example on how to use this class:
// construct the JSON value object and add values to it wxJSONValue root; root["key1"] = "some value"; ... // construct the string that will contain the JSON text wxString str; // construct a JSON writer: use the default writer's settings wxJSONWriter jsw; // call the writer's Write() memberfunction jsw.Write( root, str );
When writing to a stream object, the JSON text output is always encoded in UTF-8 in both ANSI and Unicode builds.
For more info about the unicode topic see Unicode support in wxJSON.
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:
|
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:
wxJSONWriter writer( wxJSONWRITER_STYLED | // want a styled output wxJSONWRITER_WRITE_COMMENTS | // want comments in the document wxJSONWRITER_COMMENTS_BEFORE, // force comments before value 0, // initial indentation 4); // indentation step writer.Write( value, document );
The following code construct a JSON writer that produces hard-to-read output: indentation is suppressed and no comment lines are written and the values in the JSON text are not separated by LF.
wxJSONWriter writer( wxJSONWRITER_NO_LINEFEEDS ); writer.Write( value, document );
wxJSONWriter::~wxJSONWriter | ( | ) |
Dtor - does nothing.
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:
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:
// construct the JSON value object and add values to it wxJSONValue root; root["key1"] = "some value"; // write to a stream wxMemoryOutputStream mem; wxJSONWriter writer; writer.Write( root, mem ); wxStreamError err = mem.GetLastError(); if ( err != wxSTREAM_NO_ERROR ) { MessageBox( _T("ERROR: cannot write the JSON text output")); }
void wxJSONWriter::Write | ( | const wxJSONValue & | value, | |
wxOutputStream & | os | |||
) |
int wxJSONWriter::DoWrite | ( | wxOutputStream & | os, | |
const wxJSONValue & | value, | |||
const wxString * | key, | |||
bool | comma | |||
) | [protected] |
Perform the real write operation.
This is a recursive function. The function calls:
WritePrimitiveValue()
for null, boolean, integer and double types WriteStringValue()
for string and C-string types m_level
data member. int wxJSONWriter::WriteIndent | ( | wxOutputStream & | os | ) | [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.
int wxJSONWriter::WriteIndent | ( | wxOutputStream & | os, | |
int | num | |||
) | [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:
numSpaces = m_indent + ( m_step * num )
bool wxJSONWriter::IsSpace | ( | wxChar | ch | ) | [protected] |
Returns TRUE if the character is a space character.
bool wxJSONWriter::IsPunctuation | ( | wxChar | ch | ) | [protected] |
Returns TRUE if the character if a puctuation character.
int wxJSONWriter::WriteString | ( | wxOutputStream & | os, | |
const wxString & | str | |||
) | [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..
int wxJSONWriter::WriteStringValue | ( | wxOutputStream & | os, | |
const wxString & | str | |||
) | [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.
int wxJSONWriter::WriteNullValue | ( | wxOutputStream & | os | ) | [protected] |
Write the NULL JSON value to the output stream.
The function writes the null literal string to the output stream.
int wxJSONWriter::WriteIntValue | ( | wxOutputStream & | os, | |
const wxJSONValue & | value | |||
) | [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.
int wxJSONWriter::WriteUIntValue | ( | wxOutputStream & | os, | |
const wxJSONValue & | value | |||
) | [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.
int wxJSONWriter::WriteBoolValue | ( | wxOutputStream & | os, | |
const wxJSONValue & | value | |||
) | [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.
int wxJSONWriter::WriteDoubleValue | ( | wxOutputStream & | os, | |
const wxJSONValue & | value | |||
) | [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.
int wxJSONWriter::WriteInvalid | ( | wxOutputStream & | os | ) | [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:
null
This function writes a non-JSON text to the output stream:
<invalid JSON value>
int wxJSONWriter::WriteSeparator | ( | wxOutputStream & | os | ) | [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.
int wxJSONWriter::WriteKey | ( | wxOutputStream & | os, | |
const wxString & | key | |||
) | [protected] |
Write the key of a key/value element to the output stream.
int wxJSONWriter::WriteComment | ( | wxOutputStream & | os, | |
const wxJSONValue & | value, | |||
bool | indent | |||
) | [protected] |
Write the comment strings, if any.
int wxJSONWriter::WriteError | ( | const wxString & | err | ) | [protected] |
Writes an error string to the output JSON text (not used).
The function just writes the provided string prepended with the ERROR word to the output JSON text. This could be usefull for signalling errors but not much reliable if the error occurs for the stream itself