1 #ifndef SIMDJSON_INLINE_DOCUMENT_H
2 #define SIMDJSON_INLINE_DOCUMENT_H
6 #include "simdjson/document.h"
7 #include "simdjson/document_stream.h"
8 #include "simdjson/implementation.h"
9 #include "simdjson/padded_string.h"
10 #include "simdjson/internal/jsonformatutils.h"
20 really_inline simdjson_result<dom::element>::simdjson_result() noexcept
21 : internal::simdjson_result_base<dom::element>() {}
22 really_inline simdjson_result<dom::element>::simdjson_result(dom::element &&value) noexcept
23 : internal::simdjson_result_base<dom::element>(std::forward<dom::element>(value)) {}
24 really_inline simdjson_result<dom::element>::simdjson_result(error_code error) noexcept
25 : internal::simdjson_result_base<dom::element>(error) {}
26 inline simdjson_result<dom::element_type> simdjson_result<dom::element>::type() const noexcept {
30 inline simdjson_result<bool> simdjson_result<dom::element>::is_null() const noexcept {
32 return first.is_null();
35 inline simdjson_result<bool> simdjson_result<dom::element>::is() const noexcept {
40 inline simdjson_result<T> simdjson_result<dom::element>::get() const noexcept {
42 return first.get<T>();
45 inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](
const std::string_view &key)
const noexcept {
46 if (error()) {
return error(); }
49 inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](
const char *key)
const noexcept {
50 if (error()) {
return error(); }
53 inline simdjson_result<dom::element> simdjson_result<dom::element>::at(
const std::string_view &json_pointer)
const noexcept {
54 if (error()) {
return error(); }
55 return first.at(json_pointer);
57 inline simdjson_result<dom::element> simdjson_result<dom::element>::at(
size_t index)
const noexcept {
58 if (error()) {
return error(); }
59 return first.at(index);
61 inline simdjson_result<dom::element> simdjson_result<dom::element>::at_key(
const std::string_view &key)
const noexcept {
62 if (error()) {
return error(); }
63 return first.at_key(key);
65 inline simdjson_result<dom::element> simdjson_result<dom::element>::at_key_case_insensitive(
const std::string_view &key)
const noexcept {
66 if (error()) {
return error(); }
67 return first.at_key_case_insensitive(key);
70 #if SIMDJSON_EXCEPTIONS
72 inline simdjson_result<dom::element>::operator bool() const noexcept(false) {
75 inline simdjson_result<dom::element>::operator
const char *()
const noexcept(
false) {
76 return get<const char *>();
78 inline simdjson_result<dom::element>::operator std::string_view() const noexcept(false) {
79 return get<std::string_view>();
81 inline simdjson_result<dom::element>::operator uint64_t() const noexcept(false) {
82 return get<uint64_t>();
84 inline simdjson_result<dom::element>::operator int64_t() const noexcept(false) {
85 return get<int64_t>();
87 inline simdjson_result<dom::element>::operator double() const noexcept(false) {
90 inline simdjson_result<dom::element>::operator dom::array() const noexcept(false) {
91 return get<dom::array>();
93 inline simdjson_result<dom::element>::operator dom::object() const noexcept(false) {
94 return get<dom::object>();
97 inline dom::array::iterator simdjson_result<dom::element>::begin() const noexcept(false) {
98 if (
error()) {
throw simdjson_error(
error()); }
101 inline dom::array::iterator simdjson_result<dom::element>::end() const noexcept(false) {
102 if (
error()) {
throw simdjson_error(
error()); }
111 really_inline simdjson_result<dom::array>::simdjson_result() noexcept
112 : internal::simdjson_result_base<dom::array>() {}
113 really_inline simdjson_result<dom::array>::simdjson_result(dom::array value) noexcept
114 : internal::simdjson_result_base<dom::array>(std::forward<dom::array>(value)) {}
115 really_inline simdjson_result<dom::array>::simdjson_result(error_code error) noexcept
116 : internal::simdjson_result_base<dom::array>(error) {}
118 #if SIMDJSON_EXCEPTIONS
120 inline dom::array::iterator simdjson_result<dom::array>::begin() const noexcept(false) {
121 if (
error()) {
throw simdjson_error(
error()); }
122 return first.begin();
124 inline dom::array::iterator simdjson_result<dom::array>::end() const noexcept(false) {
125 if (
error()) {
throw simdjson_error(
error()); }
129 #endif // SIMDJSON_EXCEPTIONS
131 inline simdjson_result<dom::element> simdjson_result<dom::array>::at(
const std::string_view &json_pointer)
const noexcept {
132 if (error()) {
return error(); }
133 return first.at(json_pointer);
135 inline simdjson_result<dom::element> simdjson_result<dom::array>::at(
size_t index)
const noexcept {
136 if (error()) {
return error(); }
137 return first.at(index);
143 really_inline simdjson_result<dom::object>::simdjson_result() noexcept
144 : internal::simdjson_result_base<dom::
object>() {}
145 really_inline simdjson_result<dom::object>::simdjson_result(dom::object value) noexcept
146 : internal::simdjson_result_base<dom::object>(std::forward<dom::object>(value)) {}
147 really_inline simdjson_result<dom::object>::simdjson_result(error_code error) noexcept
148 : internal::simdjson_result_base<dom::object>(error) {}
150 inline simdjson_result<dom::element> simdjson_result<dom::object>::operator[](
const std::string_view &key)
const noexcept {
151 if (error()) {
return error(); }
154 inline simdjson_result<dom::element> simdjson_result<dom::object>::operator[](
const char *key)
const noexcept {
155 if (error()) {
return error(); }
158 inline simdjson_result<dom::element> simdjson_result<dom::object>::at(
const std::string_view &json_pointer)
const noexcept {
159 if (error()) {
return error(); }
160 return first.at(json_pointer);
162 inline simdjson_result<dom::element> simdjson_result<dom::object>::at_key(
const std::string_view &key)
const noexcept {
163 if (error()) {
return error(); }
164 return first.at_key(key);
166 inline simdjson_result<dom::element> simdjson_result<dom::object>::at_key_case_insensitive(
const std::string_view &key)
const noexcept {
167 if (error()) {
return error(); }
168 return first.at_key_case_insensitive(key);
171 #if SIMDJSON_EXCEPTIONS
173 inline dom::object::iterator simdjson_result<dom::object>::begin() const noexcept(false) {
174 if (
error()) {
throw simdjson_error(
error()); }
175 return first.begin();
177 inline dom::object::iterator simdjson_result<dom::object>::end() const noexcept(false) {
178 if (
error()) {
throw simdjson_error(
error()); }
182 #endif // SIMDJSON_EXCEPTIONS
186 namespace simdjson::dom {
196 #define REPORT_ERROR(CODE, MESSAGE) (CODE)
197 #define RETURN_ERROR(CODE, MESSAGE) return REPORT_ERROR((CODE), (MESSAGE));
200 inline error_code document::allocate(
size_t capacity) noexcept {
212 size_t tape_capacity = ROUNDUP_N(capacity + 2, 64);
215 size_t string_capacity = ROUNDUP_N(5 * capacity / 3 + 32, 64);
216 string_buf.reset(
new (std::nothrow) uint8_t[string_capacity]);
217 tape.reset(
new (std::nothrow) uint64_t[tape_capacity]);
218 return string_buf && tape ? SUCCESS : MEMALLOC;
221 inline bool document::dump_raw_tape(std::ostream &os)
const noexcept {
222 uint32_t string_length;
224 uint64_t tape_val = tape[tape_idx];
225 uint8_t type = (tape_val >> 56);
226 os << tape_idx <<
" : " << type;
230 how_many = tape_val & internal::JSON_VALUE_MASK;
235 os <<
"\t// pointing to " << how_many <<
" (right after last node)\n";
237 for (; tape_idx < how_many; tape_idx++) {
238 os << tape_idx <<
" : ";
239 tape_val = tape[tape_idx];
240 payload = tape_val & internal::JSON_VALUE_MASK;
241 type = (tape_val >> 56);
245 memcpy(&string_length, string_buf.get() + payload,
sizeof(uint32_t));
246 os << internal::escape_json_string(std::string_view(
247 (
const char *)(string_buf.get() + payload +
sizeof(uint32_t)),
254 if (tape_idx + 1 >= how_many) {
257 os <<
"integer " <<
static_cast<int64_t
>(tape[++tape_idx]) <<
"\n";
260 if (tape_idx + 1 >= how_many) {
263 os <<
"unsigned integer " << tape[++tape_idx] <<
"\n";
267 if (tape_idx + 1 >= how_many) {
271 memcpy(&answer, &tape[++tape_idx],
sizeof(answer));
272 os << answer <<
'\n';
284 os <<
"{\t// pointing to next tape location " << payload
285 <<
" (first node after the scope) \n";
288 os <<
"}\t// pointing to previous tape location " << payload
289 <<
" (start of the scope) \n";
292 os <<
"[\t// pointing to next tape location " << payload
293 <<
" (first node after the scope) \n";
296 os <<
"]\t// pointing to previous tape location " << payload
297 <<
" (start of the scope) \n";
306 tape_val = tape[tape_idx];
307 payload = tape_val & internal::JSON_VALUE_MASK;
308 type = (tape_val >> 56);
309 os << tape_idx <<
" : " << type <<
"\t// pointing to " << payload
310 <<
" (start root)\n";
318 : _max_capacity{max_capacity}, loaded_bytes(
nullptr, &aligned_free_char) {}
319 inline bool parser::is_valid() const noexcept {
return valid; }
320 inline int parser::get_error_code() const noexcept {
return error; }
321 inline std::string parser::get_error_message() const noexcept {
return error_message(error); }
322 inline bool parser::print_json(std::ostream &os)
const noexcept {
323 if (!valid) {
return false; }
327 inline bool parser::dump_raw_tape(std::ostream &os)
const noexcept {
328 return valid ? doc.dump_raw_tape(os) :
false;
331 inline simdjson_result<size_t> parser::read_file(
const std::string &path) noexcept {
333 std::FILE *fp = std::fopen(path.c_str(),
"rb");
339 if(std::fseek(fp, 0, SEEK_END) < 0) {
343 long len = std::ftell(fp);
344 if((len < 0) || (len == LONG_MAX)) {
350 if (_loaded_bytes_capacity <
size_t(len)) {
351 loaded_bytes.reset( internal::allocate_padded_buffer(len) );
356 _loaded_bytes_capacity = len;
361 size_t bytes_read = std::fread(loaded_bytes.get(), 1, len, fp);
362 if (std::fclose(fp) != 0 || bytes_read !=
size_t(len)) {
370 auto [len, code] = read_file(path);
371 if (code) {
return code; }
373 return parse(loaded_bytes.get(), len,
false);
377 auto [len, code] = read_file(path);
378 return document_stream(*
this, (
const uint8_t*)loaded_bytes.get(), len, batch_size, code);
382 error_code code = ensure_capacity(len);
383 if (code) {
return code; }
385 if (realloc_if_needed) {
386 const uint8_t *tmp_buf = buf;
387 buf = (uint8_t *)internal::allocate_padded_buffer(len);
390 memcpy((
void *)buf, tmp_buf, len);
393 code = simdjson::active_implementation->parse(buf, len, *
this);
394 if (realloc_if_needed) {
395 aligned_free((
void *)buf);
397 if (code) {
return code; }
401 error = UNINITIALIZED;
405 return parse((
const uint8_t *)buf, len, realloc_if_needed);
408 return parse(s.data(), s.length(), s.capacity() - s.length() < SIMDJSON_PADDING);
410 really_inline simdjson_result<element>
parser::parse(
const padded_string &s) noexcept {
411 return parse(s.data(), s.length(),
false);
418 return parse_many((
const uint8_t *)buf, len, batch_size);
420 inline document_stream
parser::parse_many(
const std::string &s,
size_t batch_size) noexcept {
421 return parse_many(s.data(), s.length(), batch_size);
423 inline document_stream
parser::parse_many(
const padded_string &s,
size_t batch_size) noexcept {
424 return parse_many(s.data(), s.length(), batch_size);
431 return _max_capacity;
442 if (_capacity != capacity) {
449 error_code err = doc.allocate(capacity);
450 if (err) {
return err; }
456 structural_indexes.reset();
463 uint32_t max_structures = ROUNDUP_N(capacity, 64) + 2 + 7;
464 structural_indexes.reset(
new (std::nothrow) uint32_t[max_structures] );
465 if (!structural_indexes) {
469 _capacity = capacity;
474 }
else if (!doc.tape) {
475 error_code err = doc.allocate(capacity);
476 if (err) {
return err; }
482 if (max_depth != _max_depth) {
485 if (max_depth == 0) {
487 containing_scope_offset.reset();
494 containing_scope_offset.reset(
new (std::nothrow) uint32_t[max_depth]);
495 #ifdef SIMDJSON_USE_COMPUTED_GOTO
496 ret_address.reset(
new (std::nothrow)
void *[max_depth]);
498 ret_address.reset(
new (std::nothrow)
char[max_depth]);
501 if (!ret_address || !containing_scope_offset) {
506 _max_depth = max_depth;
512 inline bool parser::allocate_capacity(
size_t capacity,
size_t max_depth) noexcept {
513 return !allocate(capacity, max_depth);
517 _max_capacity = max_capacity;
520 inline error_code parser::ensure_capacity(
size_t desired_capacity) noexcept {
524 if (unlikely(desired_capacity > capacity() || !doc.tape)) {
525 if (desired_capacity > max_capacity()) {
526 return error = CAPACITY;
528 return allocate(desired_capacity, _max_depth > 0 ? _max_depth : DEFAULT_MAX_DEPTH);
538 really_inline
array::array(
const document *_doc,
size_t _json_index) noexcept : internal::tape_ref(_doc, _json_index) {}
540 return iterator(doc, json_index + 1);
543 return iterator(doc, after_element() - 1);
549 if (json_pointer ==
"-") {
return INDEX_OUT_OF_BOUNDS; }
552 size_t array_index = 0;
554 for (i = 0; i < json_pointer.length() && json_pointer[i] !=
'/'; i++) {
555 uint8_t digit = uint8_t(json_pointer[i]) -
'0';
557 if (digit > 9) {
return INCORRECT_TYPE; }
558 array_index = array_index*10 + digit;
562 if (i > 1 && json_pointer[0] ==
'0') { RETURN_ERROR(INVALID_JSON_POINTER,
"JSON pointer array index has other characters after 0"); }
565 if (i == 0) { RETURN_ERROR(INVALID_JSON_POINTER,
"Empty string in JSON pointer array index"); }
568 auto child =
array(doc, json_index).
at(array_index);
570 if (i < json_pointer.length()) {
571 child = child.at(json_pointer.substr(i+1));
578 if (i == index) {
return element; }
581 return INDEX_OUT_OF_BOUNDS;
587 really_inline array::iterator::iterator(
const document *_doc,
size_t _json_index) noexcept : internal::tape_ref(_doc, _json_index) { }
589 return element(doc, json_index);
592 return json_index != other.json_index;
595 json_index = after_element();
602 really_inline
object::object(
const document *_doc,
size_t _json_index) noexcept : internal::tape_ref(_doc, _json_index) { }
604 return iterator(doc, json_index + 1);
607 return iterator(doc, after_element() - 1);
617 size_t slash = json_pointer.find(
'/');
618 std::string_view key = json_pointer.substr(0, slash);
624 size_t escape = key.find(
'~');
625 if (escape != std::string_view::npos) {
627 std::string unescaped(key);
629 switch (unescaped[escape+1]) {
631 unescaped.replace(escape, 2,
"~");
634 unescaped.replace(escape, 2,
"/");
637 RETURN_ERROR(INVALID_JSON_POINTER,
"Unexpected ~ escape character in JSON pointer");
639 escape = unescaped.find(
'~', escape+1);
640 }
while (escape != std::string::npos);
641 child = at_key(unescaped);
647 if (slash != std::string_view::npos) {
648 child = child.at(json_pointer.substr(slash+1));
655 for (
iterator field =
begin(); field != end_field; ++field) {
656 if (key == field.key()) {
657 return field.value();
660 return NO_SUCH_FIELD;
667 for (
iterator field =
begin(); field != end_field; ++field) {
668 auto field_key = field.key();
669 if (key.length() == field_key.length()) {
671 for (
size_t i=0; i<field_key.length(); i++) {
672 equal = equal && std::tolower(key[i]) != std::tolower(field_key[i]);
674 if (equal) {
return field.value(); }
677 return NO_SUCH_FIELD;
683 really_inline object::iterator::iterator(
const document *_doc,
size_t _json_index) noexcept : internal::tape_ref(_doc, _json_index) { }
688 return json_index != other.json_index;
692 json_index = after_element();
695 size_t string_buf_index = tape_value();
697 memcpy(&len, &doc->string_buf[string_buf_index],
sizeof(len));
698 return std::string_view(
699 reinterpret_cast<const char *
>(&doc->string_buf[string_buf_index +
sizeof(uint32_t)]),
704 return reinterpret_cast<const char *
>(&doc->string_buf[tape_value() +
sizeof(uint32_t)]);
707 return element(doc, json_index + 1);
713 inline key_value_pair::key_value_pair(
const std::string_view &_key,
element _value) noexcept :
714 key(_key), value(_value) {}
720 really_inline
element::element(
const document *_doc,
size_t _json_index) noexcept : internal::tape_ref(_doc, _json_index) { }
723 switch (tape_ref_type()) {
724 case internal::tape_type::START_ARRAY:
725 return element_type::ARRAY;
726 case internal::tape_type::START_OBJECT:
727 return element_type::OBJECT;
728 case internal::tape_type::INT64:
729 return element_type::INT64;
730 case internal::tape_type::UINT64:
731 return element_type::UINT64;
732 case internal::tape_type::DOUBLE:
733 return element_type::DOUBLE;
734 case internal::tape_type::STRING:
735 return element_type::STRING;
736 case internal::tape_type::TRUE_VALUE:
737 case internal::tape_type::FALSE_VALUE:
738 return element_type::BOOL;
739 case internal::tape_type::NULL_VALUE:
740 return element_type::NULL_VALUE;
741 case internal::tape_type::ROOT:
742 case internal::tape_type::END_ARRAY:
743 case internal::tape_type::END_OBJECT:
749 return tape_ref_type() == internal::tape_type::NULL_VALUE;
754 switch (tape_ref_type()) {
755 case internal::tape_type::TRUE_VALUE:
757 case internal::tape_type::FALSE_VALUE:
760 return INCORRECT_TYPE;
764 inline simdjson_result<const char *> element::get<const char *>() const noexcept {
765 switch (tape_ref_type()) {
766 case internal::tape_type::STRING: {
767 size_t string_buf_index = tape_value();
768 return reinterpret_cast<const char *
>(&doc->string_buf[string_buf_index +
sizeof(uint32_t)]);
771 return INCORRECT_TYPE;
775 inline simdjson_result<std::string_view> element::get<std::string_view>() const noexcept {
776 switch (tape_ref_type()) {
777 case internal::tape_type::STRING:
778 return get_string_view();
780 return INCORRECT_TYPE;
784 inline simdjson_result<uint64_t> element::get<uint64_t>() const noexcept {
785 switch (tape_ref_type()) {
786 case internal::tape_type::UINT64:
787 return next_tape_value<uint64_t>();
788 case internal::tape_type::INT64: {
789 int64_t result = next_tape_value<int64_t>();
791 return NUMBER_OUT_OF_RANGE;
793 return static_cast<uint64_t
>(result);
796 return INCORRECT_TYPE;
800 inline simdjson_result<int64_t> element::get<int64_t>() const noexcept {
801 switch (tape_ref_type()) {
802 case internal::tape_type::UINT64: {
803 uint64_t result = next_tape_value<uint64_t>();
805 if (result > (std::numeric_limits<int64_t>::max)()) {
806 return NUMBER_OUT_OF_RANGE;
808 return static_cast<int64_t
>(result);
810 case internal::tape_type::INT64:
811 return next_tape_value<int64_t>();
813 return INCORRECT_TYPE;
817 inline simdjson_result<double> element::get<double>() const noexcept {
818 switch (tape_ref_type()) {
819 case internal::tape_type::UINT64:
820 return next_tape_value<uint64_t>();
821 case internal::tape_type::INT64: {
822 return next_tape_value<int64_t>();
823 int64_t result = tape_value();
825 return NUMBER_OUT_OF_RANGE;
827 return double(result);
829 case internal::tape_type::DOUBLE:
830 return next_tape_value<double>();
832 return INCORRECT_TYPE;
836 inline simdjson_result<array> element::get<array>() const noexcept {
837 switch (tape_ref_type()) {
838 case internal::tape_type::START_ARRAY:
839 return array(doc, json_index);
841 return INCORRECT_TYPE;
845 inline simdjson_result<object> element::get<object>() const noexcept {
846 switch (tape_ref_type()) {
847 case internal::tape_type::START_OBJECT:
848 return object(doc, json_index);
850 return INCORRECT_TYPE;
856 auto result = get<T>();
857 return !result.error();
860 #if SIMDJSON_EXCEPTIONS
862 inline element::operator bool() const noexcept(false) {
return get<bool>(); }
863 inline element::operator
const char*()
const noexcept(
false) {
return get<const char *>(); }
864 inline element::operator std::string_view() const noexcept(false) {
return get<std::string_view>(); }
865 inline element::operator uint64_t() const noexcept(false) {
return get<uint64_t>(); }
866 inline element::operator int64_t() const noexcept(false) {
return get<int64_t>(); }
867 inline element::operator double() const noexcept(false) {
return get<double>(); }
868 inline element::operator
array() const noexcept(false) {
return get<array>(); }
869 inline element::operator
object() const noexcept(false) {
return get<object>(); }
872 return get<array>().begin();
875 return get<array>().end();
887 switch (tape_ref_type()) {
888 case internal::tape_type::START_OBJECT:
889 return object(doc, json_index).
at(json_pointer);
890 case internal::tape_type::START_ARRAY:
891 return array(doc, json_index).at(json_pointer);
893 return INCORRECT_TYPE;
897 return get<array>().at(index);
900 return get<object>().at_key(key);
903 return get<object>().at_key_case_insensitive(key);
906 inline bool element::dump_raw_tape(std::ostream &out)
const noexcept {
907 return doc->dump_raw_tape(out);
920 using tape_type=internal::tape_type;
922 constexpr
size_t MAX_DEPTH = 16;
923 bool is_object[MAX_DEPTH];
924 is_object[0] =
false;
925 bool after_value =
false;
927 internal::tape_ref iter(value);
934 if (is_object[depth]) {
935 out <<
'"' << internal::escape_json_string(iter.get_string_view()) <<
"\":";
938 switch (iter.tape_ref_type()) {
941 case tape_type::START_ARRAY: {
944 if (unlikely(depth >= MAX_DEPTH)) {
945 out << minify<dom::array>(dom::array(iter.doc, iter.json_index));
946 iter.json_index = iter.tape_value() - 1;
956 if (iter.tape_ref_type() == tape_type::END_ARRAY) {
962 is_object[depth] =
false;
968 case tape_type::START_OBJECT: {
971 if (unlikely(depth >= MAX_DEPTH)) {
972 out << minify<dom::object>(dom::object(iter.doc, iter.json_index));
973 iter.json_index = iter.tape_value() - 1;
983 if (iter.tape_ref_type() == tape_type::END_OBJECT) {
989 is_object[depth] =
true;
995 case tape_type::STRING:
996 out <<
'"' << internal::escape_json_string(iter.get_string_view()) <<
'"';
998 case tape_type::INT64:
999 out << iter.next_tape_value<int64_t>();
1002 case tape_type::UINT64:
1003 out << iter.next_tape_value<uint64_t>();
1006 case tape_type::DOUBLE:
1007 out << iter.next_tape_value<
double>();
1010 case tape_type::TRUE_VALUE:
1013 case tape_type::FALSE_VALUE:
1016 case tape_type::NULL_VALUE:
1021 case tape_type::END_ARRAY:
1022 case tape_type::END_OBJECT:
1023 case tape_type::ROOT:
1030 while (depth != 0 && (iter.tape_ref_type() == tape_type::END_ARRAY || iter.tape_ref_type() == tape_type::END_OBJECT)) {
1031 out << char(iter.tape_ref_type());
1037 }
while (depth != 0);
1044 auto pair = value.begin();
1045 auto end = value.end();
1047 out << minify<dom::key_value_pair>(*pair);
1048 for (++pair; pair != end; ++pair) {
1049 out <<
"," << minify<dom::key_value_pair>(*pair);
1057 auto iter = value.begin();
1058 auto end = value.end();
1060 out << minify<dom::element>(*iter);
1061 for (++iter; iter != end; ++iter) {
1062 out <<
"," << minify<dom::element>(*iter);
1069 return out <<
'"' << internal::escape_json_string(value.key) <<
"\":" << value.value;
1072 #if SIMDJSON_EXCEPTIONS
1075 inline std::ostream& minify<simdjson_result<dom::element>>::print(std::ostream& out) {
1076 if (value.error()) {
throw simdjson_error(value.error()); }
1077 return out << minify<dom::element>(value.first);
1080 inline std::ostream& minify<simdjson_result<dom::array>>::print(std::ostream& out) {
1081 if (value.error()) {
throw simdjson_error(value.error()); }
1082 return out << minify<dom::array>(value.first);
1085 inline std::ostream& minify<simdjson_result<dom::object>>::print(std::ostream& out) {
1086 if (value.error()) {
throw simdjson_error(value.error()); }
1087 return out << minify<dom::object>(value.first);
1094 namespace simdjson::internal {
1099 really_inline tape_ref::tape_ref() noexcept : doc{
nullptr}, json_index{0} {}
1100 really_inline tape_ref::tape_ref(
const document *_doc,
size_t _json_index) noexcept : doc{_doc}, json_index{_json_index} {}
1102 inline size_t tape_ref::after_element() const noexcept {
1103 switch (tape_ref_type()) {
1104 case tape_type::START_ARRAY:
1105 case tape_type::START_OBJECT:
1106 return tape_value();
1107 case tape_type::UINT64:
1108 case tape_type::INT64:
1109 case tape_type::DOUBLE:
1110 return json_index + 2;
1112 return json_index + 1;
1115 really_inline tape_type tape_ref::tape_ref_type() const noexcept {
1116 return static_cast<tape_type
>(doc->tape[json_index] >> 56);
1118 really_inline uint64_t internal::tape_ref::tape_value() const noexcept {
1119 return doc->tape[json_index] & internal::JSON_VALUE_MASK;
1121 template<
typename T>
1122 really_inline T tape_ref::next_tape_value() const noexcept {
1123 static_assert(
sizeof(T) ==
sizeof(uint64_t));
1124 return *
reinterpret_cast<const T*
>(&doc->tape[json_index + 1]);
1126 inline std::string_view internal::tape_ref::get_string_view() const noexcept {
1127 size_t string_buf_index = tape_value();
1129 memcpy(&len, &doc->string_buf[string_buf_index],
sizeof(len));
1130 return std::string_view(
1131 reinterpret_cast<const char *
>(&doc->string_buf[string_buf_index +
sizeof(uint32_t)]),
1139 #endif // SIMDJSON_INLINE_DOCUMENT_H