simdjson  3.11.0
Ridiculously Fast JSON
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 
11 namespace simdjson {
12 namespace SIMDJSON_IMPLEMENTATION {
13 namespace ondemand {
14 
20 static constexpr size_t DEFAULT_BATCH_SIZE = 1000000;
29 static constexpr size_t MINIMAL_BATCH_SIZE = 32;
30 
36 class parser {
37 public:
43  inline explicit parser(size_t max_capacity = SIMDJSON_MAXSIZE_BYTES) noexcept;
44 
45  inline parser(parser &&other) noexcept = default;
46  simdjson_inline parser(const parser &other) = delete;
47  simdjson_inline parser &operator=(const parser &other) = delete;
48  simdjson_inline parser &operator=(parser &&other) noexcept = default;
49 
51  inline ~parser() noexcept = default;
52 
116  simdjson_warn_unused simdjson_result<document> iterate(padded_string_view json) & noexcept;
117 #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
118  simdjson_warn_unused simdjson_result<document> iterate_allow_incomplete_json(padded_string_view json) & noexcept;
119 #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
121  simdjson_warn_unused simdjson_result<document> iterate(const char *json, size_t len, size_t capacity) & noexcept;
123  simdjson_warn_unused simdjson_result<document> iterate(const uint8_t *json, size_t len, size_t capacity) & noexcept;
125  simdjson_warn_unused simdjson_result<document> iterate(std::string_view json, size_t capacity) & noexcept;
127  simdjson_warn_unused simdjson_result<document> iterate(const std::string &json) & noexcept;
129  simdjson_warn_unused simdjson_result<document> iterate(std::string &json) & noexcept;
131  simdjson_warn_unused simdjson_result<document> iterate(const simdjson_result<padded_string> &json) & noexcept;
133  simdjson_warn_unused simdjson_result<document> iterate(const simdjson_result<padded_string_view> &json) & noexcept;
135  simdjson_warn_unused simdjson_result<document> iterate(padded_string &&json) & noexcept = delete;
136 
177  simdjson_warn_unused simdjson_result<json_iterator> iterate_raw(padded_string_view json) & noexcept;
178 
179 
250  inline simdjson_result<document_stream> iterate_many(const uint8_t *buf, size_t len, size_t batch_size = DEFAULT_BATCH_SIZE, bool allow_comma_separated = false) noexcept;
252  inline simdjson_result<document_stream> iterate_many(const char *buf, size_t len, size_t batch_size = DEFAULT_BATCH_SIZE, bool allow_comma_separated = false) noexcept;
254  inline simdjson_result<document_stream> iterate_many(const std::string &s, size_t batch_size = DEFAULT_BATCH_SIZE, bool allow_comma_separated = false) noexcept;
255  inline simdjson_result<document_stream> iterate_many(const std::string &&s, size_t batch_size, bool allow_comma_separated = false) = delete;// unsafe
257  inline simdjson_result<document_stream> iterate_many(const padded_string &s, size_t batch_size = DEFAULT_BATCH_SIZE, bool allow_comma_separated = false) noexcept;
258  inline simdjson_result<document_stream> iterate_many(const padded_string &&s, size_t batch_size, bool allow_comma_separated = false) = delete;// unsafe
259 
261  simdjson_result<document_stream> iterate_many(const char *buf, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept = delete;
262 
264  simdjson_pure simdjson_inline size_t capacity() const noexcept;
266  simdjson_pure simdjson_inline size_t max_capacity() const noexcept;
267  simdjson_inline void set_max_capacity(size_t max_capacity) noexcept;
274  simdjson_pure simdjson_inline size_t max_depth() const noexcept;
275 
288  simdjson_warn_unused error_code allocate(size_t capacity, size_t max_depth=DEFAULT_MAX_DEPTH) noexcept;
289 
290  #ifdef SIMDJSON_THREADS_ENABLED
296  bool threaded{true};
297  #else
301  bool threaded{false};
302  #endif
325  simdjson_inline simdjson_result<std::string_view> unescape(raw_json_string in, uint8_t *&dst, bool allow_replacement = false) const noexcept;
326 
348  simdjson_inline simdjson_result<std::string_view> unescape_wobbly(raw_json_string in, uint8_t *&dst) const noexcept;
349 
350 #if SIMDJSON_DEVELOPMENT_CHECKS
358  bool string_buffer_overflow(const uint8_t *string_buf_loc) const noexcept;
359 #endif
360 
361 private:
363  std::unique_ptr<simdjson::internal::dom_parser_implementation> implementation{};
364  size_t _capacity{0};
365  size_t _max_capacity;
366  size_t _max_depth{DEFAULT_MAX_DEPTH};
367  std::unique_ptr<uint8_t[]> string_buf{};
368 #if SIMDJSON_DEVELOPMENT_CHECKS
369  std::unique_ptr<token_position[]> start_positions{};
370 #endif
371 
372  friend class json_iterator;
373  friend class document_stream;
374 };
375 
376 } // namespace ondemand
377 } // namespace SIMDJSON_IMPLEMENTATION
378 } // namespace simdjson
379 
380 namespace simdjson {
381 
382 template<>
383 struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::parser> {
384 public:
386  simdjson_inline simdjson_result(error_code error) noexcept;
387  simdjson_inline simdjson_result() noexcept = default;
388 };
389 
390 } // namespace simdjson
391 
392 #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:51
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:161
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:176
~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:164
simdjson_pure simdjson_inline size_t capacity() const noexcept
The capacity of this parser (the largest document it can process).
Definition: parser-inl.h:158
parser(size_t max_capacity=SIMDJSON_MAXSIZE_BYTES) noexcept
Create a JSON parser.
Definition: parser-inl.h:20
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:184
bool threaded
When SIMDJSON_THREADS_ENABLED is not defined, the parser instance cannot use threads.
Definition: parser.h:301
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
simdjson_result< document_stream > iterate_many(const uint8_t *buf, size_t len, size_t batch_size=DEFAULT_BATCH_SIZE, bool allow_comma_separated=false) noexcept
Parse a buffer containing many JSON documents.
Definition: parser-inl.h:139
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
constexpr SIMDJSON_PUSH_DISABLE_UNUSED_WARNINGS size_t SIMDJSON_MAXSIZE_BYTES
The maximum document size supported by simdjson.
Definition: base.h:22
constexpr size_t DEFAULT_MAX_DEPTH
By default, simdjson supports this many nested objects and arrays.
Definition: base.h:39
error_code
All possible errors returned by simdjson.
Definition: error.h:19
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:23
The result of a simdjson operation that could fail.
Definition: error.h:215
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:131
simdjson_inline T & value() &noexcept(false)
Get the result value.