nlib
nn::nlib::msgpack::JsonReader Class Referencefinal

JSON parser. Reads and parses the JSON string from the stream. More...

#include "nn/nlib/msgpack/JsonReader.h"

Public Types

enum  Option {
  OPTION_DEFAULT = 0x00000000,
  OPTION_RELAXED = 0x00000001
}
 Defines the option value that may be passed to Read. More...
 

Public Member Functions

InputStreamGetStream () noexcept
 Gets the base stream specified in Init. More...
 
int GetLine () const noexcept
 Gets the line number (near where the error occurred).
 
int GetColumn () const noexcept
 Gets the digit number (near where the error occurred).
 
bool Read (MpObject *obj, uint32_t option=0) noexcept
 Reads and parses the JSON string from the stream, and sets it in the obj parameter. More...
 
Basic Member Functions
 JsonReader () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~JsonReader () noexcept
 Destructor.
 
Initialization and Finalization
bool Init (InputStream *stream, const JsonReaderSettings &settings) noexcept
 Initializes JsonReader based on the settings parameter. Sets an error and returns false when it fails. More...
 
bool Init (InputStream *stream) noexcept
 Initializes JsonReader based on the default settings. Sets an error and returns false when it fails. More...
 
bool Close () noexcept
 Closes JsonReader. More...
 
Error Checking
errno_t GetErrorValue () const noexcept
 Gets the error that occurred. More...
 
 operator bool () const
 Returns true if an error has not occurred.
 

Static Public Member Functions

static errno_t ReadEx (MpObject *obj, const void *jsontext, uint32_t option=0, size_t n=RSIZE_MAX) noexcept
 Parse the JSON string in the specified object. More...
 
static bool Read (MpObject *obj, const char *jsontext, uint32_t option=0) noexcept
 Parse the JSON string in the specified object. More...
 

Detailed Description

JSON parser. Reads and parses the JSON string from the stream.

Description
The string must be a JSON string as defined in RFC 4627 (https://www.ietf.org/rfc/rfc4627.txt).
The following strings are not strictly JSON, and fail to parse.
  • 42 (Numerical values are not JSON.)
  • "abc" (Strings are not JSON.) Only arrays or associative arrays are JSON.
  • ['abc'] strings surrounded by single quotes are not JSON strings. The string must be surrounded by double quotes.
  • {abc:42} (The key to the associative array must be a string.)

Definition at line 38 of file JsonReader.h.

Member Enumeration Documentation

Defines the option value that may be passed to Read.

Enumerator
OPTION_DEFAULT 

The default settings.

OPTION_RELAXED 

Relaxes the requirements where JSON must be an array or an associative array. Specify this option when you want to load a string that is not strictly JSON, such as only numbers and strings.

Definition at line 40 of file JsonReader.h.

Member Function Documentation

nn::nlib::msgpack::JsonReader::Close ( )
inlinenoexcept

Closes JsonReader.

Returns
Returns true when successful.
Description
Closes JsonReader, and detaches the base stream. The base stream will not be closed.
The object may be reused by calling the Init member function again.

Definition at line 52 of file JsonReader.h.

nn::nlib::msgpack::JsonReader::GetErrorValue ( ) const
inlinenoexcept

Gets the error that occurred.

Return values
0No error occurred.
EILSEQIncorrect JSON character found (parse error).
EINVALError in an argument.
EEXISTAttempted to initialize a JsonReader that has already been initialized.
EIOAn error occurred in the base stream.
EBADFAttempted to use an uninitialized JsonReader.
E2BIGThe size of the array (and similar items) is too big.
ENOMEMFailed to allocate memory.

Definition at line 53 of file JsonReader.h.

nn::nlib::msgpack::JsonReader::GetStream ( )
inlinenoexcept

Gets the base stream specified in Init.

Returns
Returns the pointer to the stream.

Definition at line 54 of file JsonReader.h.

nn::nlib::msgpack::JsonReader::Init ( InputStream stream,
const JsonReaderSettings settings 
)
noexcept

Initializes JsonReader based on the settings parameter. Sets an error and returns false when it fails.

Parameters
[in]streamPointer to the input stream.
[in]settingsSettings for the JSON parser.
Returns
Returns true when successful.
nn::nlib::msgpack::JsonReader::Init ( InputStream stream)
inlinenoexcept

Initializes JsonReader based on the default settings. Sets an error and returns false when it fails.

Parameters
[in]streamPointer to the input stream.
Returns
Returns true when successful.

Definition at line 48 of file JsonReader.h.

nn::nlib::msgpack::JsonReader::Read ( MpObject obj,
const char *  jsontext,
uint32_t  option = 0 
)
inlinestaticnoexcept

Parse the JSON string in the specified object.

Parameters
[in]objThe object that stores the converted JSON.
[in]jsontextJSON string.
[in]optionOption. The value synthesized by the logical disjunction of the JsonReader::Option type value is assigned.
Returns
Returns true on success.
Description
Internally creates a JsonReader to read and parses the JSON string from the stream. Returns false if there is any error during this process. If this occurs, obj is not altered.

Definition at line 61 of file JsonReader.h.

nn::nlib::msgpack::JsonReader::Read ( MpObject obj,
uint32_t  option = 0 
)
noexcept

Reads and parses the JSON string from the stream, and sets it in the obj parameter.

Parameters
[in]objObject where the read JSON is set.
[in]optionOption. The value synthesized by the logical disjunction of the JsonReader::Option type value is assigned.
Returns
Returns true when successful.
Description
The JSON string is converted to an MpObject and stored in the obj parameter. Sets an error and returns false when an error occurs while reading.
nn::nlib::msgpack::JsonReader::ReadEx ( MpObject obj,
const void *  jsontext,
uint32_t  option = 0,
size_t  n = RSIZE_MAX 
)
staticnoexcept

Parse the JSON string in the specified object.

Parameters
[in]objThe object that stores the converted JSON.
[in]jsontextJSON string.
[in]optionOption. The value synthesized by the logical disjunction of the JsonReader::Option type value is assigned.
[in]nThe size of jsontext parameter. This parameter must be specified if jsontext is not null-terminated.
Returns
Returns true on success.
Description
Internally creates a JsonReader to read and parses the JSON string from the stream. Returns false if there is any error during this process. If this occurs, obj is not altered.

The documentation for this class was generated from the following files: