| # nlohmann::basic_json::number_integer_t | |
| ``` | |
| using number_integer_t = NumberIntegerType; | |
| ``` | |
| The type used to store JSON numbers (integers). | |
| [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: | |
| > The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. | |
| This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is known whether the number is a signed integer, an unsigned integer, or a floating-point number. Therefore, three different types, `number_integer_t`, [`number_unsigned_t`](https://json.nlohmann.me/api/basic_json/number_unsigned_t/index.md) and [`number_float_t`](https://json.nlohmann.me/api/basic_json/number_float_t/index.md) are used. | |
| To store integer numbers in C++, a type is defined by the template parameter `NumberIntegerType` which chooses the type to use. | |
| ## Template parameters | |
| `NumberIntegerType` : the type to store signed integers. It must be a **signed integral** type (`std::is_integral`) with a `std::numeric_limits` specialization, and it is stored directly inside a `basic_json` value. See [Template Parameter Requirements](https://json.nlohmann.me/features/types/template_parameters/#numberintegertype-and-numberunsignedtype). | |
| ## Notes | |
| ### Default type | |
| With the default values for `NumberIntegerType` (`std::int64_t`), the default type for `number_integer_t` is `std::int64_t`. | |
| ### Default behavior | |
| - The restrictions about leading zeros are not enforced in C++. Instead, leading zeros in integer literals lead to an interpretation as an octal number. Internally, the value will be stored as a decimal number. For instance, the C++ integer literal `010` will be serialized to `8`. During deserialization, leading zeros yield an error. | |
| ### Limits | |
| [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: | |
| > An implementation may set limits on the range and precision of numbers. | |
| When the default type is used, the maximal integer number that can be stored is `9223372036854775807` (INT64_MAX) and the minimal integer number that can be stored is `-9223372036854775808` (INT64_MIN). Integer numbers that are out of range will yield over/underflow when used in a constructor. During deserialization (from JSON text or any of the binary formats), too large or small integer numbers will automatically be stored as [`number_unsigned_t`](https://json.nlohmann.me/api/basic_json/number_unsigned_t/index.md) or [`number_float_t`](https://json.nlohmann.me/api/basic_json/number_float_t/index.md). | |
| [RFC 8259](https://tools.ietf.org/html/rfc8259) further states: | |
| > Note that when such software is used, numbers that are integers and are in the range [-253+1, 253-1] are interoperable in the sense that implementations will agree exactly on their numeric values. | |
| As this range is a subrange of the exactly supported range [INT64_MIN, INT64_MAX], this class's integer type is interoperable. | |
| ### Storage | |
| Integer number values are stored directly inside a `basic_json` type. | |
| ## Examples | |
| Example | |
| The following code shows that `number_integer_t` is by default, a typedef to `std::int64_t`. | |
| ``` | |
| #include <iostream> | |
| #include <iomanip> | |
| #include <nlohmann/json.hpp> | |
| using json = nlohmann::json; | |
| int main() | |
| { | |
| std::cout << std::boolalpha << std::is_same<std::int64_t, json::number_integer_t>::value << std::endl; | |
| } | |
| ``` | |
| Output: | |
| ``` | |
| true | |
| ``` | |
| ## See also | |
| - [number_unsigned_t](https://json.nlohmann.me/api/basic_json/number_unsigned_t/index.md) the type used to store JSON unsigned integer numbers | |
| - [number_float_t](https://json.nlohmann.me/api/basic_json/number_float_t/index.md) the type used to store JSON floating-point numbers | |
| - [is_number_integer](https://json.nlohmann.me/api/basic_json/is_number_integer/index.md) checks whether the JSON value is a signed integer number | |
| - [Number Handling](https://json.nlohmann.me/features/types/number_handling/index.md) - the article on number handling | |
| ## Version history | |
| - Added in version 1.0.0. |