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  Detail
 When an error occurs, the string indicating the location of the error is stored in path. More...
 
struct  Token
 Stores data on the tokens obtained by the parser. More...
 

Public Types

enum  Event {
  kNone = -1,
  kEventNull = 1,
  kEventTrue,
  kEventFalse,
  kEventString,
  kEventNumberInt32,
  kEventNumberUint32,
  kEventNumberInt64,
  kEventNumberUint64,
  kEventNumberFloat,
  kEventNumberDouble,
  kEventStartArray,
  kEventEndArray,
  kEventStartMap,
  kEventEndMap,
  kEventKeyName,
  kEventBinary,
  kEventExt,
  kEventEndDocument
}
 The type used by JsonStreamParser::Next() and Token, and the event that corresponds to the data read by the parser. More...
 

Public Member Functions

errno_t Open (InputStream *stream) noexcept
 Specifies a stream and starts the parsing. More...
 
errno_t Close () noexcept
 Closes the parser and sets it to the state immediately after it is initialized. More...
 
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...
 
Constructor, Destructor, and Initialization
constexpr JsonStreamParser () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~JsonStreamParser () noexcept
 Destructor.
 
 JsonStreamParser (JsonStreamParser &&rhs) noexcept
 Instantiates the object (move constructor).
 
JsonStreamParseroperator= (JsonStreamParser &&rhs) noexcept
 Move assignment operator.
 
errno_t Init (const JsonStreamParserSettings &settings) noexcept
 Initializes by specifying the behavior option. Returns 0 if successful. More...
 
errno_t Init () noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
Error Checking
Error GetError () const noexcept
 Gets an error. More...
 
 operator bool () const
 Returns true if the object has been initialized and an error has not occurred inside, or returns false if an error has occurred inside.
 

Static Public Member Functions

static errno_t Parse (JsonStreamParser *parser, UniquePtr< MpObject > &obj, bool peek, Detail *detail) noexcept
 Creates MpObject by parsing JSON or msgpack. More...
 
static errno_t Parse (JsonStreamParser *parser, UniquePtr< MpObject > &obj, bool peek) noexcept
 A parameter omitted version of the above function which passes nullptr as a parameter.
 
static errno_t Parse (JsonStreamParser *parser, UniquePtr< MpObject > &obj) noexcept
 A parameter omitted version of the above function which passes false as a parameter.
 
static errno_t Parse (UniquePtr< MpObject > &obj, const void *data, size_t n, const JsonStreamParserSettings &settings, Detail *detail) noexcept
 Creates MpObject by parsing JSON or msgpack. More...
 
static errno_t Parse (UniquePtr< MpObject > &obj, const void *data, size_t n, const JsonStreamParserSettings &settings) noexcept
 A parameter omitted version of the above function which passes nullptr as a parameter.
 
static errno_t Parse (UniquePtr< MpObject > &obj, const void *data, size_t n) noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
static errno_t Parse (UniquePtr< MpObject > &obj, const nlib_utf8_t *str, const JsonStreamParserSettings &settings, Detail *detail) noexcept
 Creates MpObject by parsing JSON. More...
 
static errno_t Parse (UniquePtr< MpObject > &obj, const nlib_utf8_t *str, const JsonStreamParserSettings &settings) noexcept
 A parameter omitted version of the above function which passes nullptr as a parameter.
 
