nlib
nn::nlib::msgpack::JsonStreamParser Class Referencefinal

The class to parse JSON or msgpack in a pull manner. More...

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

Classes

struct  Token
 Stores data on the tokens obtained by the parser. More...
 

Public Types

enum  Event {
  NONE = -1,
  EVENT_NULL = 1,
  EVENT_TRUE,
  EVENT_FALSE,
  EVENT_STRING,
  EVENT_NUMBER_INT32,
  EVENT_NUMBER_UINT32,
  EVENT_NUMBER_INT64,
  EVENT_NUMBER_UINT64,
  EVENT_NUMBER_FLOAT,
  EVENT_NUMBER_DOUBLE,
  EVENT_START_ARRAY,
  EVENT_END_ARRAY,
  EVENT_START_MAP,
  EVENT_END_MAP,
  EVENT_KEY_NAME,
  EVENT_BINARY,
  EVENT_EXT,
  EVENT_END_DOCUMENT
}
 The type used by JsonStreamParser::Next() and Token, and the event that corresponds to the data read by the parser. More...
 

Public Member Functions

bool HasNext () const noexcept
 If there is a subsequent state to transit (the read JSON or msgpack has not ended), true is returned.
 
Event Next () noexcept
 Reads JSON or msgpack and transits the state in the parser. More...
 
bool Skip () noexcept
 Skips one value, the entire array, or the entire associative array. More...
 
int GetLine () const noexcept
 If JSON is now being read, returns the current number of rows.
 
int GetColumn () const noexcept
 If JSON is now being read, returns the current number of digits.
 
const TokenGetToken () const noexcept
 Gets the token. More...
 
Basic Member Functions
 JsonStreamParser () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~JsonStreamParser () noexcept
 Destructor.
 
Initialization and Finalization
errno_t Init (const JsonStreamParserSettings &settings) noexcept
 Initializes the parser. More...
 
errno_t Init () noexcept
 Initializes the parser using the default settings.
 
errno_t Open (InputStream *stream) noexcept
 Specifies a stream and starts the parsing. More...
 
errno_t Open (TextReader *reader) noexcept
 Specifies the TextReader object and starts the parsing. More...
 
errno_t Close () noexcept
 Closes the parser and sets it to the state immediately after it is initialized. More...
 
Error Checking
Error GetError () const noexcept
 Gets an error. More...
 
 operator bool () const
 Returns true if an error has not occurred.
 

Static Public Member Functions

static errno_t Parse (JsonStreamParser *parser, UniquePtr< MpObject > &obj, bool peek) noexcept
 Creates MpObject by parsing JSON or msgpack. More...
 
static errno_t Parse (JsonStreamParser *parser, UniquePtr< MpObject > &obj) noexcept
 Runs Parse(parser, obj, false).
 
static errno_t Parse (UniquePtr< MpObject > &obj, const void *data, size_t n, const JsonStreamParserSettings &settings) noexcept
 Creates MpObject by parsing JSON or msgpack. More...
 
static errno_t Parse (UniquePtr< MpObject > &obj, const void *data, size_t n) noexcept
 Runs Parse(obj, data, n, settings) with the default JsonStreamParserSettings settings specified.
 
static errno_t Parse (UniquePtr< MpObject > &obj, const char *str, const JsonStreamParserSettings &settings) noexcept
 Creates MpObject by parsing JSON. More...
 
static errno_t Parse (UniquePtr< MpObject > &obj, const char *str) noexcept
 Runs Parse(obj, str, settings) with the default JsonStreamParserSettings settings specified.
 
Converting Parsed Numeric Values

Converts a numeric-type token into the specified type.

static errno_t ToInt32 (const Token &token, int32_t *number) noexcept
 Casts a numeric token to int32_t. More...
 
static errno_t ToUint32 (const Token &token, uint32_t *number) noexcept
 Casts a numeric token to uint32_t. More...
 
static errno_t ToInt64 (const Token &token, int64_t *number) noexcept
 Casts a numeric token to int64_t. More...
 
static errno_t ToUint64 (const Token &token, uint64_t *number) noexcept
 Casts a numeric token to uint64_t. More...
 
static errno_t ToFloat (const Token &token, float *number) noexcept
 Casts a numeric token to float. More...
 
static errno_t ToDouble (const Token &token, double *number) noexcept
 Casts a numeric token to double. More...
 

Detailed Description

The class to parse JSON or msgpack in a pull manner.

Description
Performs parsing by causing an event for each syntax element and processing it on the user side. Parsing is performed by obtaining events caused by the following codes.
JsonStreamParser::Token token;
if (nlib_is_error(parser.Init())) { ERROR }
if (nlib_is_error(parser.Open(stream))) { ERROR }
while (parser.HasNext()) {
JsonStreamParser::Event ev = parser.Next();
switch (ev) {
......
......
......
......
token = parser.GetToken();
......
.....
}
}
if (!nlib_is_error(parser)) { ERROR }

Definition at line 37 of file JsonStreamParser.h.

