Engine → Core → document.h
json::document
| Type | Class |
| Namespace | conjure::core::json |
| Include File Path | engine/include/conjure/core/json/document.h |
| Source File Path | engine/src/core/json/document.cpp |
| API ↗ | JSON |
Syntax
Constructors
| Access | Signature | Description |
|---|---|---|
| public | document(); | A document whose root is JSON null. The first set() turns the root into an object, the first push_back() into an array. |
| public | document(
const document &other); | |
| public | document(
document &&other) noexcept; | Moving transfers the tree without copying. The moved-from document is empty: its root() is invalid, it serialises to "", every mutation is rejected (asserts or returns false) and it equals nothing; only assignment and destruction are meant for it. Copying it yields another empty document. |
| private | explicit document(
void *handle) noexcept; |
Destructors
| Access | Signature | Description |
|---|---|---|
| public | ~document(); |
Variables
| Access | Name | Type | Default | Description |
|---|---|---|---|---|
| private | mHandle | void * | nullptr |
Functions
Public
at_pointer
at_pointer[[nodiscard]] value at_pointer(
std::string_view pointer) const noexcept;The value at a JSON Pointer such as "/a/b/0" ("" is the root), or an invalid value when the pointer is malformed or does not resolve.
clear
clearvoid clear();Removes every member or element of a container root; any other root becomes null.
erase
erasebool erase(
std::string_view key);Removes every member named key. Returns false when there was none.
bool erase(
usize index);Removes the element at index. Returns false when out of range.
from_value
from_valueA document whose root is a deep copy of source (any node of any document). An invalid source yields a null root.
merge_patch
merge_patchbool merge_patch(
const value &patch);Applies an RFC 7386 merge patch: object members are merged recursively, a null member removes the target member, anything else replaces the target. Returns false only when memory runs out; the document is then unchanged.
bool merge_patch(
const document &patch);parse
parseParses strict RFC 8259 JSON (no comments, trailing commas, inf/nan or BOM). Fails with PARSE_FAILURE; the message carries the 1-based line and column, or names the limit when containers nest deeper than MAX_NESTING_DEPTH.
parse_file
parse_fileReads a file and parses it. Fails with IO_FAILURE when the path is not a readable regular file and PARSE_FAILURE when its content is not JSON. A leading UTF-8 byte order mark is skipped (as file_system::read_text_file does); parse() itself rejects one.
push_back
push_backvoid push_back(
bool item);Appends a scalar element. Non-negative integers are stored unsigned, negative ones signed; non-finite reals assert and store null; a null C string stores null.
void push_back(
i32 item);void push_back(
u32 item);void push_back(
i64 item);void push_back(
u64 item);void push_back(
f32 item);void push_back(
f64 item);void push_back(
std::string_view item);void push_back(
const char *item);void push_back(
std::nullptr_t);template <detail::other_integer T> void push_back(
T item);void push_back(
const document &item);Appends a deep copy of another document's tree or of a value from any document.
void push_back(
const value &item);root
root[[nodiscard]] value root() const noexcept;A view of the root value. Valid for every document except a moved-from one.
set
setvoid set(
std::string_view key,
bool item);Sets member key to a scalar. Non-negative integers are stored unsigned, negative ones signed; non-finite reals assert and store null; a null C string stores null.
void set(
std::string_view key,
i32 item);void set(
std::string_view key,
u32 item);void set(
std::string_view key,
i64 item);void set(
std::string_view key,
u64 item);void set(
std::string_view key,
f32 item);void set(
std::string_view key,
f64 item);void set(
std::string_view key,
std::string_view item);void set(
std::string_view key,
const char *item);void set(
std::string_view key,
std::nullptr_t);template <detail::other_integer T> void set(
std::string_view key,
T item);void set(
std::string_view key,
const document &item);Deep-copies another document's tree under key.
void set(
std::string_view key,
const value &item);Deep-copies a value (from this or any other document) under key.
set_null
set_nullvoid set_null(
std::string_view key);Sets key to JSON null (the member stays; use erase() to remove it).
set_pointer
set_pointerbool set_pointer(
std::string_view pointer,
bool item);Sets the value at a JSON Pointer, creating missing intermediate objects. In an array the last token replaces an existing index, while "-" or an index equal to the current size appends (RFC 6902 "add" semantics). Returns false for a malformed pointer or when an existing parent is a scalar or an array index is past the end.
bool set_pointer(
std::string_view pointer,
i32 item);bool set_pointer(
std::string_view pointer,
u32 item);bool set_pointer(
std::string_view pointer,
i64 item);bool set_pointer(
std::string_view pointer,
u64 item);bool set_pointer(
std::string_view pointer,
f32 item);bool set_pointer(
std::string_view pointer,
f64 item);bool set_pointer(
std::string_view pointer,
std::string_view item);bool set_pointer(
std::string_view pointer,
const char *item);bool set_pointer(
std::string_view pointer,
std::nullptr_t);bool set_pointer(
std::string_view pointer,
const document &item);bool set_pointer(
std::string_view pointer,
const value &item);template <detail::other_integer T> bool set_pointer(
std::string_view pointer,
T item);to_canonical_string
to_canonical_string[[nodiscard]] std::string to_canonical_string() const;Canonical form for hashing and diffing: members sorted by key (byte order), minified, shortest round-trip reals, integers as plain digits. Identical on every platform for the same content, and identical to what writer produces when the same data is written with keys in sorted order. Returns an empty string (and logs a warning) only when a string holds invalid UTF-8.
to_string
to_string[[nodiscard]] std::string to_string(
bool pretty = false) const;Serialises in member order: minified, or pretty with 4-space indentation. Returns an empty string (and logs a warning) only when a string holds invalid UTF-8.
write_file
write_file[[nodiscard]] result<void> write_file(
const std::filesystem::path &path,
bool pretty = false) const;Writes to_string(pretty) to the file at path, replacing it in one step through a temporary sibling that is flushed to the storage device first (core::vfs::write_native_file_atomic), so after a failure, a crash or a power cut the path holds the old contents or the new ones, never a part. The cost is one device flush per call. Missing parent directories are created; a relative path is resolved against the working directory. Replacing is not writing through: what it does to the file's permissions, links and attributes is listed at write_native_file_atomic (a read-only file is refused on Windows and replaced on POSIX). INVALID_STATE when the document cannot be serialised (nothing is written), INVALID_ARGUMENT when the path is empty or ends in a separator or a directory (or a link to one) is at the path, IO_FAILURE for anything else; messages name the file (a refused rename in the form handed to the system, see write_native_file_atomic).
Private
array
arrayfriend CONJURE_API document array(
std::initializer_list<init_value> items);array_root
array_root[[nodiscard]] void *array_root();object
objectfriend CONJURE_API document object(
std::initializer_list<init_member> members);object_root
object_root[[nodiscard]] void *object_root();push_node
push_nodevoid push_node(
void *node);root_node
root_node[[nodiscard]] void *root_node() const noexcept;set_node
set_nodevoid set_node(
std::string_view key,
void *node);set_pointer_node
set_pointer_nodebool set_pointer_node(
std::string_view pointer,
void *node);Operator Overloads
Public
operator=
operator=operator==
operator==[[nodiscard]] bool operator==(
const document &other) const;Structural equality of the two roots (see value::operator==).