Skip to content

Engine → Core → document.h

json::document

Syntax

class CONJURE_API document

Constructors

AccessSignatureDescription
publicdocument();A document whose root is JSON null. The first set() turns the root into an object, the first push_back() into an array.
publicdocument( const document &other);
publicdocument( 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.
privateexplicit document( void *handle) noexcept;

Destructors

AccessSignatureDescription
public~document();

Variables

AccessNameTypeDefaultDescription
privatemHandlevoid *nullptr

Functions

Public

array

[[nodiscard]] static document array();

A document whose root is an empty array.

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

void clear();

Removes every member or element of a container root; any other root becomes null.

erase

bool 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

[[nodiscard]] static document from_value(
    const value &source);

A document whose root is a deep copy of source (any node of any document). An invalid source yields a null root.

merge_patch

bool 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);

object

[[nodiscard]] static document object();

A document whose root is an empty object.

parse

[[nodiscard]] static result<document> parse(
    std::string_view text);

Parses 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

[[nodiscard]] static result<document> parse_file(
    const std::filesystem::path &path);

Reads 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

void 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

[[nodiscard]] value root() const noexcept;

A view of the root value. Valid for every document except a moved-from one.

set

void 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

void set_null(
    std::string_view key);

Sets key to JSON null (the member stays; use erase() to remove it).

set_pointer

bool 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

[[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

[[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

[[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

friend CONJURE_API document array(
    std::initializer_list<init_value> items);

array_root

[[nodiscard]] void *array_root();

object

friend CONJURE_API document object(
    std::initializer_list<init_member> members);

object_root

[[nodiscard]] void *object_root();

push_node

void push_node(
    void *node);

root_node

[[nodiscard]] void *root_node() const noexcept;

set_node

void set_node(
    std::string_view key,
    void *node);

set_pointer_node

bool set_pointer_node(
    std::string_view pointer,
    void *node);

Operator Overloads

Public

operator=

document &operator=(
    const document &other);
document &operator=(
    document &&other) noexcept;

operator==

[[nodiscard]] bool operator==(
    const document &other) const;

Structural equality of the two roots (see value::operator==).