static errno_t Parse (UniquePtr< MpObject > &obj, const nlib_utf8_t *str) noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (JsonStreamParser *parser, bool peek, Detail *detail) noexcept
 Creates and returns MpObject by parsing JSON or msgpack. More...
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (JsonStreamParser *parser, bool peek) noexcept
 A parameter omitted version of the above function which passes nullptr as a parameter.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (JsonStreamParser *parser) noexcept
 A parameter omitted version of the above function which passes false as a parameter.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_byte_t *first, const nlib_byte_t *last, const JsonStreamParserSettings &settings, Detail *detail) noexcept
 Creates and returns MpObject by parsing msgpack. More...
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_byte_t *first, const nlib_byte_t *last, Detail *detail) noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_byte_t *first, const nlib_byte_t *last, const JsonStreamParserSettings &settings) noexcept
 A parameter omitted version of the above function which passes nullptr as a parameter.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_byte_t *first, const nlib_byte_t *last) noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const MpWalker &walker, const JsonStreamParserSettings &settings, Detail *detail) noexcept
 Runs Parse(walker.GetPtr(), walker.GetPtr() + walker.GetSize(), settings, detail). More...
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const MpWalker &walker, Detail *detail) noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const MpWalker &walker, const JsonStreamParserSettings &settings) noexcept
 A parameter omitted version of the above function which passes nullptr as a parameter.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const MpWalker &walker) noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_utf8_t *str, const JsonStreamParserSettings &settings, Detail *detail) noexcept
 Creates and returns MpObject by parsing JSON. More...
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_utf8_t *str, const JsonStreamParserSettings &settings) noexcept
 A parameter omitted version of the above function which passes nullptr as a parameter.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_utf8_t *str, Detail *detail) noexcept
 A parameter omitted version of the above function which passes settings as the default value.
 
static std::pair< errno_t, UniquePtr< MpObject > > Parse (const nlib_utf8_t *str) noexcept
 
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...
 
static errno_t ToTimestamp (const Token &token, nlib_time *t) noexcept
 Converts a timestamp to nlib_time. More...
 
static std::pair< errno_t, int32_t > ToInt32 (const Token &token) noexcept
 Casts a numeric token to int32_t. More...
 
static std::pair< errno_t, uint32_t > ToUint32 (const Token &token) noexcept
 Casts a numeric token to uint32_t. More...
 
static std::pair< errno_t, int64_t > ToInt64 (const Token &token) noexcept
 Casts a numeric token to int64_t. More...
 
static std::pair< errno_t, uint64_t > ToUint64 (const Token &token) noexcept
 Casts a numeric token to uint64_t. More...
 
static std::pair< errno_t, float > ToFloat (const Token &token) noexcept
 Casts a numeric token to float. More...
 
static std::pair< errno_t, double > ToDouble (const Token &token) noexcept
 Casts a numeric token to double. More...
 
static std::pair< errno_t, nlib_timeToTimestamp (const Token &token) noexcept
 Converts a timestamp to nlib_time. It is converted only when Timestamp extension type in msgpack was parsed. 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.
const nlib_utf8_t* json = R"({"key1" : "value", "key2" : [-1,2,12345678901,1.2], "key3" : [true, false, null]})";
MemoryInputStream stream(json, nlib_strlen(json));
SUCCEED_IF(parser.Init() == 0);
SUCCEED_IF(parser.Open(&stream) == 0);
while (parser.HasNext()) {
JsonStreamParser::Event ev = parser.Next();
SUCCEED_IF(!parser.GetToken().token_toobig);
switch (ev) {
case JsonStreamParser::kEventNull: EV_FOUND("null");
case JsonStreamParser::kEventTrue: EV_FOUND("true");
case JsonStreamParser::kEventFalse: EV_FOUND("false");
nlib_printf("String(%s) found\n", parser.GetToken().buf); break;
nlib_printf("Int32(%" PRId32 ") found\n", parser.GetToken().number.i32); break;
nlib_printf("Uint32(%" PRIu32 ") found\n", parser.GetToken().number.u32); break;
nlib_printf("Int64(%" PRId64 ") found\n", parser.GetToken().number.i64); break;
nlib_printf("Uint64(%" PRIu64 ") found\n", parser.GetToken().number.u64); break;
nlib_printf("Float(%f) found\n", parser.GetToken().number.f32); break;
nlib_printf("Double(%f) found\n", parser.GetToken().number.f64); break;
case JsonStreamParser::kEventStartArray: EV_FOUND("array open");
case JsonStreamParser::kEventEndArray: EV_FOUND("array close");
case JsonStreamParser::kEventStartMap: EV_FOUND("map open");
case JsonStreamParser::kEventEndMap: EV_FOUND("map close");
nlib_printf("Key(%s) found\n", parser.GetToken().buf); break;
// unreachable because parser.HasNext() becomes false
nlib_printf("End document found\n");
break;
default:
SUCCEED_IF(false); // error
}
}
SUCCEED_IF(!!parser);
parser.Close();
stream.Close();
/*
Output:
map open found
Key(key1) found
String(value) found
Key(key2) found
array open found
Int32(-1) found
Uint32(2) found
Uint64(12345678901) found
Double(1.200000) found
array close found
Key(key3) found
array open found
true found
false found
null found
array close found
map close found
*/
If you want to use it as MpObject, the code can be simply written as follows:
const nlib_utf8_t* json = R"({"key1" : "value", "key2" : [-1,2,12345678901,1.2], "key3" : [true, false, null]})";
auto result = JsonStreamParser::Parse(json);
SUCCEED_IF(result.first == 0);
auto& mpobj_ptr = result.second; // std::unique_ptr<MpObject>
In either case, note that you have to close or destruct JsonStreamParser before closing the stream.

