1#ifndef SIMDJSON_DOCUMENT_INL_H
2#define SIMDJSON_DOCUMENT_INL_H
6#include "simdjson/dom/base.h"
7#include "simdjson/dom/document.h"
8#include "simdjson/dom/element-inl.h"
9#include "simdjson/internal/tape_ref-inl.h"
10#include "simdjson/internal/jsonformatutils.h"
21 return element(internal::tape_ref(
this, 1));
24inline size_t document::capacity() const noexcept {
25 return allocated_capacity;
29inline error_code document::allocate(
size_t capacity)
noexcept {
33 allocated_capacity = 0;
45 if(capacity + 3 < capacity) {
48 size_t tape_capacity = SIMDJSON_ROUNDUP_N(capacity + 3, 64);
54 size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (capacity / 3) +
SIMDJSON_PADDING, 64);
55 string_buf.reset(
new (std::nothrow) uint8_t[string_capacity]);
56 tape.reset(
new (std::nothrow) uint64_t[tape_capacity]);
57 if(!(string_buf && tape)) {
58 allocated_capacity = 0;
65 allocated_capacity = capacity;
69inline bool document::dump_raw_tape(std::ostream &os)
const noexcept {
70 uint32_t string_length;
72 uint64_t tape_val = tape[tape_idx];
73 uint8_t type = uint8_t(tape_val >> 56);
74 os << tape_idx <<
" : " << type;
78 how_many = size_t(tape_val & internal::JSON_VALUE_MASK);
83 os <<
"\t// pointing to " << how_many <<
" (right after last node)\n";
85 for (; tape_idx < how_many; tape_idx++) {
86 os << tape_idx <<
" : ";
87 tape_val = tape[tape_idx];
88 payload = tape_val & internal::JSON_VALUE_MASK;
89 type = uint8_t(tape_val >> 56);
93 std::memcpy(&string_length, string_buf.get() + payload,
sizeof(uint32_t));
94 os << internal::escape_json_string(std::string_view(
95 reinterpret_cast<const char *
>(string_buf.get() + payload +
sizeof(uint32_t)),
102 if (tape_idx + 1 >= how_many) {
105 os <<
"integer " <<
static_cast<int64_t
>(tape[++tape_idx]) <<
"\n";
108 if (tape_idx + 1 >= how_many) {
111 os <<
"unsigned integer " << tape[++tape_idx] <<
"\n";
115 if (tape_idx + 1 >= how_many) {
119 std::memcpy(&answer, &tape[++tape_idx],
sizeof(answer));
120 os << answer <<
'\n';
132 os <<
"{\t// pointing to next tape location " << uint32_t(payload)
133 <<
" (first node after the scope), "
134 <<
" saturated count "
135 << ((payload >> 32) & internal::JSON_COUNT_MASK)<<
"\n";
137 os <<
"}\t// pointing to previous tape location " << uint32_t(payload)
138 <<
" (start of the scope)\n";
141 os <<
"[\t// pointing to next tape location " << uint32_t(payload)
142 <<
" (first node after the scope), "
143 <<
" saturated count "
144 << ((payload >> 32) & internal::JSON_COUNT_MASK)<<
"\n";
147 os <<
"]\t// pointing to previous tape location " << uint32_t(payload)
148 <<
" (start of the scope)\n";
155 std::memcpy(&string_length, string_buf.get() + payload,
sizeof(uint32_t));
156 os << std::string_view(
157 reinterpret_cast<const char *
>(string_buf.get() + payload +
sizeof(uint32_t)),
166 tape_val = tape[tape_idx];
167 payload = tape_val & internal::JSON_VALUE_MASK;
168 type = uint8_t(tape_val >> 56);
169 os << tape_idx <<
" : " << type <<
"\t// pointing to " << payload
170 <<
" (start root)\n";
element root() const noexcept
Get the root element of this document as a JSON array.
The top level simdjson namespace, containing everything the library provides.
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.
SIMDJSON_PUSH_DISABLE_UNUSED_WARNINGS constexpr size_t SIMDJSON_MAXSIZE_BYTES
The maximum document size supported by simdjson.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.