simdjson  0.3.1
Ridiculously Fast JSON
document.h
1 #ifndef SIMDJSON_DOCUMENT_H
2 #define SIMDJSON_DOCUMENT_H
3 
4 #include <cstring>
5 #include <memory>
6 #include <string>
7 #include <limits>
8 #include <sstream>
9 #include "simdjson/common_defs.h"
10 #include "simdjson/simdjson.h"
11 #include "simdjson/padded_string.h"
12 
13 namespace simdjson::dom {
14 
15 class parser;
16 class element;
17 class array;
18 class object;
19 class key_value_pair;
20 class document;
21 class document_stream;
22 
24 static constexpr size_t DEFAULT_BATCH_SIZE = 1000000;
25 
26 } // namespace simdjson::dom
27 
28 namespace simdjson {
29 
30 template<> struct simdjson_result<dom::element>;
31 template<> struct simdjson_result<dom::array>;
32 template<> struct simdjson_result<dom::object>;
33 
34 template<typename T>
35 class minify;
36 
37 } // namespace simdjson
38 
39 namespace simdjson::internal {
40 
41 using namespace simdjson::dom;
42 
43 constexpr const uint64_t JSON_VALUE_MASK = 0x00FFFFFFFFFFFFFF;
44 
48 enum class tape_type {
49  ROOT = 'r',
50  START_ARRAY = '[',
51  START_OBJECT = '{',
52  END_ARRAY = ']',
53  END_OBJECT = '}',
54  STRING = '"',
55  INT64 = 'l',
56  UINT64 = 'u',
57  DOUBLE = 'd',
58  TRUE_VALUE = 't',
59  FALSE_VALUE = 'f',
60  NULL_VALUE = 'n'
61 };
62 
66 class tape_ref {
67 public:
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;
73  template<typename T>
74  really_inline T next_tape_value() const noexcept;
75  inline std::string_view get_string_view() const noexcept;
76 
78  const document *doc;
79 
81  size_t json_index;
82 };
83 
84 } // namespace simdjson::internal
85 
86 namespace simdjson::dom {
87 
92 enum class element_type {
93  ARRAY,
94  OBJECT,
95  INT64,
96  UINT64,
97  DOUBLE,
98  STRING,
99  BOOL,
100  NULL_VALUE
101 };
102 
106 class array : protected internal::tape_ref {
107 public:
109  really_inline array() noexcept;
110 
111  class iterator : protected internal::tape_ref {
112  public:
116  inline element operator*() const noexcept;
122  inline void operator++() noexcept;
128  inline bool operator!=(const iterator& other) const noexcept;
129  private:
130  really_inline iterator(const document *doc, size_t json_index) noexcept;
131  friend class array;
132  };
133 
139  inline iterator begin() const noexcept;
145  inline iterator end() const noexcept;
146 
161  inline simdjson_result<element> at(const std::string_view &json_pointer) const noexcept;
162 
169  inline simdjson_result<element> at(size_t index) const noexcept;
170 
171 private:
172  really_inline array(const document *doc, size_t json_index) noexcept;
173  friend class element;
174  friend struct simdjson_result<element>;
175  template<typename T>
176  friend class simdjson::minify;
177 };
178 
182 class object : protected internal::tape_ref {
183 public:
185  really_inline object() noexcept;
186 
187  class iterator : protected internal::tape_ref {
188  public:
192  inline const key_value_pair operator*() const 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;
216  inline element value() const noexcept;
217  private:
218  really_inline iterator(const document *doc, size_t json_index) noexcept;
219  friend class object;
220  };
221 
227  inline iterator begin() const noexcept;
233  inline iterator end() const noexcept;
234 
248  inline simdjson_result<element> operator[](const std::string_view &key) const noexcept;
249 
263  inline simdjson_result<element> operator[](const char *key) const noexcept;
264 
279  inline simdjson_result<element> at(const std::string_view &json_pointer) const noexcept;
280 
293  inline simdjson_result<element> at_key(const std::string_view &key) const noexcept;
294 
303  inline simdjson_result<element> at_key_case_insensitive(const std::string_view &key) const noexcept;
304 
305 private:
306  really_inline object(const document *doc, size_t json_index) noexcept;
307  friend class element;
308  friend struct simdjson_result<element>;
309  template<typename T>
310  friend class simdjson::minify;
311 };
312 
318 class document {
319 public:
325  document() noexcept = default;
326  ~document() noexcept = default;
327 
333  document(document &&other) noexcept = default;
335  document(const document &) = delete; // Disallow copying
341  document &operator=(document &&other) noexcept = default;
343  document &operator=(const document &) = delete; // Disallow copying
344 
348  element root() const noexcept;
349 
356  bool dump_raw_tape(std::ostream &os) const noexcept;
357 
359  std::unique_ptr<uint64_t[]> tape;
360 
365  std::unique_ptr<uint8_t[]> string_buf;
366 
367 private:
368  inline error_code allocate(size_t len) noexcept;
369  template<typename T>
370  friend class simdjson::minify;
371  friend class parser;
372 }; // class document
373 
380 class element : protected internal::tape_ref {
381 public:
383  really_inline element() noexcept;
384 
386  really_inline element_type type() const noexcept;
387 
389  really_inline bool is_null() const noexcept;
390 
400  template<typename T>
401  really_inline bool is() const noexcept;
402 
415  template<typename T>
416  really_inline simdjson_result<T> get() const noexcept;
417 
418 #if SIMDJSON_EXCEPTIONS
419 
425  inline operator bool() const noexcept(false);
426 
436  inline explicit operator const char*() const noexcept(false);
437 
447  inline operator std::string_view() const noexcept(false);
448 
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);
487 
494  inline dom::array::iterator begin() const noexcept(false);
495 
502  inline dom::array::iterator end() const noexcept(false);
503 #endif // SIMDJSON_EXCEPTIONS
504 
518  inline simdjson_result<element> operator[](const std::string_view &key) const noexcept;
519 
533  inline simdjson_result<element> operator[](const char *key) const noexcept;
534 
550  inline simdjson_result<element> at(const std::string_view &json_pointer) const noexcept;
551 
558  inline simdjson_result<element> at(size_t index) const noexcept;
559 
572  inline simdjson_result<element> at_key(const std::string_view &key) const noexcept;
573 
582  inline simdjson_result<element> at_key_case_insensitive(const std::string_view &key) const noexcept;
583 
585  inline bool dump_raw_tape(std::ostream &out) const noexcept;
586 
587 private:
588  really_inline element(const document *doc, size_t json_index) noexcept;
589  friend class document;
590  friend class object;
591  friend class array;
592  friend struct simdjson_result<element>;
593  template<typename T>
594  friend class simdjson::minify;
595 };
596 
601 public:
602  std::string_view key;
603  element value;
604 
605 private:
606  really_inline key_value_pair(const std::string_view &_key, element _value) noexcept;
607  friend class object;
608 };
609 
620 class parser {
621 public:
633  really_inline parser(size_t max_capacity = SIMDJSON_MAXSIZE_BYTES) noexcept;
634 
640  parser(parser &&other) = default;
641  parser(const parser &) = delete;
642 
647  parser &operator=(parser &&other) = default;
648  parser &operator=(const parser &) = delete;
649 
651  ~parser()=default;
652 
677  inline simdjson_result<element> load(const std::string &path) noexcept;
678 
714  inline simdjson_result<element> parse(const uint8_t *buf, size_t len, bool realloc_if_needed = true) noexcept;
716  really_inline simdjson_result<element> parse(const char *buf, size_t len, bool realloc_if_needed = true) noexcept;
718  really_inline simdjson_result<element> parse(const std::string &s) noexcept;
720  really_inline simdjson_result<element> parse(const padded_string &s) noexcept;
721 
723  really_inline simdjson_result<element> parse(const char *buf) noexcept = delete;
724 
780  inline document_stream load_many(const std::string &path, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
781 
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;
848  inline document_stream parse_many(const padded_string &s, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept;
849 
851  really_inline simdjson_result<element> parse_many(const char *buf, size_t batch_size = DEFAULT_BATCH_SIZE) noexcept = delete;
852 
861  WARN_UNUSED inline error_code allocate(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
862 
874  [[deprecated("Use allocate() instead.")]]
875  WARN_UNUSED inline bool allocate_capacity(size_t capacity, size_t max_depth = DEFAULT_MAX_DEPTH) noexcept;
876 
882  really_inline size_t capacity() const noexcept;
883 
891  really_inline size_t max_capacity() const noexcept;
892 
898  really_inline size_t max_depth() const noexcept;
899 
909  really_inline void set_max_capacity(size_t max_capacity) noexcept;
910 
912  class Iterator;
914  using InvalidJSON [[deprecated("Use simdjson_error instead")]] = simdjson_error;
915 
917  uint32_t current_loc{0};
918 
920  uint32_t n_structural_indexes{0};
922  std::unique_ptr<uint32_t[]> structural_indexes;
923 
925  std::unique_ptr<uint32_t[]> containing_scope_offset;
926 #ifdef SIMDJSON_USE_COMPUTED_GOTO
927 
928  std::unique_ptr<void*[]> ret_address;
929 #else
930 
931  std::unique_ptr<char[]> ret_address;
932 #endif
933 
935  uint8_t *current_string_buf_loc;
936 
938  bool valid{false};
940  error_code error{UNINITIALIZED};
941 
943  document doc;
944 
946  [[deprecated("Use the result of parser.parse() instead")]]
947  inline bool is_valid() const noexcept;
948 
953  [[deprecated("Use the result of parser.parse() instead")]]
954  inline int get_error_code() const noexcept;
955 
957  [[deprecated("Use error_message() on the result of parser.parse() instead, or cout << error")]]
958  inline std::string get_error_message() const noexcept;
959 
961  [[deprecated("Use cout << on the result of parser.parse() instead")]]
962  inline bool print_json(std::ostream &os) const noexcept;
963 
965  inline bool dump_raw_tape(std::ostream &os) const noexcept;
966 
967  //
968  // Parser callbacks: these are internal!
969  //
970 
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;
978  // TODO we're not checking this bool
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;
990 
991 private:
997  size_t _max_capacity;
998 
1004  size_t _capacity{0};
1005 
1011  size_t _max_depth{0};
1012 
1016  std::unique_ptr<char[], decltype(&aligned_free_char)> loaded_bytes;
1017 
1019  size_t _loaded_bytes_capacity{0};
1020 
1021  // all nodes are stored on the doc.tape using a 64-bit word.
1022  //
1023  // strings, double and ints are stored as
1024  // a 64-bit word with a pointer to the actual value
1025  //
1026  //
1027  //
1028  // for objects or arrays, store [ or { at the beginning and } and ] at the
1029  // end. For the openings ([ or {), we annotate them with a reference to the
1030  // location on the doc.tape of the end, and for then closings (} and ]), we
1031  // annotate them with a reference to the location of the opening
1032  //
1033  //
1034 
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;
1037 
1042  inline error_code ensure_capacity(size_t desired_capacity) noexcept;
1043 
1045  inline simdjson_result<size_t> read_file(const std::string &path) noexcept;
1046 
1047  friend class parser::Iterator;
1048  friend class document_stream;
1049 }; // class parser
1050 
1051 } // namespace simdjson::dom
1052 
1053 namespace simdjson {
1054 
1063 template<typename T>
1064 class minify {
1065 public:
1071  inline minify(const T &_value) noexcept : value{_value} {}
1072 
1076  inline operator std::string() const noexcept { std::stringstream s; s << *this; return s.str(); }
1077 
1081  inline std::ostream& print(std::ostream& out);
1082 private:
1083  const T &value;
1084 };
1085 
1093 template<typename T>
1094 inline std::ostream& operator<<(std::ostream& out, minify<T> formatter) { return formatter.print(out); }
1095 
1096 namespace dom {
1097 
1098 // << operators need to be in the same namespace as the class being output, so C++ can find them
1099 // automatically
1100 
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); }
1140 inline std::ostream& operator<<(std::ostream& out, const key_value_pair &value) { return out << minify(value); }
1141 
1149 inline std::ostream& operator<<(std::ostream& out, element_type type) {
1150  switch (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";
1167  default:
1168  abort();
1169  }
1170 }
1171 
1172 } // namespace dom
1173 
1174 #if SIMDJSON_EXCEPTIONS
1175 
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); }
1212 
1213 #endif
1214 
1216 template<>
1217 struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom::element> {
1218 public:
1219  really_inline simdjson_result() noexcept;
1220  really_inline simdjson_result(dom::element &&value) noexcept;
1221  really_inline simdjson_result(error_code error) noexcept;
1222 
1223  inline simdjson_result<dom::element_type> type() const noexcept;
1224  inline simdjson_result<bool> is_null() const noexcept;
1225  template<typename T>
1226  inline simdjson_result<bool> is() const noexcept;
1227  template<typename T>
1228  inline simdjson_result<T> get() const noexcept;
1229 
1230  inline simdjson_result<dom::element> operator[](const std::string_view &key) const noexcept;
1231  inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
1232  inline simdjson_result<dom::element> at(const std::string_view &json_pointer) const noexcept;
1233  inline simdjson_result<dom::element> at(size_t index) const noexcept;
1234  inline simdjson_result<dom::element> at_key(const std::string_view &key) const noexcept;
1235  inline simdjson_result<dom::element> at_key_case_insensitive(const std::string_view &key) const noexcept;
1236 
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);
1246 
1247  inline dom::array::iterator begin() const noexcept(false);
1248  inline dom::array::iterator end() const noexcept(false);
1249 #endif // SIMDJSON_EXCEPTIONS
1250 };
1251 
1253 template<>
1254 struct simdjson_result<dom::array> : public internal::simdjson_result_base<dom::array> {
1255 public:
1256  really_inline simdjson_result() noexcept;
1257  really_inline simdjson_result(dom::array value) noexcept;
1258  really_inline simdjson_result(error_code error) noexcept;
1259 
1260  inline simdjson_result<dom::element> at(const std::string_view &json_pointer) const noexcept;
1261  inline simdjson_result<dom::element> at(size_t index) const noexcept;
1262 
1263 #if SIMDJSON_EXCEPTIONS
1264  inline dom::array::iterator begin() const noexcept(false);
1265  inline dom::array::iterator end() const noexcept(false);
1266 #endif // SIMDJSON_EXCEPTIONS
1267 };
1268 
1270 template<>
1271 struct simdjson_result<dom::object> : public internal::simdjson_result_base<dom::object> {
1272 public:
1273  really_inline simdjson_result() noexcept;
1274  really_inline simdjson_result(dom::object value) noexcept;
1275  really_inline simdjson_result(error_code error) noexcept;
1276 
1277  inline simdjson_result<dom::element> operator[](const std::string_view &key) const noexcept;
1278  inline simdjson_result<dom::element> operator[](const char *key) const noexcept;
1279  inline simdjson_result<dom::element> at(const std::string_view &json_pointer) const noexcept;
1280  inline simdjson_result<dom::element> at_key(const std::string_view &key) const noexcept;
1281  inline simdjson_result<dom::element> at_key_case_insensitive(const std::string_view &key) const noexcept;
1282 
1283 #if SIMDJSON_EXCEPTIONS
1284  inline dom::object::iterator begin() const noexcept(false);
1285  inline dom::object::iterator end() const noexcept(false);
1286 #endif // SIMDJSON_EXCEPTIONS
1287 };
1288 
1289 } // namespace simdjson
1290 
1291 #endif // SIMDJSON_DOCUMENT_H
simdjson::dom::element
A JSON element.
Definition: document.h:380
simdjson::dom::array::iterator
Definition: document.h:111
simdjson::simdjson_result::value
really_inline T & value() noexcept(false)
Get the result value.
Definition: error.h:134
simdjson::minify::minify
minify(const T &_value) noexcept
Create a new minifier.
Definition: document.h:1071
simdjson::dom::object
JSON object.
Definition: document.h:182
simdjson::dom::document
A parsed JSON document.
Definition: document.h:318
simdjson::simdjson_result
The result of a simdjson operation that could fail.
Definition: error.h:166
simdjson::minify
Minifies a JSON element or document, printing the smallest possible valid JSON.
Definition: document.h:35
simdjson::dom::parser
A persistent document parser.
Definition: document.h:620
simdjson::dom::document_stream
A forward-only stream of documents.
Definition: document_stream.h:15
simdjson::simdjson_result< dom::element >
The result of a JSON navigation that may fail.
Definition: document.h:1217
simdjson::dom::array
JSON array.
Definition: document.h:106
simdjson::simdjson_error
Exception thrown when an exception-supporting simdjson method is called.
Definition: error.h:62
simdjson::dom::object::iterator
Definition: document.h:187
simdjson::padded_string
String with extra allocation for ease of use with parser::parse()
Definition: padded_string.h:18
simdjson::dom::key_value_pair
Key/value pair in an object.
Definition: document.h:600
simdjson::simdjson_result::error
really_inline error_code error() const noexcept
The error.
Definition: error.h:127
simdjson::dom::parser::Iterator
Definition: parsedjson_iterator.h:19
simdjson.h