1 #ifndef SIMDJSON_INLINE_DOCUMENT_H
2 #define SIMDJSON_INLINE_DOCUMENT_H
6 #include "simdjson/dom/document.h"
7 #include "simdjson/dom/element.h"
8 #include "simdjson/internal/tape_ref.h"
9 #include "simdjson/internal/jsonformatutils.h"
20 return element(internal::tape_ref(
this, 1));
23 inline size_t document::capacity() const noexcept {
24 return allocated_capacity;
28 inline error_code document::allocate(
size_t capacity) noexcept {
32 allocated_capacity = 0;
41 size_t tape_capacity = SIMDJSON_ROUNDUP_N(capacity + 3, 64);
44 size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * capacity / 3 +
SIMDJSON_PADDING, 64);
45 string_buf.reset(
new (std::nothrow) uint8_t[string_capacity]);
46 tape.reset(
new (std::nothrow) uint64_t[tape_capacity]);
47 if(!(string_buf && tape)) {
48 allocated_capacity = 0;
55 allocated_capacity = capacity;
59 inline bool document::dump_raw_tape(std::ostream &os)
const noexcept {
60 uint32_t string_length;
62 uint64_t tape_val = tape[tape_idx];
63 uint8_t type = uint8_t(tape_val >> 56);
64 os << tape_idx <<
" : " << type;
68 how_many = size_t(tape_val & internal::JSON_VALUE_MASK);
73 os <<
"\t// pointing to " << how_many <<
" (right after last node)\n";
75 for (; tape_idx < how_many; tape_idx++) {
76 os << tape_idx <<
" : ";
77 tape_val = tape[tape_idx];
78 payload = tape_val & internal::JSON_VALUE_MASK;
79 type = uint8_t(tape_val >> 56);
83 std::memcpy(&string_length, string_buf.get() + payload,
sizeof(uint32_t));
84 os << internal::escape_json_string(std::string_view(
85 reinterpret_cast<const char *
>(string_buf.get() + payload +
sizeof(uint32_t)),
92 if (tape_idx + 1 >= how_many) {
95 os <<
"integer " <<
static_cast<int64_t
>(tape[++tape_idx]) <<
"\n";
98 if (tape_idx + 1 >= how_many) {
101 os <<
"unsigned integer " << tape[++tape_idx] <<
"\n";
105 if (tape_idx + 1 >= how_many) {
109 std::memcpy(&answer, &tape[++tape_idx],
sizeof(answer));
110 os << answer <<
'\n';
122 os <<
"{\t// pointing to next tape location " << uint32_t(payload)
123 <<
" (first node after the scope), "
124 <<
" saturated count "
125 << ((payload >> 32) & internal::JSON_COUNT_MASK)<<
"\n";
127 os <<
"}\t// pointing to previous tape location " << uint32_t(payload)
128 <<
" (start of the scope)\n";
131 os <<
"[\t// pointing to next tape location " << uint32_t(payload)
132 <<
" (first node after the scope), "
133 <<
" saturated count "
134 << ((payload >> 32) & internal::JSON_COUNT_MASK)<<
"\n";
137 os <<
"]\t// pointing to previous tape location " << uint32_t(payload)
138 <<
" (start of the scope)\n";
147 tape_val = tape[tape_idx];
148 payload = tape_val & internal::JSON_VALUE_MASK;
149 type = uint8_t(tape_val >> 56);
150 os << tape_idx <<
" : " << type <<
"\t// pointing to " << payload
151 <<
" (start root)\n";
element root() const noexcept
Get the root element of this document as a JSON array.
We want to support argument-dependent lookup (ADL).
error_code
All possible errors returned by simdjson.
@ MEMALLOC
Error allocating memory, most likely out of memory.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.