simdjson  3.1.6
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 ;
193  inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
194  inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
196  simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
197  simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
199  simdjson_inline simdjson_result<element> parse(const std::string &s) & noexcept;
200  simdjson_inline simdjson_result<element> parse(const std::string &s) && =delete;
202  simdjson_inline simdjson_result<element> parse(const padded_string &s) & noexcept;
203  simdjson_inline simdjson_result<element> parse(const padded_string &s) && =delete;
205  simdjson_inline simdjson_result<element> parse(const padded_string_view &v) & noexcept;
206  simdjson_inline simdjson_result<element> parse(const padded_string_view &v) && =delete;
207 
209  simdjson_inline simdjson_result<element> parse(const char *buf) noexcept = delete;
210 
255  inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
256  inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
258  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
259  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
261  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) & noexcept;
262  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) && =delete;
264  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) & noexcept;
265  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) && =delete;
266 
268  simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf) noexcept = delete;
269 
343  inline simdjson_result<document_stream> load_many(const std::string &path, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
344 
439  inline simdjson_result<document_stream> parse_many(const uint8_t *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
441  inline simdjson_result<document_stream> parse_many(const char *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
443  inline simdjson_result<document_stream> parse_many(const std::string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
444  inline simdjson_result<document_stream> parse_many(const std::string &&s, size_t batch_size) = delete;// unsafe
446  inline simdjson_result<document_stream> parse_many(const padded_string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
447  inline simdjson_result<document_stream> parse_many(const padded_string &&s, size_t batch_size) = delete;// unsafe
448 
450  simdjson_result<document_stream> parse_many(const char *buf, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept = delete;
451 
460  simdjson_warn_unused inline error_code allocate(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
461 
462 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
474  [[deprecated("Use allocate() instead.")]]
475  simdjson_warn_unused inline bool allocate_capacity(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
476 #endif // SIMDJSON_DISABLE_DEPRECATED_API
482  simdjson_inline size_t capacity() const noexcept;
483 
491  simdjson_inline size_t max_capacity() const noexcept;
492 
498  simdjson_inline size_t max_depth() const noexcept;
499 
514  simdjson_inline void set_max_capacity(size_t max_capacity) noexcept;
515 
516 #ifdef SIMDJSON_THREADS_ENABLED
522  bool threaded{true};
523 #endif
525  class Iterator;
527  using InvalidJSON [[deprecated("Use simdjson_error instead")]] = simdjson_error;
528 
530  std::unique_ptr<internal::dom_parser_implementation> implementation{};
531 
533  bool valid{false};
535  error_code error{UNINITIALIZED};
536 
538  document doc{};
539 
541  [[deprecated("Use the result of parser.parse() instead")]]
542  inline bool is_valid() const noexcept;
543 
548  [[deprecated("Use the result of parser.parse() instead")]]
549  inline int get_error_code() const noexcept;
550 
552  [[deprecated("Use error_message() on the result of parser.parse() instead, or cout << error")]]
553  inline std::string get_error_message() const noexcept;
554 
556  [[deprecated("Use cout << on the result of parser.parse() instead")]]
557  inline bool print_json(std::ostream &os) const noexcept;
558 
560  inline bool dump_raw_tape(std::ostream &os) const noexcept;
561 
562 
563 private:
569  size_t _max_capacity;
570 
574  std::unique_ptr<char[]> loaded_bytes;
575 
577  size_t _loaded_bytes_capacity{0};
578 
579  // all nodes are stored on the doc.tape using a 64-bit word.
580  //
581  // strings, double and ints are stored as
582  // a 64-bit word with a pointer to the actual value
583  //
584  //
585  //
586  // for objects or arrays, store [ or { at the beginning and } and ] at the
587  // end. For the openings ([ or {), we annotate them with a reference to the
588  // location on the doc.tape of the end, and for then closings (} and ]), we
589  // annotate them with a reference to the location of the opening
590  //
591  //
592 
598  inline error_code ensure_capacity(size_t desired_capacity) noexcept;
604  inline error_code ensure_capacity(document& doc, size_t desired_capacity) noexcept;
605 
607  inline simdjson_result<size_t> read_file(const std::string &path) noexcept;
608 
609  friend class parser::Iterator;
610  friend class document_stream;
611 
612 
613 }; // class parser
614 
615 } // namespace dom
616 } // namespace simdjson
617 
618 #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