simdjson  3.1.1
Ridiculously Fast JSON
parser.h
1 #ifndef SIMDJSON_DOM_PARSER_H
2 #define SIMDJSON_DOM_PARSER_H
3 
4 #include "simdjson/common_defs.h"
5 #include "simdjson/dom/document.h"
6 #include "simdjson/error.h"
7 #include "simdjson/internal/dom_parser_implementation.h"
8 #include "simdjson/internal/tape_ref.h"
9 #include "simdjson/padded_string.h"
10 #include "simdjson/portability.h"
11 #include <memory>
12 #include <ostream>
13 #include <string>
14 
15 namespace simdjson {
16 
17 namespace dom {
18 
19 class document_stream;
20 class element;
21 
23 static constexpr size_t DEFAULT_BATCH_SIZE = 1000000;
32 static constexpr size_t MINIMAL_BATCH_SIZE = 32;
33 
37 static constexpr size_t MINIMAL_DOCUMENT_CAPACITY = 32;
38 
58 class parser {
59 public:
71  simdjson_inline explicit parser(size_t max_capacity = SIMDJSON_MAXSIZE_BYTES) noexcept;
77  simdjson_inline parser(parser &&other) noexcept;
78  parser(const parser &) = delete;
84  simdjson_inline parser &operator=(parser &&other) noexcept;
85  parser &operator=(const parser &) = delete;
86 
88  ~parser()=default;
89 
124  inline simdjson_result<element> load(const std::string &path) & noexcept;
125  inline simdjson_result<element> load(const std::string &path) && = delete ;
191  inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
192  inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
194  simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
195  simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
197  simdjson_inline simdjson_result<element> parse(const std::string &s) & noexcept;
198  simdjson_inline simdjson_result<element> parse(const std::string &s) && =delete;
200  simdjson_inline simdjson_result<element> parse(const padded_string &s) & noexcept;
201  simdjson_inline simdjson_result<element> parse(const padded_string &s) && =delete;
203  simdjson_inline simdjson_result<element> parse(const padded_string_view &v) & noexcept;
204  simdjson_inline simdjson_result<element> parse(const padded_string_view &v) && =delete;
205 
207  simdjson_inline simdjson_result<element> parse(const char *buf) noexcept = delete;
208 
253  inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
254  inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
256  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
257  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
259  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) & noexcept;
260  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) && =delete;
262  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) & noexcept;
263  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) && =delete;
264 
266  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf) noexcept = delete;
267 
341  inline simdjson_result<document_stream> load_many(const std::string &path, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
342 
435  inline simdjson_result<document_stream> parse_many(const uint8_t *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
437  inline simdjson_result<document_stream> parse_many(const char *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
439  inline simdjson_result<document_stream> parse_many(const std::string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
440  inline simdjson_result<document_stream> parse_many(const std::string &&s, size_t batch_size) = delete;// unsafe
442  inline simdjson_result<document_stream> parse_many(const padded_string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
443  inline simdjson_result<document_stream> parse_many(const padded_string &&s, size_t batch_size) = delete;// unsafe
444 
446  simdjson_result<document_stream> parse_many(const char *buf, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept = delete;
447 
456  simdjson_warn_unused inline error_code allocate(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
457 
458 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
470  [[deprecated("Use allocate() instead.")]]
471  simdjson_warn_unused inline bool allocate_capacity(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
472 #endif // SIMDJSON_DISABLE_DEPRECATED_API
478  simdjson_inline size_t capacity() const noexcept;
479 
487  simdjson_inline size_t max_capacity() const noexcept;
488 
494  simdjson_inline size_t max_depth() const noexcept;
495 
510  simdjson_inline void set_max_capacity(size_t max_capacity) noexcept;
511 
512 #ifdef SIMDJSON_THREADS_ENABLED
518  bool threaded{true};
519 #endif
521  class Iterator;
523  using InvalidJSON [[deprecated("Use simdjson_error instead")]] = simdjson_error;
524 
526  std::unique_ptr<internal::dom_parser_implementation> implementation{};
527 
529  bool valid{false};
531  error_code error{UNINITIALIZED};
532 
534  document doc{};
535 
537  [[deprecated("Use the result of parser.parse() instead")]]
538  inline bool is_valid() const noexcept;
539 
544  [[deprecated("Use the result of parser.parse() instead")]]
545  inline int get_error_code() const noexcept;
546 
548  [[deprecated("Use error_message() on the result of parser.parse() instead, or cout << error")]]
549  inline std::string get_error_message() const noexcept;
550 
552  [[deprecated("Use cout << on the result of parser.parse() instead")]]
553  inline bool print_json(std::ostream &os) const noexcept;
554 
556  inline bool dump_raw_tape(std::ostream &os) const noexcept;
557 
558 
559 private:
565  size_t _max_capacity;
566 
570  std::unique_ptr<char[]> loaded_bytes;
571 
573  size_t _loaded_bytes_capacity{0};
574 
575  // all nodes are stored on the doc.tape using a 64-bit word.
576  //
577  // strings, double and ints are stored as
578  // a 64-bit word with a pointer to the actual value
579  //
580  //
581  //
582  // for objects or arrays, store [ or { at the beginning and } and ] at the
583  // end. For the openings ([ or {), we annotate them with a reference to the
584  // location on the doc.tape of the end, and for then closings (} and ]), we
585  // annotate them with a reference to the location of the opening
586  //
587  //
588 
594  inline error_code ensure_capacity(size_t desired_capacity) noexcept;
600  inline error_code ensure_capacity(document& doc, size_t desired_capacity) noexcept;
601 
603  inline simdjson_result<size_t> read_file(const std::string &path) noexcept;
604 
605  friend class parser::Iterator;
606  friend class document_stream;
607 
608 
609 }; // class parser
610 
611 } // namespace dom
612 } // namespace simdjson
613 
614 #endif // SIMDJSON_DOM_PARSER_H
A parsed JSON document.
Definition: document.h:18
A persistent document parser.
Definition: parser.h:58
simdjson_inline parser(size_t max_capacity=SIMDJSON_MAXSIZE_BYTES) noexcept
Create a JSON parser.
Definition: parser-inl.h:18
simdjson_result< element > parse(const uint8_t *buf, size_t len, bool realloc_if_needed=true) &noexcept
Parse a JSON document and return a temporary reference to it.
Definition: parser-inl.h:138
simdjson_result< element > parse_into_document(document &doc, const uint8_t *buf, size_t len, bool realloc_if_needed=true) &noexcept
Parse a JSON document into a provide document instance and return a temporary reference to it.
Definition: parser-inl.h:104
simdjson_inline size_t max_depth() const noexcept
The maximum level of nested object and arrays supported by this parser.
Definition: parser-inl.h:175
simdjson_inline parser(parser &&other) noexcept
Take another parser's buffers and state.
simdjson_result< document_stream > load_many(const std::string &path, size_t batch_size=dom::DEFAULT_BATCH_SIZE) noexcept
Load a file containing many JSON documents.
Definition: parser-inl.h:96
simdjson_inline void set_max_capacity(size_t max_capacity) noexcept
Set max_capacity.
Definition: parser-inl.h:228
simdjson_inline size_t max_capacity() const noexcept
The largest document this parser can automatically support.
Definition: parser-inl.h:172
simdjson_result< element > load(const std::string &path) &noexcept
Load a JSON document from a file and return a reference to it.
Definition: parser-inl.h:89
~parser()=default
Deallocate the JSON parser.
simdjson_inline size_t capacity() const noexcept
The largest document this parser can support without reallocating.
Definition: parser-inl.h:169
simdjson_inline parser & operator=(parser &&other) noexcept
Take another parser's buffers and state.
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:180
simdjson_result< document_stream > parse_many(const uint8_t *buf, size_t len, size_t batch_size=dom::DEFAULT_BATCH_SIZE) noexcept
Parse a buffer containing many JSON documents.
Definition: parser-inl.h:155
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().
We want to support argument-dependent lookup (ADL).
const char * error_message(error_code error) noexcept
Get the error message for the given error code.
Definition: error-inl.h:21
constexpr size_t DEFAULT_MAX_DEPTH
By default, simdjson supports this many nested objects and arrays.
Definition: common_defs.h:52
error_code
All possible errors returned by simdjson.
Definition: error.h:17
@ UNINITIALIZED
unknown error, or uninitialized document
Definition: error.h:29
constexpr size_t SIMDJSON_MAXSIZE_BYTES
The maximum document size supported by simdjson.
Definition: common_defs.h:35
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:21
Exception thrown when an exception-supporting simdjson method is called.
Definition: error.h:72
The result of a simdjson operation that could fail.
Definition: error.h:205