1 #ifndef SIMDJSON_DOCUMENT_H
2 #define SIMDJSON_DOCUMENT_H
9 #include "simdjson/common_defs.h"
11 #include "simdjson/padded_string.h"
13 namespace simdjson::dom {
21 class document_stream;
24 static constexpr
size_t DEFAULT_BATCH_SIZE = 1000000;
30 template<>
struct simdjson_result<dom::element>;
31 template<> struct simdjson_result<dom::array>;
32 template<> struct simdjson_result<dom::object>;
39 namespace simdjson::internal {
41 using namespace simdjson::dom;
43 constexpr
const uint64_t JSON_VALUE_MASK = 0x00FFFFFFFFFFFFFF;
48 enum class tape_type {
68 really_inline tape_ref() noexcept;
69 really_inline tape_ref(
const document *doc,
size_t json_index) noexcept;
70 inline size_t after_element()
const noexcept;
71 really_inline tape_type tape_ref_type()
const noexcept;
72 really_inline uint64_t tape_value()
const noexcept;
74 really_inline T next_tape_value()
const noexcept;
75 inline std::string_view get_string_view()
const noexcept;
86 namespace simdjson::dom {
92 enum class element_type {
106 class array :
protected internal::tape_ref {
109 really_inline
array() noexcept;
116 inline element operator*()
const noexcept;
122 inline void operator++() noexcept;
128 inline bool operator!=(
const iterator& other)
const noexcept;
139 inline iterator begin() const noexcept;
145 inline
iterator end() const noexcept;
172 really_inline
array(const
document *doc,
size_t json_index) noexcept;
176 friend class simdjson::
minify;
182 class
object : protected internal::tape_ref {
185 really_inline
object() noexcept;
198 inline void operator++() noexcept;
204 inline bool operator!=(
const iterator& other)
const noexcept;
208 inline std::string_view key()
const noexcept;
212 inline const char *key_c_str()
const noexcept;
227 inline iterator begin() const noexcept;
233 inline
iterator end() const noexcept;
306 really_inline
object(const
document *doc,
size_t json_index) noexcept;
310 friend class simdjson::
minify;
356 bool dump_raw_tape(std::ostream &os)
const noexcept;
359 std::unique_ptr<uint64_t[]> tape;
365 std::unique_ptr<uint8_t[]> string_buf;
368 inline error_code allocate(
size_t len) noexcept;
380 class element :
protected internal::tape_ref {
383 really_inline
element() noexcept;
386 really_inline element_type type()
const noexcept;
389 really_inline
bool is_null()
const noexcept;
401 really_inline
bool is()
const noexcept;
418 #if SIMDJSON_EXCEPTIONS
425 inline operator bool()
const noexcept(
false);
436 inline explicit operator const char*()
const noexcept(
false);
447 inline operator std::string_view()
const noexcept(
false);
456 inline operator uint64_t()
const noexcept(
false);
464 inline operator int64_t()
const noexcept(
false);
472 inline operator double()
const noexcept(
false);
479 inline operator array()
const noexcept(
false);
486 inline operator object()
const noexcept(
false);
503 #endif // SIMDJSON_EXCEPTIONS
585 inline bool dump_raw_tape(std::ostream &out)
const noexcept;
588 really_inline
element(
const document *doc,
size_t json_index) noexcept;
602 std::string_view key;
633 really_inline
parser(
size_t max_capacity = SIMDJSON_MAXSIZE_BYTES) noexcept;
780 inline document_stream load_many(
const std::string &path,
size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
842 inline document_stream parse_many(
const uint8_t *buf,
size_t len,
size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
844 inline document_stream parse_many(
const char *buf,
size_t len,
size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
846 inline document_stream parse_many(
const std::string &s,
size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
851 really_inline
simdjson_result<element> parse_many(
const char *buf,
size_t batch_size = DEFAULT_BATCH_SIZE) noexcept =
delete;
861 WARN_UNUSED
inline error_code allocate(
size_t capacity,
size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
874 [[deprecated(
"Use allocate() instead.")]]
875 WARN_UNUSED
inline bool allocate_capacity(
size_t capacity,
size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
882 really_inline
size_t capacity()
const noexcept;
891 really_inline
size_t max_capacity()
const noexcept;
898 really_inline
size_t max_depth()
const noexcept;
909 really_inline
void set_max_capacity(
size_t max_capacity) noexcept;
917 uint32_t current_loc{0};
920 uint32_t n_structural_indexes{0};
922 std::unique_ptr<uint32_t[]> structural_indexes;
925 std::unique_ptr<uint32_t[]> containing_scope_offset;
926 #ifdef SIMDJSON_USE_COMPUTED_GOTO
928 std::unique_ptr<void*[]> ret_address;
931 std::unique_ptr<char[]> ret_address;
935 uint8_t *current_string_buf_loc;
940 error_code
error{UNINITIALIZED};
946 [[deprecated(
"Use the result of parser.parse() instead")]]
947 inline bool is_valid()
const noexcept;
953 [[deprecated(
"Use the result of parser.parse() instead")]]
954 inline int get_error_code()
const noexcept;
957 [[deprecated(
"Use error_message() on the result of parser.parse() instead, or cout << error")]]
958 inline std::string get_error_message()
const noexcept;
961 [[deprecated(
"Use cout << on the result of parser.parse() instead")]]
962 inline bool print_json(std::ostream &os)
const noexcept;
965 inline bool dump_raw_tape(std::ostream &os)
const noexcept;
972 inline void init_stage2() noexcept;
973 really_inline error_code on_error(error_code new_error_code) noexcept;
974 really_inline error_code on_success(error_code success_code) noexcept;
975 really_inline
bool on_start_document(uint32_t depth) noexcept;
976 really_inline
bool on_start_object(uint32_t depth) noexcept;
977 really_inline
bool on_start_array(uint32_t depth) noexcept;
979 really_inline
bool on_end_document(uint32_t depth) noexcept;
980 really_inline
bool on_end_object(uint32_t depth) noexcept;
981 really_inline
bool on_end_array(uint32_t depth) noexcept;
982 really_inline
bool on_true_atom() noexcept;
983 really_inline
bool on_false_atom() noexcept;
984 really_inline
bool on_null_atom() noexcept;
985 really_inline uint8_t *on_start_string() noexcept;
986 really_inline
bool on_end_string(uint8_t *dst) noexcept;
987 really_inline
bool on_number_s64(int64_t
value) noexcept;
988 really_inline
bool on_number_u64(uint64_t
value) noexcept;
989 really_inline
bool on_number_double(
double value) noexcept;
997 size_t _max_capacity;
1004 size_t _capacity{0};
1011 size_t _max_depth{0};
1016 std::unique_ptr<
char[], decltype(&aligned_free_char)> loaded_bytes;
1019 size_t _loaded_bytes_capacity{0};
1035 inline void write_tape(uint64_t val, internal::tape_type t) noexcept;
1036 inline void annotate_previous_loc(uint32_t saved_loc, uint64_t val) noexcept;
1042 inline error_code ensure_capacity(
size_t desired_capacity) noexcept;
1053 namespace simdjson {
1063 template<
typename T>
1076 inline operator std::string() const noexcept { std::stringstream s; s << *
this;
return s.str(); }
1081 inline std::ostream& print(std::ostream& out);
1093 template<
typename T>
1094 inline std::ostream& operator<<(std::ostream& out, minify<T> formatter) {
return formatter.print(out); }
1110 inline std::ostream& operator<<(std::ostream& out,
const element &
value) {
return out << minify(
value); }
1120 inline std::ostream& operator<<(std::ostream& out,
const array &
value) {
return out << minify(
value); }
1130 inline std::ostream& operator<<(std::ostream& out,
const object &
value) {
return out << minify(
value); }
1149 inline std::ostream& operator<<(std::ostream& out, element_type type) {
1151 case element_type::ARRAY:
1152 return out <<
"array";
1153 case element_type::OBJECT:
1154 return out <<
"object";
1155 case element_type::INT64:
1156 return out <<
"int64_t";
1157 case element_type::UINT64:
1158 return out <<
"uint64_t";
1159 case element_type::DOUBLE:
1160 return out <<
"double";
1161 case element_type::STRING:
1162 return out <<
"string";
1163 case element_type::BOOL:
1164 return out <<
"bool";
1165 case element_type::NULL_VALUE:
1166 return out <<
"null";
1174 #if SIMDJSON_EXCEPTIONS
1187 inline std::ostream& operator<<(std::ostream& out,
const simdjson_result<dom::element> &
value) noexcept(
false) {
return out << minify(
value); }
1199 inline std::ostream& operator<<(std::ostream& out,
const simdjson_result<dom::array> &
value) noexcept(
false) {
return out << minify(
value); }
1211 inline std::ostream& operator<<(std::ostream& out,
const simdjson_result<dom::object> &
value) noexcept(
false) {
return out << minify(
value); }
1225 template<
typename T>
1227 template<
typename T>
1237 #if SIMDJSON_EXCEPTIONS
1238 inline operator bool()
const noexcept(
false);
1239 inline explicit operator const char*()
const noexcept(
false);
1240 inline operator std::string_view()
const noexcept(
false);
1241 inline operator uint64_t()
const noexcept(
false);
1242 inline operator int64_t()
const noexcept(
false);
1243 inline operator double()
const noexcept(
false);
1244 inline operator dom::array()
const noexcept(
false);
1245 inline operator dom::object()
const noexcept(
false);
1249 #endif // SIMDJSON_EXCEPTIONS
1263 #if SIMDJSON_EXCEPTIONS
1266 #endif // SIMDJSON_EXCEPTIONS
1283 #if SIMDJSON_EXCEPTIONS
1286 #endif // SIMDJSON_EXCEPTIONS
1291 #endif // SIMDJSON_DOCUMENT_H