Definition at line 52 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
kNone 

An error has occurred.

kEventNull 

null has been read.

kEventTrue 

true has been read.

kEventFalse 

false has been read.

kEventString 

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

kEventNumberInt32 

An int32_t type integer has been read.

kEventNumberUint32 

A uint32_t type integer has been read.

kEventNumberInt64 

An int64_t type integer has been read.

kEventNumberUint64 

A uint64_t type integer has been read.

kEventNumberFloat 

A float type floating-point number has been read.

kEventNumberDouble 

A double type floating-point number has been read.

kEventStartArray 

The array has started.

kEventEndArray 

The array has finished.

kEventStartMap 

The associative array has started.

kEventEndMap 

The associative array has finished.

kEventKeyName 

An associative array key has been read.

kEventBinary 

Binary data has been read (msgpack only).

kEventExt 

Extended data has been read (msgpack only).

kEventEndDocument 

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

Definition at line 54 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
kOk An error has not occurred.
kErrorColon ':' could not be found.
kErrorComma ',' could not be found.
kErrorQuote '"' could not be found.
kErrorKeyTooLong The associative array key was too long to store it in the buffer for storing tokens.
kErrorNumberTooLong The numeric string was too long to store it in the buffer for storing tokens
kErrorArrayTooBig The array size exceeded the specified limit.
kErrorMapTooBig The associative array size exceeded the specified limit.
kErrorCharacter The string contains characters that are not UTF-8.
kErrorNumberRange The number is too large or small.
kErrorDepth The array and associative array depth exceeded the specified limit.
kErrorToken An error has occurred with other read strings.
kErrorStream 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 312 of file JsonStreamParser.h.

◆ Init()

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

Initializes by specifying the behavior option. Returns 0 if successful.

Parameters
[in]settingsBehavior options.
Return values
0Success.
EALREADYAlready initialized.
ENOMEMIndicates that internal memory allocation failed.
EINVALIndicates that the specification of behavior options was invalid.

◆ 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()

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.

