• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# basic_json::empty
2
3```cpp
4bool empty() const noexcept;
5```
6
7Checks if a JSON value has no elements (i.e. whether its [`size()`](size.md) is `0`).
8
9## Return value
10
11The return value depends on the different types and is defined as follows:
12
13Value type  | return value
14----------- | -------------
15null        | `#!cpp true`
16boolean     | `#!cpp false`
17string      | `#!cpp false`
18number      | `#!cpp false`
19binary      | `#!cpp false`
20object      | result of function `object_t::empty()`
21array       | result of function `array_t::empty()`
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 `empty()` functions have
31constant complexity.
32
33## Possible implementation
34
35```cpp
36bool empty() const noexcept
37{
38    return size() == 0;
39}
40```
41
42## Notes
43
44This function does not return whether a string stored as JSON value is empty -- it returns whether the JSON container
45itself is empty which is `#!cpp false` in the case of a string.
46
47## Example
48
49??? example
50
51    The following code uses `empty()` to check if a JSON object contains any elements.
52
53    ```cpp
54    --8<-- "examples/empty.cpp"
55    ```
56
57    Output:
58
59    ```json
60    --8<-- "examples/empty.output"
61    ```
62
63## Version history
64
65- Added in version 1.0.0.
66- Extended to return `#!cpp false` for binary types in version 3.8.0.
67