1 #ifndef SIMDJSON_INLINE_PARSER_H
2 #define SIMDJSON_INLINE_PARSER_H
4 #include "simdjson/dom/document_stream.h"
5 #include "simdjson/dom/parser.h"
6 #include "simdjson/implementation.h"
7 #include "simdjson/internal/jsonformatutils.h"
8 #include "simdjson/portability.h"
19 : _max_capacity{max_capacity},
20 loaded_bytes(
nullptr) {
25 inline bool parser::is_valid() const noexcept {
return valid; }
26 inline int parser::get_error_code() const noexcept {
return error; }
27 inline std::string parser::get_error_message() const noexcept {
return error_message(error); }
29 inline bool parser::dump_raw_tape(std::ostream &os)
const noexcept {
30 return valid ? doc.dump_raw_tape(os) :
false;
33 inline simdjson_result<size_t> parser::read_file(
const std::string &path) noexcept {
35 SIMDJSON_PUSH_DISABLE_WARNINGS
36 SIMDJSON_DISABLE_DEPRECATED_WARNING
37 std::FILE *fp = std::fopen(path.c_str(),
"rb");
38 SIMDJSON_POP_DISABLE_WARNINGS
46 #if SIMDJSON_VISUAL_STUDIO && !SIMDJSON_IS_32BITS
47 ret = _fseeki64(fp, 0, SEEK_END);
49 ret = std::fseek(fp, 0, SEEK_END);
55 #if SIMDJSON_VISUAL_STUDIO && !SIMDJSON_IS_32BITS
56 __int64 len = _ftelli64(fp);
62 long len = std::ftell(fp);
63 if((len < 0) || (len == LONG_MAX)) {
70 if (_loaded_bytes_capacity <
size_t(len)) {
71 loaded_bytes.reset( internal::allocate_padded_buffer(len) );
76 _loaded_bytes_capacity = len;
81 size_t bytes_read = std::fread(loaded_bytes.get(), 1, len, fp);
82 if (std::fclose(fp) != 0 || bytes_read !=
size_t(len)) {
91 auto _error = read_file(path).get(len);
92 if (_error) {
return _error; }
93 return parse(loaded_bytes.get(), len,
false);
98 auto _error = read_file(path).get(len);
99 if (_error) {
return _error; }
100 if(batch_size < MINIMAL_BATCH_SIZE) { batch_size = MINIMAL_BATCH_SIZE; }
101 return document_stream(*
this,
reinterpret_cast<const uint8_t*
>(loaded_bytes.get()), len, batch_size);
107 error_code _error = ensure_capacity(provided_doc, len);
108 if (_error) {
return _error; }
109 if (realloc_if_needed) {
111 if (!loaded_bytes || _loaded_bytes_capacity < len) {
112 loaded_bytes.reset( internal::allocate_padded_buffer(len) );
116 _loaded_bytes_capacity = len;
118 std::memcpy(
static_cast<void *
>(loaded_bytes.get()), buf, len);
120 _error =
implementation->parse(realloc_if_needed ?
reinterpret_cast<const uint8_t*
>(loaded_bytes.get()): buf, len, provided_doc);
122 if (_error) {
return _error; }
124 return provided_doc.root();
128 return parse_into_document(provided_doc,
reinterpret_cast<const uint8_t *
>(buf), len, realloc_if_needed);
131 return parse_into_document(provided_doc, s.data(), s.length(), s.capacity() - s.length() <
SIMDJSON_PADDING);
134 return parse_into_document(provided_doc, s.data(), s.length(),
false);
139 return parse_into_document(doc, buf, len, realloc_if_needed);
143 return parse(
reinterpret_cast<const uint8_t *
>(buf), len, realloc_if_needed);
146 return parse(s.data(), s.length(), s.capacity() - s.length() <
SIMDJSON_PADDING);
148 simdjson_inline simdjson_result<element>
parser::parse(
const padded_string &s) & noexcept {
149 return parse(s.data(), s.length(),
false);
151 simdjson_inline simdjson_result<element>
parser::parse(
const padded_string_view &v) & noexcept {
152 return parse(v.data(), v.length(),
false);
156 if(batch_size < MINIMAL_BATCH_SIZE) { batch_size = MINIMAL_BATCH_SIZE; }
160 return parse_many(
reinterpret_cast<const uint8_t *
>(buf), len, batch_size);
163 return parse_many(s.data(), s.length(), batch_size);
165 inline simdjson_result<document_stream>
parser::parse_many(
const padded_string &s,
size_t batch_size) noexcept {
166 return parse_many(s.data(), s.length(), batch_size);
173 return _max_capacity;
190 if (err) {
return err; }
194 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
196 inline bool parser::allocate_capacity(
size_t capacity,
size_t max_depth) noexcept {
197 return !allocate(capacity, max_depth);
201 inline error_code parser::ensure_capacity(
size_t desired_capacity) noexcept {
202 return ensure_capacity(doc, desired_capacity);
206 inline error_code parser::ensure_capacity(document& target_document,
size_t desired_capacity) noexcept {
209 if(desired_capacity < MINIMAL_DOCUMENT_CAPACITY) { desired_capacity = MINIMAL_DOCUMENT_CAPACITY; }
216 if (simdjson_unlikely(capacity() < desired_capacity || target_document.capacity() < desired_capacity)) {
217 if (desired_capacity > max_capacity()) {
220 error_code err1 = target_document.capacity() < desired_capacity ? target_document.allocate(desired_capacity) :
SUCCESS;
221 error_code err2 = capacity() < desired_capacity ? allocate(desired_capacity, max_depth()) :
SUCCESS;
222 if(err1 !=
SUCCESS) {
return error = err1; }
223 if(err2 !=
SUCCESS) {
return error = err2; }
229 if(max_capacity < MINIMAL_DOCUMENT_CAPACITY) {
230 _max_capacity = max_capacity;
232 _max_capacity = MINIMAL_DOCUMENT_CAPACITY;
A forward-only stream of documents.
simdjson_inline parser(size_t max_capacity=SIMDJSON_MAXSIZE_BYTES) noexcept
Create a JSON parser.
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.
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.
simdjson_inline size_t max_depth() const noexcept
The maximum level of nested object and arrays supported by this parser.
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.
simdjson_inline void set_max_capacity(size_t max_capacity) noexcept
Set max_capacity.
simdjson_inline size_t max_capacity() const noexcept
The largest document this parser can automatically support.
simdjson_result< element > load(const std::string &path) &noexcept
Load a JSON document from a file and return a reference to it.
simdjson_inline size_t capacity() const noexcept
The largest document this parser can support without reallocating.
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...
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.
An implementation of simdjson for a particular CPU architecture.
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.
constexpr size_t DEFAULT_MAX_DEPTH
By default, simdjson supports this many nested objects and arrays.
error_code
All possible errors returned by simdjson.
@ CAPACITY
This parser can't support a document that big.
@ MEMALLOC
Error allocating memory, most likely out of memory.
@ IO_ERROR
Error reading a file.
SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr< const implementation > & get_active_implementation()
The active implementation.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.
The result of a simdjson operation that could fail.