simdjson 5.0.1
Ridiculously Fast JSON
Loading...
Searching...
No Matches
parser.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_PARSER_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_PARSER_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/implementation_simdjson_result_base.h"
7#endif // SIMDJSON_CONDITIONAL_INCLUDE
8
9#include <memory>
10#include <thread>
11
12namespace simdjson {
13namespace SIMDJSON_IMPLEMENTATION {
14namespace ondemand {
15
21static constexpr size_t DEFAULT_BATCH_SIZE = 1000000;
30static constexpr size_t MINIMAL_BATCH_SIZE = 32;
31
37class parser {
38public:
44 inline explicit parser(size_t max_capacity = SIMDJSON_MAXSIZE_BYTES) noexcept;
45
46 inline parser(parser &&other) noexcept = default;
47 simdjson_inline parser(const parser &other) = delete;
48 simdjson_inline parser &operator=(const parser &other) = delete;
49 simdjson_inline parser &operator=(parser &&other) noexcept = default;
50
52 inline ~parser() noexcept = default;
53
117 simdjson_warn_unused simdjson_result<document> iterate(padded_string_view json) & noexcept;
118#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
119 simdjson_warn_unused simdjson_result<document> iterate_allow_incomplete_json(padded_string_view json) & noexcept;
120#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
122 simdjson_warn_unused simdjson_result<document> iterate(const char *json, size_t len, size_t capacity) & noexcept;
124 simdjson_warn_unused simdjson_result<document> iterate(const uint8_t *json, size_t len, size_t capacity) & noexcept;
126 simdjson_warn_unused simdjson_result<document> iterate(std::string_view json, size_t capacity) & noexcept;
128 simdjson_warn_unused simdjson_result<document> iterate(const std::string &json) & noexcept;
132 simdjson_warn_unused simdjson_result<document> iterate(std::string &json) & noexcept;
134 simdjson_warn_unused simdjson_result<document> iterate(const simdjson_result<padded_string> &json) & noexcept;
136 simdjson_warn_unused simdjson_result<document> iterate(const simdjson_result<padded_string_view> &json) & noexcept;
138 simdjson_warn_unused simdjson_result<document> iterate(padded_string &&json) & noexcept = delete;
139
180 simdjson_warn_unused simdjson_result<json_iterator> iterate_raw(padded_string_view json) & noexcept;
181
182
256 inline simdjson_result<document_stream> iterate_many(const uint8_t *buf, size_t len, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
258 inline simdjson_result<document_stream> iterate_many(padded_string_view json, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
260 inline simdjson_result<document_stream> iterate_many(const char *buf, size_t len, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
262 inline simdjson_result<document_stream> iterate_many(const std::string &s, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
265 inline simdjson_result<document_stream> iterate_many(std::string &s, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
267 inline simdjson_result<document_stream> iterate_many(const padded_string &s, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
272 inline simdjson_result<document_stream> iterate_many(const std::string &&s, size_t batch_size = DEFAULT_BATCH_SIZE) = delete;// unsafe
274 inline simdjson_result<document_stream> iterate_many(const padded_string &&s, size_t batch_size = DEFAULT_BATCH_SIZE) = delete;// unsafe
276 simdjson_result<document_stream> iterate_many(const char *buf, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept = delete;
277
278#ifndef SIMDJSON_DISABLE_DEPRECATED_API
282 simdjson_deprecated inline simdjson_result<document_stream> iterate_many(const uint8_t *buf, size_t len, size_t batch_size, bool allow_comma_separated) noexcept;
284 simdjson_deprecated inline simdjson_result<document_stream> iterate_many(padded_string_view json, size_t batch_size, bool allow_comma_separated) noexcept;
286 simdjson_deprecated inline simdjson_result<document_stream> iterate_many(const char *buf, size_t len, size_t batch_size, bool allow_comma_separated) noexcept;
288 simdjson_deprecated inline simdjson_result<document_stream> iterate_many(const std::string &s, size_t batch_size, bool allow_comma_separated) noexcept;
290 simdjson_deprecated inline simdjson_result<document_stream> iterate_many(std::string &s, size_t batch_size, bool allow_comma_separated) noexcept;
292 simdjson_deprecated inline simdjson_result<document_stream> iterate_many(const padded_string &s, size_t batch_size, bool allow_comma_separated) noexcept;
294 inline simdjson_result<document_stream> iterate_many(const std::string &&s, size_t batch_size, bool allow_comma_separated) = delete;// unsafe
296 inline simdjson_result<document_stream> iterate_many(const padded_string &&s, size_t batch_size, bool allow_comma_separated) = delete;// unsafe
297#endif // SIMDJSON_DISABLE_DEPRECATED_API
298
308 inline simdjson_result<document_stream> iterate_many(const uint8_t *buf, size_t len, size_t batch_size, stream_format format) noexcept;
310 inline simdjson_result<document_stream> iterate_many(const char *buf, size_t len, size_t batch_size, stream_format format) noexcept;
312 inline simdjson_result<document_stream> iterate_many(padded_string_view s, size_t batch_size, stream_format format) noexcept;
314 inline simdjson_result<document_stream> iterate_many(const std::string &s, size_t batch_size, stream_format format) noexcept;
319 inline simdjson_result<document_stream> iterate_many(std::string &s, size_t batch_size, stream_format format) noexcept;
321 inline simdjson_result<document_stream> iterate_many(const padded_string &s, size_t batch_size, stream_format format) noexcept;
323 inline simdjson_result<document_stream> iterate_many(const std::string &&s, size_t batch_size, stream_format format) = delete;// unsafe
325 inline simdjson_result<document_stream> iterate_many(const padded_string &&s, size_t batch_size, stream_format format) = delete;// unsafe
326
328 simdjson_pure simdjson_inline size_t capacity() const noexcept;
330 simdjson_pure simdjson_inline size_t max_capacity() const noexcept;
331 simdjson_inline void set_max_capacity(size_t max_capacity) noexcept;
338 simdjson_pure simdjson_inline size_t max_depth() const noexcept;
339
352 simdjson_warn_unused error_code allocate(size_t capacity, size_t max_depth=DEFAULT_MAX_DEPTH) noexcept;
353
354 #ifdef SIMDJSON_THREADS_ENABLED
360 bool threaded{true};
361 #else
365 bool threaded{false};
366 #endif
389 simdjson_inline simdjson_result<std::string_view> unescape(raw_json_string in, uint8_t *&dst, bool allow_replacement = false) const noexcept;
390
412 simdjson_inline simdjson_result<std::string_view> unescape_wobbly(raw_json_string in, uint8_t *&dst) const noexcept;
413
414#if SIMDJSON_DEVELOPMENT_CHECKS
422 bool string_buffer_overflow(const uint8_t *string_buf_loc) const noexcept;
423#endif
424
436 static simdjson_inline simdjson_warn_unused ondemand::parser& get_parser();
442 static simdjson_inline bool release_parser();
443
444private:
446 static simdjson_inline simdjson_warn_unused std::unique_ptr<ondemand::parser>& get_parser_instance();
448 static simdjson_inline simdjson_warn_unused std::unique_ptr<ondemand::parser>& get_threadlocal_parser_if_exists();
450 std::unique_ptr<simdjson::internal::dom_parser_implementation> implementation{};
451 size_t _capacity{0};
452 size_t _max_capacity;
453 size_t _max_depth{DEFAULT_MAX_DEPTH};
454 size_t _document_len{0};
455 std::unique_ptr<uint8_t[]> string_buf{};
456
457#if SIMDJSON_DEVELOPMENT_CHECKS
458 std::unique_ptr<token_position[]> start_positions{};
459#endif
460
461 friend class json_iterator;
462 friend class document_stream;
463};
464
465} // namespace ondemand
466} // namespace SIMDJSON_IMPLEMENTATION
467} // namespace simdjson
468
469namespace simdjson {
470
471template<>
472struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::parser> {
473public:
474 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::parser &&value) noexcept;
475 simdjson_inline simdjson_result(error_code error) noexcept;
476 simdjson_inline simdjson_result() noexcept = default;
477};
478
479} // namespace simdjson
480
481#endif // SIMDJSON_GENERIC_ONDEMAND_PARSER_H
simdjson_warn_unused simdjson_result< document > iterate(padded_string_view json) &noexcept
Start iterating an on-demand JSON document.
Definition parser-inl.h:54
simdjson_pure simdjson_inline size_t max_capacity() const noexcept
The maximum capacity of this parser (the largest document it is allowed to process).
Definition parser-inl.h:248
simdjson_inline simdjson_result< std::string_view > unescape(raw_json_string in, uint8_t *&dst, bool allow_replacement=false) const noexcept
Unescape this JSON string, replacing \ with \, with newline, etc.
Definition parser-inl.h:263
static simdjson_inline bool release_parser()
Release the parser instance initialized by get_parser() and all the associated resources (memory).
Definition parser-inl.h:283
~parser() noexcept=default
Deallocate the JSON parser.
simdjson_pure simdjson_inline size_t max_depth() const noexcept
The maximum depth of this parser (the most deeply nested objects and arrays it can process).
Definition parser-inl.h:251
simdjson_pure simdjson_inline size_t capacity() const noexcept
The capacity of this parser (the largest document it can process).
Definition parser-inl.h:245
simdjson_inline simdjson_result< std::string_view > unescape_wobbly(raw_json_string in, uint8_t *&dst) const noexcept
Unescape this JSON string, replacing \ with \, with newline, etc.
Definition parser-inl.h:271
bool threaded
When SIMDJSON_THREADS_ENABLED is not defined, the parser instance cannot use threads.
Definition parser.h:365
simdjson_result< document_stream > iterate_many(const uint8_t *buf, size_t len, size_t batch_size=DEFAULT_BATCH_SIZE) noexcept
Parse a buffer containing many JSON documents.
Definition parser-inl.h:141
simdjson_warn_unused error_code allocate(size_t capacity, size_t max_depth=DEFAULT_MAX_DEPTH) noexcept
Ensure this parser has enough memory to process JSON documents up to capacity bytes in length and max...
Definition parser-inl.h:24
static simdjson_inline simdjson_warn_unused ondemand::parser & get_parser()
Get a unique parser instance corresponding to the current thread.
Definition parser-inl.h:279
A string escaped per JSON rules, terminated with quote (").
An implementation of simdjson for a particular CPU architecture.
User-provided string that promises it has extra padded bytes at the end for use with parser::parse().
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
stream_format
Stream format for parse_many/iterate_many.
Definition base.h:61
constexpr size_t DEFAULT_MAX_DEPTH
By default, simdjson supports this many nested objects and arrays.
Definition base.h:49
error_code
All possible errors returned by simdjson.
Definition error.h:19
SIMDJSON_PUSH_DISABLE_UNUSED_WARNINGS constexpr size_t SIMDJSON_MAXSIZE_BYTES
The maximum document size supported by simdjson.
Definition base.h:28
String with extra allocation for ease of use with parser::parse()
The result of a simdjson operation that could fail.
Definition error.h:281
simdjson_inline error_code error() const noexcept
The error.
Definition error-inl.h:168