Member Enumeration Documentation

§ Event

The type used by JsonStreamParser::Next() and Token, and the event that corresponds to the data read by the parser.

Enumerator
NONE 

An error has occurred.

EVENT_NULL 

null has been read.

EVENT_TRUE 

true has been read.

EVENT_FALSE 

false has been read.

EVENT_STRING 

A string other than an associative array key has been read.

EVENT_NUMBER_INT32 

An int32_t type integer has been read.

EVENT_NUMBER_UINT32 

A uint32_t type integer has been read.

EVENT_NUMBER_INT64 

An int64_t type integer has been read.

EVENT_NUMBER_UINT64 

A uint64_t type integer has been read.

EVENT_NUMBER_FLOAT 

A float type floating-point number has been read.

EVENT_NUMBER_DOUBLE 

A double type floating-point number has been read.

EVENT_START_ARRAY 

The array has started.

EVENT_END_ARRAY 

The array has finished.

EVENT_START_MAP 

The associative array has started.

EVENT_END_MAP 

The associative array has finished.

EVENT_KEY_NAME 

An associative array key has been read.

EVENT_BINARY 

Binary data has been read (msgpack only).

EVENT_EXT 

Extended data has been read (msgpack only).

EVENT_END_DOCUMENT 

JSON or msgpack has finished (A return value of Next() when HasNext() returns false).

Definition at line 39 of file JsonStreamParser.h.

Member Function Documentation

§ Close()

nn::nlib::msgpack::JsonStreamParser::Close ( )
noexcept

Closes the parser and sets it to the state immediately after it is initialized.

Return values
0Success.
Description
The stream and the TextReader object specified with Open() are not closed and detached from this class.

§ GetError()

nn::nlib::msgpack::JsonStreamParser::GetError ( ) const
noexcept

Gets an error.

Returns
An error value defined by JsonStreamParser.
Description
Returns one of the following values.
Value Description
ERROR_OK An error has not occurred.
ERROR_COLON ':' could not be found.
ERROR_COMMA ',' could not be found.
ERROR_QUOTE '"' could not be found.
ERROR_KEY_TOOLONG The associative array key was too long to store it in the buffer for storing tokens.
ERROR_NUMBER_TOOLONG The numeric string was too long to store it in the buffer for storing tokens
ERROR_ARRAY_TOOBIG The array size exceeded the specified limit.
ERROR_MAP_TOOBIG The associative array size exceeded the specified limit.
ERROR_CHARACTER The string contains characters that are not UTF-8.
ERROR_NUMBER_RANGE The number is too large or small.
ERROR_DEPTH The array and associative array depth exceeded the specified limit.
ERROR_TOKEN An error has occurred with other read strings.
ERROR_STREAM An error has occurred with a stream.

§ GetToken()

nn::nlib::msgpack::JsonStreamParser::GetToken ( ) const
inlinenoexcept

Gets the token.

Returns
The token obtained by the Next() executed just previously.

Definition at line 143 of file JsonStreamParser.h.

§ Init()

nn::nlib::msgpack::JsonStreamParser::Init ( const JsonStreamParserSettings settings)
noexcept

Initializes the parser.

Parameters
[in]settingsBehavior options.
Return values
0Success.
ENOMEMmemory allocation failed.
EALREADYAlready initialized.

§ Next()

nn::nlib::msgpack::JsonStreamParser::Next ( )
noexcept

Reads JSON or msgpack and transits the state in the parser.

Returns
The event corresponding to the read data.
Description

§ Open() [1/2]

nn::nlib::msgpack::JsonStreamParser::Open ( InputStream stream)
noexcept

Specifies a stream and starts the parsing.

Parameters
[in]streamThe stream to read JSON or msgpack.
Return values
0Success.
EINVALIndicates that stream is NULL or JsonStreamParser is uninitialized.
ENONEMMemory allocation has failed.
EALREADYAlready opened.

§ Open() [2/2]

nn::nlib::msgpack::JsonStreamParser::Open ( TextReader reader)
noexcept

Specifies the TextReader object and starts the parsing.

Parameters
[in]readerThe TextReader object that reads JSON.
Return values
0Success.
EINVALIndicates that reader is NULL or uninitialized, or that JsonStreamParser is uninitialized.
EALREADYAlready opened.

§ Parse() [1/3]

nn::nlib::msgpack::JsonStreamParser::Parse ( JsonStreamParser parser,
UniquePtr< MpObject > &  obj,
bool  peek 
)
staticnoexcept

Creates MpObject by parsing JSON or msgpack.

Parameters
[in]parserParser
[out]objMpObject that stores the parsed results
[in]peekIf true, the token that has already been obtained with Next() is used for the first token.
Returns
Returns 0 on success.

§ Parse() [2/3]

nn::nlib::msgpack::JsonStreamParser::Parse ( UniquePtr< MpObject > &  obj,
const void *  data,
size_t  n,
const JsonStreamParserSettings settings 
)
staticnoexcept

Creates MpObject by parsing JSON or msgpack.

