simdjson  3.7.0
Ridiculously Fast JSON
parser.h
1 #ifndef SIMDJSON_DOM_PARSER_H
2 #define SIMDJSON_DOM_PARSER_H
3 
4 #include "simdjson/dom/base.h"
5 #include "simdjson/dom/document.h"
6 
7 namespace simdjson {
8 
9 namespace dom {
10 
30 class parser {
31 public:
43  simdjson_inline explicit parser(size_t max_capacity = SIMDJSON_MAXSIZE_BYTES) noexcept;
49  simdjson_inline parser(parser &&other) noexcept;
50  parser(const parser &) = delete;
56  simdjson_inline parser &operator=(parser &&other) noexcept;
57  parser &operator=(const parser &) = delete;
58 
60  ~parser()=default;
61 
96  inline simdjson_result<element> load(const std::string &path) & noexcept;
97  inline simdjson_result<element> load(const std::string &path) && = delete ;
98 
142  inline simdjson_result<element> load_into_document(document& doc, const std::string &path) & noexcept;
143  inline simdjson_result<element> load_into_document(document& doc, const std::string &path) && =delete;
144 
212  inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
213  inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
215  simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
216  simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
218  simdjson_inline simdjson_result<element> parse(const std::string &s) & noexcept;
219  simdjson_inline simdjson_result<element> parse(const std::string &s) && =delete;
221  simdjson_inline simdjson_result<element> parse(const padded_string &s) & noexcept;
222  simdjson_inline simdjson_result<element> parse(const padded_string &s) && =delete;
224  simdjson_inline simdjson_result<element> parse(const padded_string_view &v) & noexcept;
225  simdjson_inline simdjson_result<element> parse(const padded_string_view &v) && =delete;
226 
228  simdjson_inline simdjson_result<element> parse(const char *buf) noexcept = delete;
229 
274  inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
275  inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
277  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
278  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
280  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) & noexcept;
281  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) && =delete;
283  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) & noexcept;
284  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) && =delete;
285 
287  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf) noexcept = delete;
288 
364  inline simdjson_result<document_stream> load_many(const std::string &path, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
365 
462  inline simdjson_result<document_stream> parse_many(const uint8_t *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
464  inline simdjson_result<document_stream> parse_many(const char *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
466  inline simdjson_result<document_stream> parse_many(const std::string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
467  inline simdjson_result<document_stream> parse_many(const std::string &&s, size_t batch_size) = delete;// unsafe
469  inline simdjson_result<document_stream> parse_many(const padded_string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
470  inline simdjson_result<document_stream> parse_many(const padded_string &&s, size_t batch_size) = delete;// unsafe
471 
473  simdjson_result<document_stream> parse_many(const char *buf, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept = delete;
474 
483  simdjson_warn_unused inline error_code allocate(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
484 
485 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
497  [[deprecated("Use allocate() instead.")]]
498  simdjson_warn_unused inline bool allocate_capacity(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
499 #endif // SIMDJSON_DISABLE_DEPRECATED_API
505  simdjson_inline size_t capacity() const noexcept;
506 
514  simdjson_inline size_t max_capacity() const noexcept;
515 
521  simdjson_inline size_t max_depth() const noexcept;
522 
537  simdjson_inline void set_max_capacity(size_t max_capacity) noexcept;
538 
539 #ifdef SIMDJSON_THREADS_ENABLED
545  bool threaded{true};
546 #endif
548  class Iterator;
550  using InvalidJSON [[deprecated("Use simdjson_error instead")]] = simdjson_error;
551 
553  std::unique_ptr<internal::dom_parser_implementation> implementation{};
554 
556  bool valid{false};
558  error_code error{UNINITIALIZED};
559 
561  document doc{};
562 
564  [[deprecated("Use the result of parser.parse() instead")]]
565  inline bool is_valid() const noexcept;
566 
571  [[deprecated("Use the result of parser.parse() instead")]]
572  inline int get_error_code() const noexcept;
573 
575  [[deprecated("Use error_message() on the result of parser.parse() instead, or cout << error")]]
576  inline std::string get_error_message() const noexcept;
577 
579  [[deprecated("Use cout << on the result of parser.parse() instead")]]
580  inline bool print_json(std::ostream &os) const noexcept;
581 
583  inline bool dump_raw_tape(std::ostream &os) const noexcept;
584 
585 
586 private:
592  size_t _max_capacity;
593 
597  std::unique_ptr<char[]> loaded_bytes;
598 
600  size_t _loaded_bytes_capacity{0};
601 
602  // all nodes are stored on the doc.tape using a 64-bit word.
603  //
604  // strings, double and ints are stored as
605  // a 64-bit word with a pointer to the actual value
606  //
607  //
608  //
609  // for objects or arrays, store [ or { at the beginning and } and ] at the
610  // end. For the openings ([ or {), we annotate them with a reference to the
611  // location on the doc.tape of the end, and for then closings (} and ]), we
612  // annotate them with a reference to the location of the opening
613  //
614  //
615 
621  inline error_code ensure_capacity(size_t desired_capacity) noexcept;
627  inline error_code ensure_capacity(document& doc, size_t desired_capacity) noexcept;
628 
630  inline simdjson_result<size_t> read_file(const std::string &path) noexcept;
631 
632  friend class parser::Iterator;
633  friend class document_stream;
634 
635 
636 }; // class parser
637 
638 } // namespace dom
639 } // namespace simdjson
640 
641 #endif // SIMDJSON_DOM_PARSER_H
A parsed JSON document.
Definition: document.h:16
A persistent document parser.
Definition: parser.h:30
simdjson_inline parser(size_t max_capacity=SIMDJSON_MAXSIZE_BYTES) noexcept
Create a JSON parser.
Definition: parser-inl.h:23
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:153
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:113
simdjson_inline size_t max_depth() const noexcept
The maximum level of nested object and arrays supported by this parser.
Definition: parser-inl.h:194
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:105
simdjson_inline void set_max_capacity(size_t max_capacity) noexcept
Set max_capacity.
Definition: parser-inl.h:247
simdjson_inline size_t max_capacity() const noexcept
The largest document this parser can automatically support.
Definition: parser-inl.h:191
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:94
simdjson_result< element > load_into_document(document &doc, const std::string &path) &noexcept
Load a JSON document from a file into a provide document instance and return a temporary reference to...
Definition: parser-inl.h:98
~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:188
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:199
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:170
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:21
const char * error_message(error_code error) noexcept
It is the convention throughout the code that the macro SIMDJSON_DEVELOPMENT_CHECKS determines whethe...
Definition: error-inl.h:20
constexpr size_t DEFAULT_MAX_DEPTH
By default, simdjson supports this many nested objects and arrays.
Definition: base.h:38
error_code
All possible errors returned by simdjson.
Definition: error.h:19
@ UNINITIALIZED
unknown error, or uninitialized document
Definition: error.h:32
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:23
Exception thrown when an exception-supporting simdjson method is called.
Definition: error.h:82
The result of a simdjson operation that could fail.
Definition: error.h:215