simdjson 5.0.1
Ridiculously Fast JSON
Loading...
Searching...
No Matches
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
7namespace simdjson {
8
9namespace dom {
10
30class parser {
31public:
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
105 inline simdjson_result<element> load(std::string_view path) & noexcept;
106 inline simdjson_result<element> load(std::string_view path) && = delete ;
107
151 inline simdjson_result<element> load_into_document(document& doc, std::string_view path) & noexcept;
152 inline simdjson_result<element> load_into_document(document& doc, std::string_view path) && =delete;
153
234 inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
235 inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
237 simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
238 simdjson_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
240 simdjson_inline simdjson_result<element> parse(const std::string &s) & noexcept;
241 simdjson_inline simdjson_result<element> parse(const std::string &s) && =delete;
243 simdjson_inline simdjson_result<element> parse(const padded_string &s) & noexcept;
244 simdjson_inline simdjson_result<element> parse(const padded_string &s) && =delete;
246 simdjson_inline simdjson_result<element> parse(const padded_string_view &v) & noexcept;
247 simdjson_inline simdjson_result<element> parse(const padded_string_view &v) && =delete;
248
250 simdjson_inline simdjson_result<element> parse(const char *buf) noexcept = delete;
251
286 inline simdjson_result<element> parse_unpadded(const uint8_t *buf, size_t len) & noexcept;
287 inline simdjson_result<element> parse_unpadded(const uint8_t *buf, size_t len) && =delete;
289 simdjson_inline simdjson_result<element> parse_unpadded(const char *buf, size_t len) & noexcept;
290 simdjson_inline simdjson_result<element> parse_unpadded(const char *buf, size_t len) && =delete;
292 simdjson_inline simdjson_result<element> parse_unpadded(std::string_view s) & noexcept;
293 simdjson_inline simdjson_result<element> parse_unpadded(std::string_view s) && =delete;
294
307 inline simdjson_result<element> parse_into_document_unpadded(document& doc, const uint8_t *buf, size_t len) & noexcept;
308 inline simdjson_result<element> parse_into_document_unpadded(document& doc, const uint8_t *buf, size_t len) && =delete;
309
354 inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) & noexcept;
355 inline simdjson_result<element> parse_into_document(document& doc, const uint8_t *buf, size_t len, bool realloc_if_needed = true) && =delete;
357 simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) & noexcept;
358 simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf, size_t len, bool realloc_if_needed = true) && =delete;
360 simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) & noexcept;
361 simdjson_inline simdjson_result<element> parse_into_document(document& doc, const std::string &s) && =delete;
363 simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) & noexcept;
364 simdjson_inline simdjson_result<element> parse_into_document(document& doc, const padded_string &s) && =delete;
365
367 simdjson_inline simdjson_result<element> parse_into_document(document& doc, const char *buf) noexcept = delete;
368
444 inline simdjson_result<document_stream> load_many(std::string_view path, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
445
542 inline simdjson_result<document_stream> parse_many(const uint8_t *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
544 inline simdjson_result<document_stream> parse_many(const char *buf, size_t len, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
546 inline simdjson_result<document_stream> parse_many(const std::string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
547 inline simdjson_result<document_stream> parse_many(const std::string &&s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) = delete;// unsafe
549 inline simdjson_result<document_stream> parse_many(const padded_string &s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
550 inline simdjson_result<document_stream> parse_many(const padded_string &&s, size_t batch_size = dom::DEFAULT_BATCH_SIZE) = delete;// unsafe
560 inline simdjson_result<document_stream> parse_many(const padded_string_view &v, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept;
561
563 simdjson_result<document_stream> parse_many(const char *buf, size_t batch_size = dom::DEFAULT_BATCH_SIZE) noexcept = delete;
564
574 inline simdjson_result<document_stream> parse_many(const uint8_t *buf, size_t len, size_t batch_size, stream_format format) noexcept;
576 inline simdjson_result<document_stream> parse_many(const char *buf, size_t len, size_t batch_size, stream_format format) noexcept;
578 inline simdjson_result<document_stream> parse_many(const std::string &s, size_t batch_size, stream_format format) noexcept;
580 inline simdjson_result<document_stream> parse_many(const padded_string &s, size_t batch_size, stream_format format) noexcept;
582 inline simdjson_result<document_stream> parse_many(const padded_string_view &v, size_t batch_size, stream_format format) noexcept;
587 inline simdjson_result<document_stream> parse_many(const std::string &&s, size_t batch_size, stream_format format) = delete;// unsafe
589 inline simdjson_result<document_stream> parse_many(const padded_string &&s, size_t batch_size, stream_format format) = delete;// unsafe
590
599 simdjson_warn_unused inline error_code allocate(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
600
601#ifndef SIMDJSON_DISABLE_DEPRECATED_API
613 [[deprecated("Use allocate() instead.")]]
614 simdjson_warn_unused inline bool allocate_capacity(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
615#endif // SIMDJSON_DISABLE_DEPRECATED_API
621 simdjson_inline size_t capacity() const noexcept;
622
630 simdjson_inline size_t max_capacity() const noexcept;
631
637 simdjson_pure simdjson_inline size_t max_depth() const noexcept;
638
653 simdjson_inline void set_max_capacity(size_t max_capacity) noexcept;
654
655#ifdef SIMDJSON_THREADS_ENABLED
661 bool threaded{true};
662#else
666 bool threaded{false};
667#endif
669 class Iterator;
671 using InvalidJSON [[deprecated("Use simdjson_error instead")]] = simdjson_error;
672
674 std::unique_ptr<internal::dom_parser_implementation> implementation{};
675
677 bool valid{false};
680
682 document doc{};
683
685 [[deprecated("Use the result of parser.parse() instead")]]
686 inline bool is_valid() const noexcept;
687
692 [[deprecated("Use the result of parser.parse() instead")]]
693 inline int get_error_code() const noexcept;
694
696 [[deprecated("Use error_message() on the result of parser.parse() instead, or cout << error")]]
697 inline std::string get_error_message() const noexcept;
698
700 [[deprecated("Use cout << on the result of parser.parse() instead")]]
701 inline bool print_json(std::ostream &os) const noexcept;
702
704 inline bool dump_raw_tape(std::ostream &os) const noexcept;
705
706
711 inline void number_as_string(bool enabled) noexcept { _number_as_string = enabled; }
712 inline bool number_as_string() const noexcept { return _number_as_string; }
713
714private:
720 size_t _max_capacity;
721
723 bool _number_as_string{false};
724
728 std::unique_ptr<char[]> loaded_bytes;
729
731 size_t _loaded_bytes_capacity{0};
732
733 // all nodes are stored on the doc.tape using a 64-bit word.
734 //
735 // strings, double and ints are stored as
736 // a 64-bit word with a pointer to the actual value
737 //
738 //
739 //
740 // for objects or arrays, store [ or { at the beginning and } and ] at the
741 // end. For the openings ([ or {), we annotate them with a reference to the
742 // location on the doc.tape of the end, and for then closings (} and ]), we
743 // annotate them with a reference to the location of the opening
744 //
745 //
746
752 inline error_code ensure_capacity(size_t desired_capacity) noexcept;
758 inline error_code ensure_capacity(document& doc, size_t desired_capacity) noexcept;
759
761 inline simdjson_result<size_t> read_file(std::string_view path) noexcept;
762
763 friend class parser::Iterator;
764 friend class document_stream;
765
766
767}; // class parser
768
769} // namespace dom
770} // namespace simdjson
771
772#endif // SIMDJSON_DOM_PARSER_H
A parsed JSON document.
Definition document.h:16
A persistent document parser.
Definition parser.h:30
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:178
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:114
simdjson_inline parser(parser &&other) noexcept
Take another parser's buffers and state.
bool threaded
When SIMDJSON_THREADS_ENABLED is not defined, the parser instance cannot use threads.
Definition parser.h:666
simdjson_inline void set_max_capacity(size_t max_capacity) noexcept
Set max_capacity.
Definition parser-inl.h:319
simdjson_result< element > parse_unpadded(const uint8_t *buf, size_t len) &noexcept
Parse a JSON document whose buffer is not padded, in place and without copying it.
Definition parser-inl.h:195
simdjson_result< element > parse_into_document_unpadded(document &doc, const uint8_t *buf, size_t len) &noexcept
Parse a non-padded JSON document into a caller-provided document instance, in place and without copyi...
Definition parser-inl.h:144
simdjson_inline size_t max_capacity() const noexcept
The largest document this parser can automatically support.
Definition parser-inl.h:263
simdjson_result< document_stream > load_many(std::string_view path, size_t batch_size=dom::DEFAULT_BATCH_SIZE) noexcept
Load a file containing many JSON documents.
Definition parser-inl.h:106
simdjson_result< element > load_into_document(document &doc, std::string_view path) &noexcept
Load a JSON document from a file into a provide document instance and return a temporary reference to...
Definition parser-inl.h:99
simdjson_pure simdjson_inline size_t max_depth() const noexcept
The maximum level of nested object and arrays supported by this parser.
Definition parser-inl.h:266
simdjson_result< element > load(std::string_view path) &noexcept
Load a JSON document from a file and return a reference to it.
Definition parser-inl.h:95
~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:260
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:271
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:205
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
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:25
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
@ UNINITIALIZED
unknown error, or uninitialized document
Definition error.h:32
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()
Exception thrown when an exception-supporting simdjson method is called.
Definition error.h:92
The result of a simdjson operation that could fail.
Definition error.h:281