• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <small>nlohmann::basic_json::</small>value_t
2
3```cpp
4enum class value_t : std::uint8_t {
5    null,
6    object,
7    array,
8    string,
9    boolean,
10    number_integer,
11    number_unsigned,
12    number_float,
13    binary,
14    discarded
15};
16```
17
18This enumeration collects the different JSON types. It is internally used to distinguish the stored values, and the
19functions [`is_null`](is_null.md), [`is_object`](is_object.md), [`is_array`](is_array.md), [`is_string`](is_string.md),
20[`is_boolean`](is_boolean.md), [`is_number`](is_number.md) (with [`is_number_integer`](is_number_integer.md),
21[`is_number_unsigned`](is_number_unsigned.md), and [`is_number_float`](is_number_float.md)),
22[`is_discarded`](is_discarded.md), [`is_binary`](is_binary.md), [`is_primitive`](is_primitive.md), and
23[`is_structured`](is_structured.md) rely on it.
24
25## Notes
26
27!!! note "Ordering"
28
29    The order of types is as follows:
30
31    1. `null`
32    2. `boolean`
33    3. `number_integer`, `number_unsigned`, `number_float`
34    4. `object`
35    5. `array`
36    6. `string`
37    7. `binary`
38
39    `discarded` is unordered.
40
41!!! note "Types of numbers"
42
43    There are three enumerators for numbers (`number_integer`, `number_unsigned`, and `number_float`) to distinguish
44    between different types of numbers:
45
46      - [`number_unsigned_t`](number_unsigned_t.md) for unsigned integers
47      - [`number_integer_t`](number_integer_t.md) for signed integers
48      - [`number_float_t`](number_float_t.md) for floating-point numbers or to approximate integers which do not fit
49        into the limits of their respective type
50
51!!! warning "Comparison operators"
52
53    `operator<` and `operator<=>` (since C++20) are overloaded and compare according to the ordering described above.
54    Until C++20 all other relational and equality operators yield results according to the integer value of each
55    enumerator. Since C++20 some compilers consider the _rewritten candidates_ generated from `operator<=>` during
56    overload resolution, while others do not. For predictable and portable behavior use:
57
58      - `operator<` or `operator<=>` when wanting to compare according to the order described above
59      - `operator==` or `operator!=` when wanting to compare according to each enumerators integer value
60
61## Examples
62
63??? example
64
65    The following code how `type()` queries the `value_t` for all JSON types.
66
67    ```cpp
68    --8<-- "examples/type.cpp"
69    ```
70
71    Output:
72
73    ```json
74    --8<-- "examples/type.output"
75    ```
76
77## Version history
78
79- Added in version 1.0.0.
80- Added unsigned integer type in version 2.0.0.
81- Added binary type in version 3.8.0.
82