• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <small>nlohmann::basic_json::</small>size
2
3```cpp
4size_type size() const noexcept;
5```
6
7Returns the number of elements in a JSON value.
8
9## Return value
10
11The return value depends on the different types and is defined as follows:
12
13| Value type | return value                        |
14|------------|-------------------------------------|
15| null       | `0`                                 |
16| boolean    | `1`                                 |
17| string     | `1`                                 |
18| number     | `1`                                 |
19| binary     | `1`                                 |
20| object     | result of function object_t::size() |
21| array      | result of function array_t::size()  |
22
23## Exception safety
24
25No-throw guarantee: this function never throws exceptions.
26
27## Complexity
28
29Constant, as long as [`array_t`](array_t.md) and [`object_t`](object_t.md) satisfy the
30[Container](https://en.cppreference.com/w/cpp/named_req/Container) concept; that is, their `size()` functions have
31constant complexity.
32
33## Notes
34
35This function does not return the length of a string stored as JSON value -- it returns the number of elements in the
36JSON value which is `1` in the case of a string.
37
38## Examples
39
40??? example
41
42    The following code calls `size()` on the different value types.
43
44    ```cpp
45    --8<-- "examples/size.cpp"
46    ```
47
48    Output:
49
50    ```json
51    --8<-- "examples/size.output"
52    ```
53
54## Version history
55
56- Added in version 1.0.0.
57- Extended to return `1` for binary types in version 3.8.0.
58