Files
json/api/basic_json_document/node_count.md
T
2026-10-21 10:31:00 +00:00

1.6 KiB

nlohmann::basic_json_document::node_count

std::size_t node_count() const noexcept;

Returns the number of entries in the document's flat index.

Return value

The number of index entries: one per value (of any type, at any nesting depth) plus one per object key. 0 for a discarded document.

Exception safety

No-throw guarantee: this function never throws exceptions.

Complexity

Constant.

Notes

Each index entry is 16 bytes, so #!cpp node_count() * 16 is the size of the index itself (part, but not all, of memory_usage(), which also counts decoded strings and, for an owned document, the text).

Examples

??? example

```cpp
#include <iostream>
#include <nlohmann/json_view.hpp>

using json_document = nlohmann::json_document;

int main()
{
    // node_count() is the size of the flat index: one 16-byte node per value,
    // plus one per object key (values and keys are all the index stores)
    json_document scalar = json_document::parse("42");
    std::cout << scalar.node_count() << '\n';

    json_document doc = json_document::parse(R"({"a": 1, "b": [1, 2]})");
    std::cout << doc.node_count() << '\n';
}

```

Output:

```json
1
7

```

See also

Version history

  • Added in version 3.13.0.