Parameters
[out]objMpObject that stores the parsed results
[in]dataData column that stores JSON or msgpack.
[in]nSize of the data column.
[in]settingsParameter for setting the parser.
Returns
Returns 0 on success.

§ Parse() [3/3]

nn::nlib::msgpack::JsonStreamParser::Parse ( UniquePtr< MpObject > &  obj,
const char *  str,
const JsonStreamParserSettings settings 
)
inlinestaticnoexcept

Creates MpObject by parsing JSON.

Parameters
[out]objMpObject that stores the parsed results
[in]strString that stores JSON
[in]settingsParameter for setting the parser.
Returns
Returns 0 on success.
Description
This function cannot be used to parse msgpack. This function works on the assumption that JsonStreamParserSettings::FORMAT_JSON is specified for settings.format.

Definition at line 104 of file JsonStreamParser.h.

§ Skip()

nn::nlib::msgpack::JsonStreamParser::Skip ( )
noexcept

Skips one value, the entire array, or the entire associative array.

Returns
Returns false if an error occurs mid-process.

§ ToDouble()

nn::nlib::msgpack::JsonStreamParser::ToDouble ( const Token token,
double *  number 
)
inlinestaticnoexcept

Casts a numeric token to double.

Parameters
[in]tokenReference to the token obtained with GetToken().
[out]numberPointer to the converted numeric value.
Return values
0Success.
EINVALnumber is NULL.
ERANGEThe token value was an integer that is larger than 9007199254740991LL or smaller than -9007199254740991LL.
EDOMAn attempt to convert a non-numeric value was made.
Description
If 0 or ERANGE is returned, the value casted to *number is stored.

Definition at line 341 of file JsonStreamParser.h.

§ ToFloat()

nn::nlib::msgpack::JsonStreamParser::ToFloat ( const Token token,
float *  number 
)
inlinestaticnoexcept

Casts a numeric token to float.

Parameters
[in]tokenReference to the token obtained with GetToken().
[out]numberPointer to the converted numeric value.
Return values
0Success.
EINVALnumber is NULL or an attempt to convert a non-numeric value was made.
ERANGEThe specified value was larger than FLT_MAX or smaller than FLT_MIN.
EDOMThe token value was an integer that is larger than 1677215 or smaller than -1677215.
Description
If 0, ERANGE, or EDOM is returned, the value casted to *number is stored.

Definition at line 302 of file JsonStreamParser.h.

§ ToInt32()

nn::nlib::msgpack::JsonStreamParser::ToInt32 ( const Token token,
int32_t *  number 
)
inlinestaticnoexcept

Casts a numeric token to int32_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
[out]numberPointer to the converted numeric value.
Return values
0Success.
EINVALnumber is NULL or an attempt to convert a non-numeric value was made.
ERANGEThe specified value was smaller than INT32_MAX or INT32_MIN.
EDOMERNAGE was not returned when attempting to convert a floating-point value.
Description
If 0, ERANGE, or EDOM is returned, the value casted to *number is stored.

Definition at line 159 of file JsonStreamParser.h.

§ ToInt64()

nn::nlib::msgpack::JsonStreamParser::ToInt64 ( const Token token,
int64_t *  number 
)
inlinestaticnoexcept

Casts a numeric token to int64_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
[out]numberPointer to the converted numeric value.
Return values
0Success.
EINVALnumber is NULL or an attempt to convert a non-numeric value was made.
ERANGEThe specified value was smaller than INT64_MAX or INT64_MIN.
EDOMERNAGE was not returned when attempting to convert a floating-point value.
Description
If 0, ERANGE, or EDOM is returned, the value casted to *number is stored.

Definition at line 233 of file JsonStreamParser.h.

§ ToUint32()

nn::nlib::msgpack::JsonStreamParser::ToUint32 ( const Token token,
uint32_t *  number 
)
inlinestaticnoexcept

Casts a numeric token to uint32_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
[out]numberPointer to the converted numeric value.
Return values
0Success.
EINVALnumber is NULL or an attempt to convert a non-numeric value was made.
ERANGEThe specified value was larger than UINT32_MAX or smaller than 0.
EDOMERNAGE was not returned when attempting to convert a floating-point value.
Description
If 0, ERANGE, or EDOM is returned, the value casted to *number is stored.

Definition at line 196 of file JsonStreamParser.h.

§ ToUint64()

nn::nlib::msgpack::JsonStreamParser::ToUint64 ( const Token token,
uint64_t *  number 
)
inlinestaticnoexcept

Casts a numeric token to uint64_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
[out]numberPointer to the converted numeric value.
Return values
0Success.
EINVALnumber is NULL or an attempt to convert a non-numeric value was made.
ERANGEThe specified value was larger than UINT64_MAX or smaller than 0.
EDOMERNAGE was not returned when attempting to convert a floating-point value.
Description
If 0, ERANGE, or EDOM is returned, the value casted to *number is stored.

Definition at line 267 of file JsonStreamParser.h.


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