16 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONSTREAMPARSER_H_ 17 #define INCLUDE_NN_NLIB_MSGPACK_JSONSTREAMPARSER_H_ 22 #include "nn/nlib/Swap.h" 25 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS) 26 #undef NLIB_VIS_PUBLIC 27 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT 39 FORMAT_ADAPTIVE = kFormatAdaptive,
40 FORMAT_JSON = kFormatJson,
41 FORMAT_MSGPACK = kFormatMsgpack
50 max_array_size(8192), max_map_size(256), token_buffer_size(2048), max_depth(7),
51 format(kFormatAdaptive), strict(false) {}
77 EVENT_NULL = kEventNull,
78 EVENT_TRUE = kEventTrue,
79 EVENT_FALSE = kEventFalse,
80 EVENT_STRING = kEventString,
81 EVENT_NUMBER_INT32 = kEventNumberInt32,
82 EVENT_NUMBER_UINT32 = kEventNumberUint32,
83 EVENT_NUMBER_INT64 = kEventNumberInt64,
84 EVENT_NUMBER_UINT64 = kEventNumberUint64,
85 EVENT_NUMBER_FLOAT = kEventNumberFloat,
86 EVENT_NUMBER_DOUBLE = kEventNumberDouble,
87 EVENT_START_ARRAY = kEventStartArray,
88 EVENT_END_ARRAY = kEventEndArray,
89 EVENT_START_MAP = kEventStartMap,
90 EVENT_END_MAP = kEventEndMap,
91 EVENT_KEY_NAME = kEventKeyName,
92 EVENT_BINARY = kEventBinary,
93 EVENT_EXT = kEventExt,
94 EVENT_END_DOCUMENT = kEventEndDocument
111 ERROR_COLON = kErrorColon,
112 ERROR_COMMA = kErrorComma,
113 ERROR_QUOTE = kErrorQuote,
114 ERROR_KEY_TOOLONG = kErrorKeyTooLong,
115 ERROR_NUMBER_TOOLONG = kErrorNumberTooLong,
116 ERROR_ARRAY_TOOBIG = kErrorArrayTooBig,
117 ERROR_MAP_TOOBIG = kErrorMapTooBig,
118 ERROR_CHARACTER = kErrorCharacter,
119 ERROR_NUMBER_RANGE = kErrorNumberRange,
120 ERROR_DEPTH = kErrorDepth,
121 ERROR_TOKEN = kErrorToken,
122 ERROR_STREAM = kErrorStream
144 return Parse(parser, obj,
false);
151 return Parse(obj, data, n, settings);
156 if (settings.
format == JsonStreamParserSettings::kFormatJson) {
157 return Parse(obj, str, n, settings);
160 settings_.
format = JsonStreamParserSettings::kFormatJson;
161 return Parse(obj, str, n, settings_);
166 return Parse(obj, str, settings);
182 return this->Init(settings);
197 swap(prv_, rhs.prv_);
198 swap(error_, rhs.error_);
199 swap(token_, rhs.token_);
209 struct JsonStreamParserPrivate;
210 JsonStreamParserPrivate* prv_;
217 NLIB_EINVAL_IFNULL(number);
218 switch (token.event) {
219 case kEventNumberInt32:
220 *number = token.number.i32;
222 case kEventNumberUint32:
223 *number =
static_cast<int32_t
>(token.number.u32);
224 if (token.number.u32 > INT32_MAX)
return ERANGE;
226 case kEventNumberInt64:
227 *number =
static_cast<int32_t
>(token.number.i64);
228 if (token.number.i64 > INT32_MAX ||
229 token.number.i64 < INT32_MIN)
return ERANGE;
231 case kEventNumberUint64:
232 *number =
static_cast<int32_t
>(token.number.u64);
233 if (token.number.u64 > INT32_MAX)
return ERANGE;
235 case kEventNumberFloat:
236 *number =
static_cast<int32_t
>(token.number.f32);
237 if (token.number.f32 > static_cast<float>(INT32_MAX) ||
238 token.number.f32 <
static_cast<float>(INT32_MIN))
241 case kEventNumberDouble:
242 *number =
static_cast<int32_t
>(token.number.f64);
243 if (token.number.f64 > static_cast<double>(INT32_MAX) ||
244 token.number.f64 <
static_cast<double>(INT32_MIN))
254 NLIB_EINVAL_IFNULL(number);
255 switch (token.event) {
256 case kEventNumberInt32:
257 *number =
static_cast<uint32_t
>(token.number.i32);
258 if (token.number.i32 < 0)
return ERANGE;
260 case kEventNumberUint32:
261 *number = token.number.u32;
263 case kEventNumberInt64:
264 *number =
static_cast<uint32_t
>(token.number.i64);
265 if (token.number.i64 > UINT32_MAX ||
266 token.number.i64 < 0)
return ERANGE;
268 case kEventNumberUint64:
269 *number =
static_cast<uint32_t
>(token.number.u64);
270 if (token.number.u64 > UINT32_MAX)
return ERANGE;
272 case kEventNumberFloat:
273 *number =
static_cast<uint32_t
>(token.number.f32);
274 if (token.number.f32 > static_cast<float>(UINT32_MAX) ||
275 token.number.f32 < 0.f)
278 case kEventNumberDouble:
279 *number =
static_cast<uint32_t
>(token.number.f64);
280 if (token.number.f64 > static_cast<double>(UINT32_MAX) ||
281 token.number.f64 < 0.0)
291 NLIB_EINVAL_IFNULL(number);
292 switch (token.event) {
293 case JsonStreamParser::kEventNumberInt32:
294 *number = token.number.i32;
296 case JsonStreamParser::kEventNumberUint32:
297 *number =
static_cast<int64_t
>(token.number.u32);
299 case JsonStreamParser::kEventNumberInt64:
300 *number = token.number.i64;
302 case JsonStreamParser::kEventNumberUint64:
303 *number =
static_cast<int64_t
>(token.number.u64);
304 if (token.number.u64 > INT64_MAX)
return ERANGE;
306 case kEventNumberFloat:
307 *number =
static_cast<int64_t
>(token.number.f32);
308 if (token.number.f32 > static_cast<float>(INT64_MAX) ||
309 token.number.f32 <
static_cast<float>(INT64_MIN))
312 case kEventNumberDouble:
313 *number =
static_cast<int64_t
>(token.number.f64);
314 if (token.number.f64 > static_cast<double>(INT64_MAX) ||
315 token.number.f64 <
static_cast<double>(INT64_MIN))
325 NLIB_EINVAL_IFNULL(number);
326 switch (token.event) {
327 case kEventNumberInt32:
328 *number =
static_cast<uint64_t
>(token.number.i32);
329 if (token.number.i32 < 0)
return ERANGE;
331 case kEventNumberUint32:
332 *number = token.number.u32;
334 case kEventNumberInt64:
335 *number =
static_cast<uint64_t
>(token.number.i64);
336 if (token.number.i64 < 0)
return ERANGE;
338 case kEventNumberUint64:
339 *number = token.number.u64;
341 case kEventNumberFloat:
342 *number =
static_cast<uint64_t
>(token.number.f32);
343 if (token.number.f32 > static_cast<float>(UINT64_MAX) ||
344 token.number.f32 < 0.f)
347 case kEventNumberDouble:
348 *number =
static_cast<uint64_t
>(token.number.f64);
349 if (token.number.f64 > static_cast<double>(UINT64_MAX) ||
350 token.number.f64 < 0.0)
360 NLIB_EINVAL_IFNULL(number);
361 switch (token.event) {
362 case kEventNumberInt32:
363 *number =
static_cast<float>(token.number.i32);
364 if (token.number.i32 > 16777215 || token.number.i32 < -16777215)
367 case kEventNumberUint32:
368 *number =
static_cast<float>(token.number.u32);
369 if (token.number.u32 > 16777215)
372 case kEventNumberInt64:
373 *number =
static_cast<float>(token.number.i64);
374 if (token.number.i64 > 16777215 || token.number.i64 < -16777215)
377 case kEventNumberUint64:
378 *number =
static_cast<float>(token.number.u64);
379 if (token.number.u64 > 16777215)
382 case kEventNumberFloat:
383 *number = token.number.f32;
385 case kEventNumberDouble:
386 *number =
static_cast<float>(token.number.f32);
387 if (token.number.f64 < FLT_MIN ||
388 token.number.f64 > FLT_MAX)
399 NLIB_EINVAL_IFNULL(number);
400 switch (token.event) {
401 case kEventNumberInt32:
402 *number =
static_cast<double>(token.number.i32);
404 case kEventNumberUint32:
405 *number =
static_cast<double>(token.number.u32);
407 case kEventNumberInt64:
408 *number =
static_cast<double>(token.number.i64);
409 if (token.number.i64 > 9007199254740991LL ||
410 token.number.i64 < -9007199254740991LL)
413 case kEventNumberUint64:
414 *number =
static_cast<double>(token.number.u64);
415 if (token.number.u64 > 9007199254740991ULL)
418 case kEventNumberFloat:
419 *number = token.number.f32;
421 case kEventNumberDouble:
422 *number = token.number.f64;
432 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::JsonStreamParser)
433 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS) 434 #undef NLIB_VIS_PUBLIC 435 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT 438 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONSTREAMPARSER_H_ size_t token_buffer_size
Specifies the size of the buffer to store tokens. The default is 2048 and the minimum value is 512...
Extended data has been read (msgpack only).
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
A uint64_t type integer has been read.
bool strict
If a value other than 0 is set, a stricter error check is performed. The default is 0...
constexpr JsonStreamParserSettings() noexcept
Instantiates the object with default parameters (default constructor). Sets each data member to the d...
The associative array has started.
nlib_utf8_t * buf
Stores strings or binary data.
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Defines that class that is corresponding to std::unique_ptr.
An int32_t type integer has been read.
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...
Event event
The event returned by Next().
A double type floating-point number has been read.
Data structure used to store the JsonStreamParser settings parameters.
The class to parse JSON or msgpack in a pull manner.
The class for reading text from streams.
Event
The type used by JsonStreamParser::Next() and Token, and the event that corresponds to the data read ...
Object created when MessagePack, JSON, or CSV is read.
size_t max_map_size
Specifies the maximum associative array size. The default is 256 and the minimum value is 16...
size_t max_array_size
Specifies the maximum array size. The default is 8192 and the minimum value is 16.
An associative array key has been read.
A uint32_t type integer has been read.
static errno_t Parse(UniquePtr< MpObject > &obj, const nlib_utf8_t *str, const JsonStreamParserSettings &settings) noexcept
Creates MpObject by parsing JSON.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
JSON or msgpack has finished (A return value of Next() when HasNext() returns false).
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
bool token_toobig
When the event is kEventString and the token is too large, stores a non-zero value. In this case, a null string is set to buf.
static errno_t Parse(JsonStreamParser *parser, UniquePtr< MpObject > &obj) noexcept
Runs Parse(parser, obj, false).
const Token & GetToken() const noexcept
Gets the token.
Stores data on the tokens obtained by the parser.
A string other than an associative array key has been read.
static errno_t Parse(UniquePtr< MpObject > &obj, const nlib_utf8_t *str) noexcept
Runs Parse(obj, str, settings) with the default JsonStreamParserSettings settings specified...
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
int8_t ext
When the event is kEventExt, stores a data type 8-bit value.
An int64_t type integer has been read.
errno_t Init() noexcept
Initializes the parser using the default settings.
Binary data has been read (msgpack only).
size_t max_depth
Specifies the maximum value for the depth of array or associative array. The default is 7...
The associative array has finished.
Format format
Decides how formats are determined when parsing. The default is kFormatAdaptive.
A float type floating-point number has been read.