◆ Parse() [1/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( JsonStreamParser parser,
UniquePtr< MpObject > &  obj,
bool  peek,
Detail detail 
)
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.
[out]detailIf not NULL, the string indicating the error location is stored.
Returns
Returns 0 on success.

◆ Parse() [2/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( UniquePtr< MpObject > &  obj,
const void *  data,
size_t  n,
const JsonStreamParserSettings settings,
Detail detail 
)
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.
[out]detailIf not NULL, the string indicating the error location is stored.
Returns
Returns 0 on success.

◆ Parse() [3/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( UniquePtr< MpObject > &  obj,
const nlib_utf8_t str,
const JsonStreamParserSettings settings,
Detail detail 
)
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.
[out]detailIf not NULL, the string indicating the error location is stored.
Returns
Returns 0 on success.
Description
This function cannot be used to parse msgpack. This function works on the assumption that JsonStreamParserSettings::kFormatJson is specified for settings.format.

Definition at line 130 of file JsonStreamParser.h.

◆ Parse() [4/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( JsonStreamParser parser,
bool  peek,
Detail detail 
)
staticnoexcept

Creates and returns MpObject by parsing JSON or msgpack.

Parameters
[in]parserParser
[in]peekIf true, the token that has already been obtained with Next() is used for the first token.
[out]detailIf not NULL, the string indicating the error location is stored.
Returns
A pair of the error value and unique_ptr<MpObject>.
Description
When parsing multiple JSON, peek passes true from the second JSON onward. Normally, pass false.

◆ Parse() [5/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( const nlib_byte_t first,
const nlib_byte_t last,
const JsonStreamParserSettings settings,
Detail detail 
)
staticnoexcept

Creates and returns MpObject by parsing msgpack.

Parameters
[in]firstPointer to the start of the msgpack byte string.
[in]lastPointer to the end of the msgpack byte string.
[in]settingsParameter for setting the parser.
[out]detailIf not NULL, the string indicating the error location is stored.
Returns
A pair of the error value and unique_ptr<MpObject>.

◆ Parse() [6/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( const MpWalker walker,
const JsonStreamParserSettings settings,
Detail detail 
)
inlinestaticnoexcept

Runs Parse(walker.GetPtr(), walker.GetPtr() + walker.GetSize(), settings, detail).

Parameters
[in]walkerThe msgpack byte string you want to parse.
[in]settingsParameter for setting the parser.
[out]detailIf not NULL, the string indicating the error location is stored.
Returns
A pair of the error value and unique_ptr<MpObject>.

Definition at line 179 of file JsonStreamParser.h.

◆ Parse() [7/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( const nlib_utf8_t str,
const JsonStreamParserSettings settings,
Detail detail 
)
inlinestaticnoexcept

Creates and returns MpObject by parsing JSON.

Parameters
[in]strA JSON string you want to parse.
[in]settingsParameter for setting the parser.
[out]detailIf not NULL, the string indicating the error location is stored.
Returns
A pair of the error value and unique_ptr<MpObject>.

Definition at line 197 of file JsonStreamParser.h.

◆ Parse() [8/8]

nn::nlib::msgpack::JsonStreamParser::Parse ( const nlib_utf8_t str)
inlinestaticnoexcept

brief A parameter omitted version of the above function which passes settings as the default value.

Definition at line 215 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() [1/2]

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 or an attempt to convert a non-numeric value was made.
EDOMThe token value was an integer that is larger than 9007199254740991LL or smaller than -9007199254740991LL.

Definition at line 500 of file JsonStreamParser.h.

◆ ToDouble() [2/2]

nn::nlib::msgpack::JsonStreamParser::ToDouble ( const Token token)
inlinestaticnoexcept

Casts a numeric token to double.

Parameters
[in]tokenReference to the token obtained with GetToken().
Returns
Returns a pair of the error value and a double type value. The error value may be as follows.
  • Successful if 0, and the value casted is set to second.
  • In the case of EINVAL, other than a numerical value or a value smaller than DBL_MIN or greater than DBL_MAX was trying to be converted. The value in second is invalid.
  • In the case of EDOM, the token value was an integer which was larger than 9007199254740991LL or smaller than -9007199254740991LL. The value casted is set to second.
Description
A code and execution result example is shown below.
const nlib_utf8_t* json = R"([123.4, "string", 9007199254740992, 1.8976931348623158e+308])";
MemoryInputStream stream(json, nlib_strlen(json));
SUCCEED_IF(parser.Init() == 0);
SUCCEED_IF(parser.Open(&stream) == 0);
SUCCEED_IF(parser.Next() == JsonStreamParser::kEventStartArray);
parser.Next();
auto r = JsonStreamParser::ToDouble(parser.GetToken());
nlib_printf("ToDouble: %s, %f\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToDouble(parser.GetToken());
nlib_printf("ToDouble: %s, invalid\n", nlib_error_string(r.first));
parser.Next();
r = JsonStreamParser::ToDouble(parser.GetToken());
nlib_printf("ToDouble: %s, %f\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToDouble(parser.GetToken());
nlib_printf("ToDouble: %s, %f\n", nlib_error_string(r.first), r.second);
parser.Close();
stream.Close();
/*
Output:
ToDouble: OK, 123.400000
ToDouble: EINVAL, invalid
ToDouble: EDOM, 9007199254740992.000000
ToDouble: EINVAL, 0.000000
*/

Definition at line 254 of file JsonStreamParser.h.

◆ ToFloat() [1/2]

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 466 of file JsonStreamParser.h.

◆ ToFloat() [2/2]

nn::nlib::msgpack::JsonStreamParser::ToFloat ( const Token token)
inlinestaticnoexcept

Casts a numeric token to float.

Parameters
[in]tokenReference to the token obtained with GetToken().
Returns
Returns a pair of the error value and a float type value. The error value may be as follows.
  • Successful if 0, and the value casted is set to second.
  • In the case of EINVAL, other than a numerical value or a value smaller than DBL_MIN or greater than DBL_MAX was trying to be converted. The value in second is invalid.
  • In the case of ERANGE, the value was larger than FLT_MAX or smaller than FLT_MIN, and the value casted is set to second.
  • In the case of EDOM, the token value is an integer which is larger than 1677215 or smaller than -1677215, or the number of double type floating point numbers was trying to be casted. The value casted is set to second.
Description
A code and execution result example is shown below.
const nlib_utf8_t* json = R"([123.4, "string", 1677216, 3.502823466e+38, 1.8976931348623158e+308])";
MemoryInputStream stream(json, nlib_strlen(json));
SUCCEED_IF(parser.Init() == 0);
SUCCEED_IF(parser.Open(&stream) == 0);
SUCCEED_IF(parser.Next() == JsonStreamParser::kEventStartArray);
parser.Next();
auto r = JsonStreamParser::ToFloat(parser.GetToken());
nlib_printf("ToFloat: %s, %f\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToFloat(parser.GetToken());
nlib_printf("ToFloat: %s, invalid\n", nlib_error_string(r.first));
parser.Next();
r = JsonStreamParser::ToFloat(parser.GetToken());
nlib_printf("ToFloat: %s, %f\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToFloat(parser.GetToken());
nlib_printf("ToFloat: %s, %f\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToFloat(parser.GetToken());
nlib_printf("ToFloat: %s, %f\n", nlib_error_string(r.first), r.second);
parser.Close();
stream.Close();
/*
Output:
ToFloat: EDOM, 123.400002
ToFloat: EINVAL, invalid
ToFloat: OK, 1677216.000000
ToFloat: ERANGE, inf
ToFloat: EINVAL, 0.000000
*/
Attention
Note that when parsing JSON, a numerical value is converted from the double type to the float type and EDOM is returned. In the case of msgpack, if float 32 is parsed, this error will not be returned.

Definition at line 249 of file JsonStreamParser.h.

◆ ToInt32() [1/2]

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 larger than INT32_MAX or smaller than 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 329 of file JsonStreamParser.h.

◆ ToInt32() [2/2]

nn::nlib::msgpack::JsonStreamParser::ToInt32 ( const Token token)
inlinestaticnoexcept

Casts a numeric token to int32_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
Returns
Returns a pair of the error value and a int32_t type value. The error value may be as follows.
  • Successful if 0, and the value casted is set to second.
  • In the case of EINVAL, other than a numerical value is trying to be converted, and the value in second is invalid.
  • In the case of ERANGE, the value was larger than INT32_MAX or smaller than INT32_MIN, and the value casted is set to second.
  • In the case of EDOM, floating point numbers are converted and it is not the case where ERANGE is to be returned. The casted value is set to second.
Description
A code and execution result example is shown below.
const nlib_utf8_t* json = R"([100, "string", 2147483648, 1.23456])";
MemoryInputStream stream(json, nlib_strlen(json));
SUCCEED_IF(parser.Init() == 0);
SUCCEED_IF(parser.Open(&stream) == 0);
SUCCEED_IF(parser.Next() == JsonStreamParser::kEventStartArray);
parser.Next();
auto r = JsonStreamParser::ToInt32(parser.GetToken());
nlib_printf("ToInt32: %s, %" PRId32 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToInt32(parser.GetToken());
nlib_printf("ToInt32: %s, invalid\n", nlib_error_string(r.first));
parser.Next();
r = JsonStreamParser::ToInt32(parser.GetToken());
nlib_printf("ToInt32: %s, %" PRId32 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToInt32(parser.GetToken());
nlib_printf("ToInt32: %s, %" PRId32 "\n", nlib_error_string(r.first), r.second);
parser.Close();
stream.Close();
/*
Output:
ToInt32: OK, 100
ToInt32: EINVAL, invalid
ToInt32: ERANGE, -2147483648
ToInt32: EDOM, 1
*/

Definition at line 229 of file JsonStreamParser.h.

◆ ToInt64() [1/2]

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 399 of file JsonStreamParser.h.

◆ ToInt64() [2/2]

nn::nlib::msgpack::JsonStreamParser::ToInt64 ( const Token token)
inlinestaticnoexcept

Casts a numeric token to int64_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
Returns
Returns a pair of the error value and a int64_t type value. The error value may be as follows.
  • Successful if 0, and the value casted is set to second.
  • In the case of EINVAL, other than a numerical value is trying to be converted, and the value in second is invalid.
  • In the case of ERANGE, the value was larger than INT64_MAX or smaller than INT64_MIN, and the value casted is set to second.
  • In the case of EDOM, floating point numbers are converted and it is not the case where ERANGE is to be returned. The casted value is set to second.
Description
A code and execution result example is shown below.
const nlib_utf8_t* json = R"([100, "string", 9223372036854775808, 1.23456])";
MemoryInputStream stream(json, nlib_strlen(json));
SUCCEED_IF(parser.Init() == 0);
SUCCEED_IF(parser.Open(&stream) == 0);
SUCCEED_IF(parser.Next() == JsonStreamParser::kEventStartArray);
parser.Next();
auto r = JsonStreamParser::ToInt64(parser.GetToken());
nlib_printf("ToInt64: %s, %" PRId64 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToInt64(parser.GetToken());
nlib_printf("ToInt64: %s, invalid\n", nlib_error_string(r.first));
parser.Next();
r = JsonStreamParser::ToInt64(parser.GetToken());
nlib_printf("ToInt64: %s, %" PRId64 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToInt64(parser.GetToken());
nlib_printf("ToInt64: %s, %" PRId64 "\n", nlib_error_string(r.first), r.second);
parser.Close();
stream.Close();
/*
Output:
ToInt64: OK, 100
ToInt64: EINVAL, invalid
ToInt64: ERANGE, -9223372036854775808
ToInt64: EDOM, 1
*/

Definition at line 239 of file JsonStreamParser.h.

◆ ToTimestamp() [1/2]

nn::nlib::msgpack::JsonStreamParser::ToTimestamp ( const Token token,
nlib_time t 
)
staticnoexcept

Converts a timestamp to nlib_time.

Parameters
[in]tokenReference to the token obtained with GetToken().
[out]tPointer to the converted time.
Return values
0Success.
EINVALt is NULL or an attempt to convert a non-timestamp value was made.
ERANGEThe time is not within a range that can be stored in nlib_time.

◆ ToTimestamp() [2/2]

nn::nlib::msgpack::JsonStreamParser::ToTimestamp ( const Token token)
inlinestaticnoexcept

Converts a timestamp to nlib_time. It is converted only when Timestamp extension type in msgpack was parsed.

Parameters
[in]tokenReference to the token obtained with GetToken().
Returns
Returns a pair of the error value and a nlib_time type value. The error value may be as follows.
  • Successful if 0, and the value casted is set to second.
  • In the case of EINVAL, other than a timestamp is trying to be converted, and the value in second is invalid.
  • In the case of ERANGE, time was not in a range that can be stored in nlib_time. The value in second is invalid.

Definition at line 259 of file JsonStreamParser.h.

◆ ToUint32() [1/2]

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 365 of file JsonStreamParser.h.

◆ ToUint32() [2/2]

nn::nlib::msgpack::JsonStreamParser::ToUint32 ( const Token token)
inlinestaticnoexcept

Casts a numeric token to uint32_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
Returns
Returns a pair of the error value and a uint32_t type value. The error value may be as follows.
  • Successful if 0, and the value casted is set to second.
  • In the case of EINVAL, other than a numerical value is trying to be converted, and the value in second is invalid.
  • In the case of ERANGE, the value was larger than UINT32_MAX or smaller than 0, and the value casted is set to second.
  • In the case of EDOM, floating point numbers are converted and it is not the case where ERANGE is to be returned. The casted value is set to second.
Description
A code and execution result example is shown below.
const nlib_utf8_t* json = R"([100, "string", -1, 1.23456])";
MemoryInputStream stream(json, nlib_strlen(json));
SUCCEED_IF(parser.Init() == 0);
SUCCEED_IF(parser.Open(&stream) == 0);
SUCCEED_IF(parser.Next() == JsonStreamParser::kEventStartArray);
parser.Next();
auto r = JsonStreamParser::ToUint32(parser.GetToken());
nlib_printf("ToUint32: %s, %" PRIu32 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToUint32(parser.GetToken());
nlib_printf("ToUint32: %s, invalid\n", nlib_error_string(r.first));
parser.Next();
r = JsonStreamParser::ToUint32(parser.GetToken());
nlib_printf("ToUint32: %s, %" PRIu32 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToUint32(parser.GetToken());
nlib_printf("ToUint32: %s, %" PRIu32 "\n", nlib_error_string(r.first), r.second);
parser.Close();
stream.Close();
/*
Output:
ToUint32: OK, 100
ToUint32: EINVAL, invalid
ToUint32: ERANGE, -1
ToUint32: EDOM, 1
*/

Definition at line 234 of file JsonStreamParser.h.

◆ ToUint64() [1/2]

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 433 of file JsonStreamParser.h.

◆ ToUint64() [2/2]

nn::nlib::msgpack::JsonStreamParser::ToUint64 ( const Token token)
inlinestaticnoexcept

Casts a numeric token to uint64_t.

Parameters
[in]tokenReference to the token obtained with GetToken().
Returns
Returns a pair of the error value and a uint64_t type value. The error value may be as follows.
  • Successful if 0, and the value casted is set to second.
  • In the case of EINVAL, other than a numerical value is trying to be converted, and the value in second is invalid.
  • In the case of ERANGE, the value was larger than UINT64_MAX or smaller than 0, and the value casted is set to second.
  • In the case of EDOM, floating point numbers are converted and it is not the case where ERANGE is to be returned. The casted value is set to second.
Description
A code and execution result example is shown below.
const nlib_utf8_t* json = R"([100, "string", -1, 1.23456])";
MemoryInputStream stream(json, nlib_strlen(json));
SUCCEED_IF(parser.Init() == 0);
SUCCEED_IF(parser.Open(&stream) == 0);
SUCCEED_IF(parser.Next() == JsonStreamParser::kEventStartArray);
parser.Next();
auto r = JsonStreamParser::ToUint64(parser.GetToken());
nlib_printf("ToUint64: %s, %" PRIu64 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToUint64(parser.GetToken());
nlib_printf("ToUint64: %s, invalid\n", nlib_error_string(r.first));
parser.Next();
r = JsonStreamParser::ToUint64(parser.GetToken());
nlib_printf("ToUint64: %s, %" PRIu64 "\n", nlib_error_string(r.first), r.second);
parser.Next();
r = JsonStreamParser::ToUint64(parser.GetToken());
nlib_printf("ToUint64: %s, %" PRIu64 "\n", nlib_error_string(r.first), r.second);
parser.Close();
stream.Close();
/*
Output:
ToUint64: OK, 100
ToUint64: EINVAL, invalid
ToUint64: ERANGE, 18446744073709551615
ToUint64: EDOM, 1
*/

Definition at line 244 of file JsonStreamParser.h.


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