mirror of
https://github.com/nlohmann/json.git
synced 2026-11-23 19:32:37 +00:00
deploy: 7c9208bfb3
This commit is contained in:
1 parent
9a231845c2
commit
279d757031
758 files changed
+56489
-503
No files matched your search
@@ -77,4 +77,4 @@ basic_json .. value_t</code></pre> <h2 id=template-arguments>Template arguments<
|
||||
<span class=w> </span><span class=n>basic_json</span><span class=p>,</span><span class=w> </span><span class=c1>// value_type</span>
|
||||
<span class=w> </span><span class=n>std</span><span class=o>::</span><span class=n>allocator</span><span class=o><</span><span class=n>basic_json</span><span class=o>></span><span class=w> </span><span class=c1>// allocator_type</span>
|
||||
<span class=o>></span>
|
||||
</code></pre></div> <h3 id=limits_1>Limits<a class=headerlink href=#limits_1 title="Permanent link">¶</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> specifies:</p> <blockquote> <p>An implementation may set limits on the maximum depth of nesting.</p> </blockquote> <p>In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the <code>max_size</code> function of a <abbr title="JavaScript Object Notation">JSON</abbr> array.</p> <h3 id=storage_2>Storage<a class=headerlink href=#storage_2 title="Permanent link">¶</a></h3> <p>Arrays are stored as pointers in a <code>basic_json</code> type. That is, for any access to array values, a pointer of type <code>array_t*</code> must be dereferenced.</p> <h2 id=strings>Strings<a class=headerlink href=#strings title="Permanent link">¶</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> strings as follows:</p> <blockquote> <p>A string is a sequence of zero or more Unicode characters.</p> </blockquote> <p>Unicode values are split by the <abbr title="JavaScript Object Notation">JSON</abbr> class into byte-sized characters during deserialization.</p> <h3 id=default-type_2>Default type<a class=headerlink href=#default-type_2 title="Permanent link">¶</a></h3> <p>With the default values for <em>StringType</em> (<code>std::string</code>), the default value for <code>string_t</code> is <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string</span></code>.</p> <h3 id=encoding>Encoding<a class=headerlink href=#encoding title="Permanent link">¶</a></h3> <p>Strings are stored in <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoding. Therefore, functions like <code>std::string::size()</code> or <code>std::string::length()</code> return the number of <strong>bytes</strong> in the string rather than the number of characters or glyphs.</p> <h3 id=string-comparison>String comparison<a class=headerlink href=#string-comparison title="Permanent link">¶</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> states:</p> <blockquote> <p>Software implementations are typically required to test names of object members for equality. Implementations that transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, code unit by code unit are interoperable in the sense that implementations will agree in all cases on equality or inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may incorrectly find that <code>"a\\b"</code> and <code>"a\u005Cb"</code> are not equal.</p> </blockquote> <p>This implementation is interoperable as it does compare strings code unit by code unit.</p> <h3 id=storage_3>Storage<a class=headerlink href=#storage_3 title="Permanent link">¶</a></h3> <p>String values are stored as pointers in a <code>basic_json</code> type. That is, for any access to string values, a pointer of type <code>string_t*</code> must be dereferenced.</p> <h2 id=booleans>Booleans<a class=headerlink href=#booleans title="Permanent link">¶</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> implicitly describes a boolean as a type which differentiates the two literals <code>true</code> and <code>false</code>.</p> <h3 id=default-type_3>Default type<a class=headerlink href=#default-type_3 title="Permanent link">¶</a></h3> <p>With the default values for <em>BooleanType</em> (<code class=highlight><span class=kt>bool</span></code>), the default value for <code>boolean_t</code> is <code class=highlight><span class=kt>bool</span></code>.</p> <h3 id=storage_4>Storage<a class=headerlink href=#storage_4 title="Permanent link">¶</a></h3> <p>Boolean values are stored directly inside a <code>basic_json</code> type.</p> <h2 id=numbers>Numbers<a class=headerlink href=#numbers title="Permanent link">¶</a></h2> <p>See the <a href=number_handling/ >number handling</a> article for a detailed discussion on how numbers are handled by this library.</p> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes numbers as follows:</p> <blockquote> <p>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 reprLine truncated
|
||||
</code></pre></div> <h3 id=limits_1>Limits<a class=headerlink href=#limits_1 title="Permanent link">¶</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> specifies:</p> <blockquote> <p>An implementation may set limits on the maximum depth of nesting.</p> </blockquote> <p>In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the <code>max_size</code> function of a <abbr title="JavaScript Object Notation">JSON</abbr> array.</p> <h3 id=storage_2>Storage<a class=headerlink href=#storage_2 title="Permanent link">¶</a></h3> <p>Arrays are stored as pointers in a <code>basic_json</code> type. That is, for any access to array values, a pointer of type <code>array_t*</code> must be dereferenced.</p> <h2 id=strings>Strings<a class=headerlink href=#strings title="Permanent link">¶</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes <abbr title="JavaScript Object Notation">JSON</abbr> strings as follows:</p> <blockquote> <p>A string is a sequence of zero or more Unicode characters.</p> </blockquote> <p>Unicode values are split by the <abbr title="JavaScript Object Notation">JSON</abbr> class into byte-sized characters during deserialization.</p> <h3 id=default-type_2>Default type<a class=headerlink href=#default-type_2 title="Permanent link">¶</a></h3> <p>With the default values for <em>StringType</em> (<code>std::string</code>), the default value for <code>string_t</code> is <code class=highlight><span class=n>std</span><span class=o>::</span><span class=n>string</span></code>.</p> <h3 id=encoding>Encoding<a class=headerlink href=#encoding title="Permanent link">¶</a></h3> <p>Strings are stored in <abbr title="Unicode Transformation Format">UTF</abbr>-8 encoding. Therefore, functions like <code>std::string::size()</code> or <code>std::string::length()</code> return the number of <strong>bytes</strong> in the string rather than the number of characters or glyphs.</p> <h3 id=string-comparison>String comparison<a class=headerlink href=#string-comparison title="Permanent link">¶</a></h3> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> states:</p> <blockquote> <p>Software implementations are typically required to test names of object members for equality. Implementations that transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, code unit by code unit are interoperable in the sense that implementations will agree in all cases on equality or inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may incorrectly find that <code>"a\\b"</code> and <code>"a\u005Cb"</code> are not equal.</p> </blockquote> <p>This implementation is interoperable as it does compare strings code unit by code unit.</p> <h3 id=storage_3>Storage<a class=headerlink href=#storage_3 title="Permanent link">¶</a></h3> <p>String values are stored as pointers in a <code>basic_json</code> type. That is, for any access to string values, a pointer of type <code>string_t*</code> must be dereferenced.</p> <h2 id=booleans>Booleans<a class=headerlink href=#booleans title="Permanent link">¶</a></h2> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> implicitly describes a boolean as a type which differentiates the two literals <code>true</code> and <code>false</code>.</p> <h3 id=default-type_3>Default type<a class=headerlink href=#default-type_3 title="Permanent link">¶</a></h3> <p>With the default values for <em>BooleanType</em> (<code class=highlight><span class=kt>bool</span></code>), the default value for <code>boolean_t</code> is <code class=highlight><span class=kt>bool</span></code>.</p> <h3 id=storage_4>Storage<a class=headerlink href=#storage_4 title="Permanent link">¶</a></h3> <p>Boolean values are stored directly inside a <code>basic_json</code> type.</p> <h2 id=numbers>Numbers<a class=headerlink href=#numbers title="Permanent link">¶</a></h2> <p>See the <a href=number_handling/ >number handling</a> article for a detailed discussion on how numbers are handled by this library.</p> <p><a href="https://tools.ietf.org/html/rfc8259"><abbr title="Request for Comments">RFC</abbr> 8259</a> describes numbers as follows:</p> <blockquote> <p>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 reprLine truncated
|
||||
@@ -0,0 +1,265 @@
|
||||
# Types
|
||||
|
||||
This page gives an overview of how JSON values are stored and how this can be configured.
|
||||
|
||||
## Overview
|
||||
|
||||
By default, JSON values are stored as follows:
|
||||
|
||||
| JSON type | C++ type |
|
||||
| --------- | --------------------------------------------- |
|
||||
| object | `std::map<std::string, basic_json>` |
|
||||
| array | `std::vector<basic_json>` |
|
||||
| null | `std::nullptr_t` |
|
||||
| string | `std::string` |
|
||||
| boolean | `bool` |
|
||||
| number | `std::int64_t`, `std::uint64_t`, and `double` |
|
||||
|
||||
Note there are three different types for numbers - when parsing JSON text, the best fitting type is chosen.
|
||||
|
||||
## Storage
|
||||
|
||||
```
|
||||
classDiagram
|
||||
|
||||
class value_t {
|
||||
<<enumeration>>
|
||||
null
|
||||
object
|
||||
array
|
||||
string
|
||||
boolean
|
||||
number_integer
|
||||
number_unsigned
|
||||
number_float
|
||||
binary
|
||||
discarded
|
||||
}
|
||||
|
||||
class json_value {
|
||||
<<union>>
|
||||
object_t* object
|
||||
array_t* array
|
||||
string_t* string
|
||||
binary_t* binary
|
||||
boolean_t boolean
|
||||
number_integer_t number_integer
|
||||
number_unsigned_t number_unsigned
|
||||
number_float_t number_float
|
||||
}
|
||||
|
||||
class basic_json {
|
||||
-value_t m_type
|
||||
-json_value m_value
|
||||
+typedef object_t
|
||||
+typedef array_t
|
||||
+typedef binary_t
|
||||
+typedef boolean_t
|
||||
+typedef number_integer_t
|
||||
+typedef number_unsigned_t
|
||||
+typedef number_float_t
|
||||
}
|
||||
|
||||
basic_json .. json_value
|
||||
basic_json .. value_t
|
||||
```
|
||||
|
||||
## Template arguments
|
||||
|
||||
The data types to store a JSON value are derived from the template arguments passed to class `basic_json`:
|
||||
|
||||
```
|
||||
template<
|
||||
template<typename U, typename V, typename... Args> class ObjectType = std::map,
|
||||
template<typename U, typename... Args> class ArrayType = std::vector,
|
||||
class StringType = std::string,
|
||||
class BooleanType = bool,
|
||||
class NumberIntegerType = std::int64_t,
|
||||
class NumberUnsignedType = std::uint64_t,
|
||||
class NumberFloatType = double,
|
||||
template<typename U> class AllocatorType = std::allocator,
|
||||
template<typename T, typename SFINAE = void> class JSONSerializer = adl_serializer,
|
||||
class BinaryType = std::vector<std::uint8_t>
|
||||
>
|
||||
class basic_json;
|
||||
```
|
||||
|
||||
Type `json` is an alias for `basic_json<>` and uses the default types.
|
||||
|
||||
From the template arguments, the following types are derived:
|
||||
|
||||
```
|
||||
using object_comparator_t = std::less<>;
|
||||
using object_t = ObjectType<StringType, basic_json, object_comparator_t,
|
||||
AllocatorType<std::pair<const StringType, basic_json>>>;
|
||||
|
||||
using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
|
||||
|
||||
using string_t = StringType;
|
||||
|
||||
using boolean_t = BooleanType;
|
||||
|
||||
using number_integer_t = NumberIntegerType;
|
||||
using number_unsigned_t = NumberUnsignedType;
|
||||
using number_float_t = NumberFloatType;
|
||||
|
||||
using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
|
||||
```
|
||||
|
||||
## Objects
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON objects as follows:
|
||||
|
||||
> An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
|
||||
|
||||
### Default type
|
||||
|
||||
With the default values for *ObjectType* (`std::map`), *StringType* (`std::string`), and *AllocatorType* (`std::allocator`), the default value for `object_t` is:
|
||||
|
||||
```
|
||||
std::map<
|
||||
std::string, // key_type
|
||||
basic_json, // value_type
|
||||
std::less<>, // key_compare
|
||||
std::allocator<std::pair<const std::string, basic_json>> // allocator_type
|
||||
>
|
||||
```
|
||||
|
||||
### Behavior
|
||||
|
||||
The choice of `object_t` influences the behavior of the JSON class. With the default type, objects have the following behavior:
|
||||
|
||||
- When all names are unique, objects will be interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings.
|
||||
- When the names within an object are not unique, it is unspecified which one of the values for a given key will be chosen. For instance, `{"key": 2, "key": 1}` could be equal to either `{"key": 1}` or `{"key": 2}`.
|
||||
- Internally, name/value pairs are stored in lexicographical order of the names. Objects will also be serialized (see `dump`) in this order. For instance, both `{"b": 1, "a": 2}` and `{"a": 2, "b": 1}` will be stored and serialized as `{"a": 2, "b": 1}`.
|
||||
- When comparing objects, the order of the name/value pairs is irrelevant. This makes objects interoperable in the sense that they will not be affected by these differences. For instance, `{"b": 1, "a": 2}` and `{"a": 2, "b": 1}` will be treated as equal.
|
||||
|
||||
### Key order
|
||||
|
||||
The order in which name/value pairs are added to the object is *not* preserved by the library. Therefore, iterating an object may return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to [RFC 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON objects.
|
||||
|
||||
### Limits
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies:
|
||||
|
||||
> An implementation may set limits on the maximum depth of nesting.
|
||||
|
||||
In this class, the object's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the `max_size` function of a JSON object.
|
||||
|
||||
### Storage
|
||||
|
||||
Objects are stored as pointers in a `basic_json` type. That is, for any access to object values, a pointer of type `object_t*` must be dereferenced.
|
||||
|
||||
## Arrays
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON arrays as follows:
|
||||
|
||||
> An array is an ordered sequence of zero or more values.
|
||||
|
||||
### Default type
|
||||
|
||||
With the default values for *ArrayType* (`std::vector`) and *AllocatorType* (`std::allocator`), the default value for `array_t` is:
|
||||
|
||||
```
|
||||
std::vector<
|
||||
basic_json, // value_type
|
||||
std::allocator<basic_json> // allocator_type
|
||||
>
|
||||
```
|
||||
|
||||
### Limits
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies:
|
||||
|
||||
> An implementation may set limits on the maximum depth of nesting.
|
||||
|
||||
In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the `max_size` function of a JSON array.
|
||||
|
||||
### Storage
|
||||
|
||||
Arrays are stored as pointers in a `basic_json` type. That is, for any access to array values, a pointer of type `array_t*` must be dereferenced.
|
||||
|
||||
## Strings
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows:
|
||||
|
||||
> A string is a sequence of zero or more Unicode characters.
|
||||
|
||||
Unicode values are split by the JSON class into byte-sized characters during deserialization.
|
||||
|
||||
### Default type
|
||||
|
||||
With the default values for *StringType* (`std::string`), the default value for `string_t` is `std::string`.
|
||||
|
||||
### Encoding
|
||||
|
||||
Strings are stored in UTF-8 encoding. Therefore, functions like `std::string::size()` or `std::string::length()` return the number of **bytes** in the string rather than the number of characters or glyphs.
|
||||
|
||||
### String comparison
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) states:
|
||||
|
||||
> Software implementations are typically required to test names of object members for equality. Implementations that transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, code unit by code unit are interoperable in the sense that implementations will agree in all cases on equality or inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may incorrectly find that `"a\\b"` and `"a\u005Cb"` are not equal.
|
||||
|
||||
This implementation is interoperable as it does compare strings code unit by code unit.
|
||||
|
||||
### Storage
|
||||
|
||||
String values are stored as pointers in a `basic_json` type. That is, for any access to string values, a pointer of type `string_t*` must be dereferenced.
|
||||
|
||||
## Booleans
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two literals `true` and `false`.
|
||||
|
||||
### Default type
|
||||
|
||||
With the default values for *BooleanType* (`bool`), the default value for `boolean_t` is `bool`.
|
||||
|
||||
### Storage
|
||||
|
||||
Boolean values are stored directly inside a `basic_json` type.
|
||||
|
||||
## Numbers
|
||||
|
||||
See the [number handling](https://json.nlohmann.me/features/types/number_handling/index.md) article for a detailed discussion on how numbers are handled by this library.
|
||||
|
||||
[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`, and `number_float_t` are used.
|
||||
|
||||
### Default types
|
||||
|
||||
With the default values for *NumberIntegerType* (`std::int64_t`), the default value for `number_integer_t` is `std::int64_t`. With the default values for *NumberUnsignedType* (`std::uint64_t`), the default value for `number_unsigned_t` is `std::uint64_t`. With the default values for *NumberFloatType* (`double`), the default value for `number_float_t` is `double`.
|
||||
|
||||
### 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.
|
||||
- Not-a-number (NaN) values will be serialized to `null`.
|
||||
|
||||
### 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, too large or small integer numbers will automatically be stored as `number_unsigned_t` or `number_float_t`.
|
||||
|
||||
When the default type is used, the maximal unsigned integer number that can be stored is `18446744073709551615` (`UINT64_MAX`) and the minimal integer number that can be stored is `0`. Integer numbers that are out of range will yield over/underflow when used in a constructor. During deserialization, too large or small integer numbers will automatically be stored as `number_integer_t` or `number_float_t`.
|
||||
|
||||
[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 [-2^{53}+1, 2^{53}-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.
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259) states:
|
||||
|
||||
> This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision.
|
||||
|
||||
This implementation does exactly follow this approach, as it uses double precision floating-point numbers. Note values smaller than `-1.79769313486232e+308` and values greater than `1.79769313486232e+308` will be stored as NaN internally and be serialized to `null`.
|
||||
|
||||
### Storage
|
||||
|
||||
Integer number values, unsigned integer number values, and floating-point number values are stored directly inside a `basic_json` type.
|
||||
@@ -0,0 +1,341 @@
|
||||
# Number Handling
|
||||
|
||||
This document describes how the library is handling numbers.
|
||||
|
||||
## Background
|
||||
|
||||
This section briefly summarizes how the JSON specification describes how numbers should be handled.
|
||||
|
||||
### JSON number syntax
|
||||
|
||||
JSON defines the syntax of numbers as follows:
|
||||
|
||||
!!! quote "[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6"
|
||||
|
||||
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.
|
||||
|
||||
A fraction part is a decimal point followed by one or more digits.
|
||||
|
||||
An exponent part begins with the letter E in uppercase or lowercase,
|
||||
which may be followed by a plus or minus sign. The E and optional
|
||||
sign are followed by one or more digits.
|
||||
|
||||
The following railroad diagram from [json.org](https://json.org) visualizes the number syntax:
|
||||
|
||||

|
||||
|
||||
### Number interoperability
|
||||
|
||||
On number interoperability, the following remarks are made:
|
||||
|
||||
!!! quote "[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6"
|
||||
|
||||
This specification allows implementations to set limits on the range
|
||||
and precision of numbers accepted. Since software that implements
|
||||
IEEE 754 binary64 (double precision) numbers [IEEE754] is generally
|
||||
available and widely used, good interoperability can be achieved by
|
||||
implementations that expect no more precision or range than these
|
||||
provide, in the sense that implementations will approximate JSON
|
||||
numbers within the expected precision. A JSON number such as 1E400
|
||||
or 3.141592653589793238462643383279 may indicate potential
|
||||
interoperability problems, since it suggests that the software that
|
||||
created it expects receiving software to have greater capabilities
|
||||
for numeric magnitude and precision than is widely available.
|
||||
|
||||
Note that when such software is used, numbers that are integers and
|
||||
are in the range $[-2^{53}+1, 2^{53}-1]$ are interoperable in the
|
||||
sense that implementations will agree exactly on their numeric
|
||||
values.
|
||||
|
||||
## Library implementation
|
||||
|
||||
This section describes how this library implements the above number specification.
|
||||
|
||||
### Number storage
|
||||
|
||||
In the default [`json`](../../api/json.md) type, numbers are stored as `#!c std::uint64_t`, `#!c std::int64_t`, and
|
||||
`#!c double`, respectively. Thereby, `#!c std::uint64_t` and `#!c std::int64_t` are used only if they can store the
|
||||
number without loss of precision. If this is impossible (e.g., if the number is too large), the number is stored as
|
||||
`#!c double`.
|
||||
|
||||
!!! info "Notes"
|
||||
|
||||
- Numbers with a decimal digit or scientific notation are always stored as `#!c double`.
|
||||
- The number types can be changed, see [Template number types](#template-number-types).
|
||||
- As of version 3.9.1, the conversion is realized by
|
||||
[`std::strtoull`](https://en.cppreference.com/w/cpp/string/byte/strtoul),
|
||||
[`std::strtoll`](https://en.cppreference.com/w/cpp/string/byte/strtol), and
|
||||
[`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof), respectively.
|
||||
|
||||
!!! example "Examples"
|
||||
|
||||
- Integer `#!c -12345678912345789123456789` is smaller than `#!c INT64_MIN` and will be stored as floating-point
|
||||
number `#!c -1.2345678912345788e+25`.
|
||||
- Integer `#!c 1E3` will be stored as floating-point number `#!c 1000.0`.
|
||||
|
||||
### Number limits
|
||||
|
||||
- Any 64-bit signed or unsigned integer can be stored without loss of precision.
|
||||
- Numbers exceeding the limits of `#!c double` (i.e., numbers that after conversion via
|
||||
[`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof) are not satisfying
|
||||
[`std::isfinite`](https://en.cppreference.com/w/cpp/numeric/math/isfinite) such as `#!c 1E400`) will throw exception
|
||||
[`json.exception.out_of_range.406`](../../home/exceptions.md#jsonexceptionout_of_range406) during parsing.
|
||||
- Floating-point numbers are rounded to the next number representable as `double`. For instance
|
||||
`#!c 3.141592653589793238462643383279` is stored as [`0x400921fb54442d18`](https://float.exposed/0x400921fb54442d18).
|
||||
This is the same behavior as the code `#!c double x = 3.141592653589793238462643383279;`.
|
||||
|
||||
!!! success "Interoperability"
|
||||
|
||||
- The library is interoperable with respect to the specification, because its supported range $[-2^{63}, 2^{64}-1]$ is
|
||||
larger than the described range $[-2^{53}+1, 2^{53}-1]$.
|
||||
- All integers outside the range $[-2^{63}, 2^{64}-1]$, as well as floating-point numbers are stored as `double`.
|
||||
This also concurs with the specification above.
|
||||
|
||||
### Zeros
|
||||
|
||||
The JSON number grammar allows for different ways to express zero, and this library will store zeros differently:
|
||||
|
||||
| Literal | Stored value and type | Serialization |
|
||||
|---------|------------------------|---------------|
|
||||
| `0` | `#!c std::uint64_t(0)` | `0` |
|
||||
| `-0` | `#!c std::int64_t(0)` | `0` |
|
||||
| `0.0` | `#!c double(0.0)` | `0.0` |
|
||||
| `-0.0` | `#!c double(-0.0)` | `-0.0` |
|
||||
| `0E0` | `#!c double(0.0)` | `0.0` |
|
||||
| `-0E0` | `#!c double(-0.0)` | `-0.0` |
|
||||
|
||||
That is, `-0` is stored as a signed integer, but the serialization does not reproduce the `-`.
|
||||
|
||||
### Number serialization
|
||||
|
||||
- Integer numbers are serialized as is; that is, no scientific notation is used.
|
||||
- Floating-point numbers are serialized as specified by the `#!c %g` printf modifier with
|
||||
[`std::numeric_limits<double>::max_digits10`](https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10)
|
||||
significant digits. The rationale is to use the shortest representation while still allowing round-tripping.
|
||||
|
||||
!!! hint "Notes regarding precision of floating-point numbers"
|
||||
|
||||
As described above, floating-point numbers are rounded to the nearest double and serialized with the shortest
|
||||
representation to allow round-tripping. This can yield confusing examples:
|
||||
|
||||
- The serialization can have fewer decimal places than the input: `#!c 2555.5599999999999` will be serialized as
|
||||
`#!c 2555.56`. The reverse can also be true.
|
||||
- The serialization can be in scientific notation even if the input is not: `#!c 0.0000972439793401814` will be
|
||||
serialized as `#!c 9.72439793401814e-05`. The reverse can also be true: `#!c 12345E-5` will be serialized as
|
||||
`#!c 0.12345`.
|
||||
- Conversions from `#!c float` to `#!c double` can also introduce rounding errors:
|
||||
```cpp
|
||||
float f = 0.3;
|
||||
json j = f;
|
||||
std::cout << j << '\n';
|
||||
```
|
||||
yields `#!c 0.30000001192092896`.
|
||||
|
||||
All examples here can be reproduced by passing the original double value to
|
||||
|
||||
```cpp
|
||||
std::printf("%.*g\n", std::numeric_limits<double>::max_digits10, double_value);
|
||||
```
|
||||
|
||||
#### NaN handling
|
||||
|
||||
NaN (not-a-number) cannot be expressed with the number syntax described above and are in fact explicitly excluded:
|
||||
|
||||
!!! quote "[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6"
|
||||
|
||||
Numeric values that cannot be represented in the grammar below (such
|
||||
as Infinity and NaN) are not permitted.
|
||||
|
||||
That is, there is no way to *parse* a NaN value. However, assignments can store NaN values in a JSON value.
|
||||
|
||||
This library serializes NaN values as `#!js null`. This corresponds to the behavior of JavaScript's
|
||||
[`JSON.stringify`](https://www.w3schools.com/js/js_json_stringify.asp) function.
|
||||
|
||||
!!! example
|
||||
|
||||
The following example shows how a NaN value is stored in a `json` value.
|
||||
|
||||
```cpp
|
||||
int main()
|
||||
{
|
||||
double val = std::numeric_limits<double>::quiet_NaN();
|
||||
std::cout << "val=" << val << std::endl;
|
||||
json j = val;
|
||||
std::cout << "j=" << j.dump() << std::endl;
|
||||
val = j;
|
||||
std::cout << "val=" << val << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
output:
|
||||
|
||||
```
|
||||
val=nan
|
||||
j=null
|
||||
val=nan
|
||||
```
|
||||
|
||||
### Number comparison
|
||||
|
||||
Floating-point numbers inside JSON values are compared with `#!c json::number_float_t::operator==` which is
|
||||
`#!c double::operator==` by default.
|
||||
|
||||
!!! example "Alternative comparison functions"
|
||||
|
||||
To compare floating-point while respecting an epsilon, an alternative
|
||||
[comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39)
|
||||
could be used, for instance
|
||||
|
||||
```cpp
|
||||
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
|
||||
inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
|
||||
{
|
||||
return std::abs(a - b) <= epsilon;
|
||||
}
|
||||
```
|
||||
Or you can self-define an operator equal function like this:
|
||||
|
||||
```cpp
|
||||
bool my_equal(const_reference lhs, const_reference rhs)
|
||||
{
|
||||
const auto lhs_type = lhs.type();
|
||||
const auto rhs_type = rhs.type();
|
||||
if (lhs_type == rhs_type)
|
||||
{
|
||||
switch(lhs_type)
|
||||
{
|
||||
// self_defined case
|
||||
case value_t::number_float:
|
||||
return std::abs(lhs - rhs) <= std::numeric_limits<float>::epsilon();
|
||||
|
||||
// other cases remain the same with the original
|
||||
...
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
(see [#703](https://github.com/nlohmann/json/issues/703) for more information.)
|
||||
|
||||
!!! note
|
||||
|
||||
NaN values never compare equal to themselves or to other NaN values. See [#514](https://github.com/nlohmann/json/issues/514).
|
||||
|
||||
### Number conversion
|
||||
|
||||
Just like the C++ language itself, the `get` family of functions allows conversions between unsigned and signed
|
||||
integers, and between integers and floating-point values. This behavior may be surprising.
|
||||
|
||||
!!! warning "Unconditional number conversions"
|
||||
|
||||
```cpp hl_lines="3"
|
||||
double d = 42.3; // non-integer double value 42.3
|
||||
json jd = d; // stores double value 42.3
|
||||
std::int64_t i = jd.get<std::int64_t>(); // now i==42; no warning or error is produced
|
||||
```
|
||||
|
||||
Note the last line with throw a [`json.exception.type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302)
|
||||
exception if `jd` is not a numerical type, for instance a string.
|
||||
|
||||
Numeric conversions are performed according to the corresponding C++ conversion rules. The library does not perform
|
||||
range checks when converting between numeric types.
|
||||
|
||||
In particular, conversions from floating-point values to integer types, or conversions to integer types with a
|
||||
smaller range than the stored value, may produce implementation-defined or undefined behavior if the source value
|
||||
cannot be represented by the target type.
|
||||
|
||||
Applications requiring checked conversions should inspect the stored number type with
|
||||
[`is_number_float()`](../../api/basic_json/is_number_float.md),
|
||||
[`is_number_integer()`](../../api/basic_json/is_number_integer.md),
|
||||
[`is_number_unsigned()`](../../api/basic_json/is_number_unsigned.md), or
|
||||
[`type()`](../../api/basic_json/type.md), and perform explicit range checks before converting to a narrower type.
|
||||
|
||||
The rationale is twofold:
|
||||
|
||||
1. JSON does not define a number type or precision (see above).
|
||||
2. C++ also allows silently converting between number types.
|
||||
|
||||
!!! success "Conditional number conversion"
|
||||
|
||||
The code above can be solved by explicitly checking the nature of the value with members such as
|
||||
[`is_number_integer()`](../../api/basic_json/is_number_integer.md) or
|
||||
[`is_number_unsigned()`](../../api/basic_json/is_number_unsigned.md):
|
||||
|
||||
```cpp hl_lines="2"
|
||||
// check if jd is really integer-valued
|
||||
if (jd.is_number_integer())
|
||||
{
|
||||
// if so, do the conversion and use i
|
||||
std::int64_t i = jd.get<std::int64_t>();
|
||||
// ...
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, take appropriate action
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Note this approach also has the advantage that it can react on non-numerical JSON value types such as strings.
|
||||
|
||||
(Example taken from [#777](https://github.com/nlohmann/json/issues/777#issuecomment-459968458).)
|
||||
|
||||
### Determine number types
|
||||
|
||||
As the example in [Number conversion](#number-conversion) shows, there are different functions to determine the type of
|
||||
the stored number:
|
||||
|
||||
- [`is_number()`](../../api/basic_json/is_number.md) returns `#!c true` for any number type
|
||||
- [`is_number_integer()`](../../api/basic_json/is_number_integer.md) returns `#!c true` for signed and unsigned integers
|
||||
- [`is_number_unsigned()`](../../api/basic_json/is_number_unsigned.md) returns `#!c true` for unsigned integers only
|
||||
- [`is_number_float()`](../../api/basic_json/is_number_float.md) returns `#!c true` for floating-point numbers
|
||||
- [`type_name()`](../../api/basic_json/type_name.md) returns `#!c "number"` for any number type
|
||||
- [`type()`](../../api/basic_json/type.md) returns a different enumerator of
|
||||
[`value_t`](../../api/basic_json/value_t.md) for all number types
|
||||
|
||||
| function | unsigned integer | signed integer | floating-point | string |
|
||||
|----------------------------------------------------------------------|-------------------|------------------|----------------|----------------|
|
||||
| [`is_number()`](../../api/basic_json/is_number.md) | `#!c true` | `#!c true` | `#!c true` | `#!c false` |
|
||||
| [`is_number_integer()`](../../api/basic_json/is_number_integer.md) | `#!c true` | `#!c true` | `#!c false` | `#!c false` |
|
||||
| [`is_number_unsigned()`](../../api/basic_json/is_number_unsigned.md) | `#!c true` | `#!c false` | `#!c false` | `#!c false` |
|
||||
| [`is_number_float()`](../../api/basic_json/is_number_float.md) | `#!c false` | `#!c false` | `#!c true` | `#!c false` |
|
||||
| [`type_name()`](../../api/basic_json/type_name.md) | `#!c "number"` | `#!c "number"` | `#!c "number"` | `#!c "string"` |
|
||||
| [`type()`](../../api/basic_json/type.md) | `number_unsigned` | `number_integer` | `number_float` | `string` |
|
||||
|
||||
### Template number types
|
||||
|
||||
The number types can be changed with template parameters.
|
||||
|
||||
| position | number type | default type | possible values |
|
||||
|----------|-------------------|---------------------|------------------------------------------------|
|
||||
| 5 | signed integers | `#!c std::int64_t` | `#!c std::int32_t`, `#!c std::int16_t`, etc. |
|
||||
| 6 | unsigned integers | `#!c std::uint64_t` | `#!c std::uint32_t`, `#!c std::uint16_t`, etc. |
|
||||
| 7 | floating-point | `#!c double` | `#!c float`, `#!c long double` |
|
||||
|
||||
!!! info "Constraints on number types"
|
||||
|
||||
- The type for signed integers must be convertible from `#!c long long`. The type for floating-point numbers is used
|
||||
in case of overflow.
|
||||
- The type for unsigned integers must be convertible from `#!c unsigned long long`. The type for floating-point
|
||||
numbers is used in case of overflow.
|
||||
- The types for signed and unsigned integers must be distinct, see
|
||||
[#2573](https://github.com/nlohmann/json/issues/2573).
|
||||
- Only `#!c double`, `#!c float`, and `#!c long double` are supported for floating-point numbers.
|
||||
|
||||
!!! example
|
||||
|
||||
A `basic_json` type that uses `#!c long double` as floating-point type.
|
||||
|
||||
```cpp hl_lines="2"
|
||||
using json_ld = nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||
std::int64_t, std::uint64_t, long double>;
|
||||
```
|
||||
|
||||
Note values should then be parsed with `json_ld::parse` rather than `json::parse` as the latter would parse
|
||||
floating-point values to `#!c double` before then converting them to `#!c long double`.
|
||||
@@ -54,4 +54,4 @@ val=nan
|
||||
<span class=p>}</span>
|
||||
</code></pre></div> <p>Note this approach also has the advantage that it can react on non-numerical <abbr title="JavaScript Object Notation">JSON</abbr> value types such as strings.</p> <p>(Example taken from <a href="https://github.com/nlohmann/json/issues/777#issuecomment-459968458">#777</a>.)</p> </div> <h3 id=determine-number-types>Determine number types<a class=headerlink href=#determine-number-types title="Permanent link">¶</a></h3> <p>As the example in <a href=#number-conversion>Number conversion</a> shows, there are different functions to determine the type of the stored number:</p> <ul> <li><a href=../../../api/basic_json/is_number/ ><code>is_number()</code></a> returns <code class=highlight><span class=nb>true</span></code> for any number type</li> <li><a href=../../../api/basic_json/is_number_integer/ ><code>is_number_integer()</code></a> returns <code class=highlight><span class=nb>true</span></code> for signed and unsigned integers</li> <li><a href=../../../api/basic_json/is_number_unsigned/ ><code>is_number_unsigned()</code></a> returns <code class=highlight><span class=nb>true</span></code> for unsigned integers only</li> <li><a href=../../../api/basic_json/is_number_float/ ><code>is_number_float()</code></a> returns <code class=highlight><span class=nb>true</span></code> for floating-point numbers</li> <li><a href=../../../api/basic_json/type_name/ ><code>type_name()</code></a> returns <code class=highlight><span class=s>"number"</span></code> for any number type</li> <li><a href=../../../api/basic_json/type/ ><code>type()</code></a> returns a different enumerator of <a href=../../../api/basic_json/value_t/ ><code>value_t</code></a> for all number types</li> </ul> <table> <thead> <tr> <th>function</th> <th>unsigned integer</th> <th>signed integer</th> <th>floating-point</th> <th>string</th> </tr> </thead> <tbody> <tr> <td><a href=../../../api/basic_json/is_number/ ><code>is_number()</code></a></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/is_number_integer/ ><code>is_number_integer()</code></a></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/is_number_unsigned/ ><code>is_number_unsigned()</code></a></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/is_number_float/ ><code>is_number_float()</code></a></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> <td><code class=highlight><span class=nb>true</span></code></td> <td><code class=highlight><span class=nb>false</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/type_name/ ><code>type_name()</code></a></td> <td><code class=highlight><span class=s>"number"</span></code></td> <td><code class=highlight><span class=s>"number"</span></code></td> <td><code class=highlight><span class=s>"number"</span></code></td> <td><code class=highlight><span class=s>"string"</span></code></td> </tr> <tr> <td><a href=../../../api/basic_json/type/ ><code>type()</code></a></td> <td><code>number_unsigned</code></td> <td><code>number_integer</code></td> <td><code>number_float</code></td> <td><code>string</code></td> </tr> </tbody> </table> <h3 id=template-number-types>Template number types<a class=headerlink href=#template-number-types title="Permanent link">¶</a></h3> <p>The number types can be changed with template parameters.</p> <table> <thead> <tr> <th>position</th> <th>number type</th> <th>default type</th> <th>possible values</th> </tr> </thead> <tbody> <tr> <td>5</td> <td>signed integers</td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int64_t</span></code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int32_t</span></code>, <code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>int16_t</span></code>, etc.</td> </tr> <tr> <td>6</td> <td>unsigned integers</td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint64_t</span></code></td> <td><code class=highlight><span class=n>std</span><span class=o>::</span><span class=kt>uint32_t</span></code>, <code class=highliLine truncated
|
||||
<span class=hll><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=kt>int64_t</span><span class=p>,</span><span class=w> </span><span class=n>std</span><span class=o>::</span><span class=kt>uint64_t</span><span class=p>,</span><span class=w> </span><span class=kt>long</span><span class=w> </span><span class=kt>double</span><span class=o>></span><span class=p>;</span>
|
||||
</span></code></pre></div> <p>Note values should then be parsed with <code>json_ld::parse</code> rather than <code>json::parse</code> as the latter would parse floating-point values to <code class=highlight><span class=kt>double</span></code> before then converting them to <code class=highlight><span class=kt>long</span><span class=w> </span><span class=kt>double</span></code>.</p> </div> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="July 4, 2026 09:13:25 UTC">July 4, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: Line truncated
|
||||
</span></code></pre></div> <p>Note values should then be parsed with <code>json_ld::parse</code> rather than <code>json::parse</code> as the latter would parse floating-point values to <code class=highlight><span class=kt>double</span></code> before then converting them to <code class=highlight><span class=kt>long</span><span class=w> </span><span class=kt>double</span></code>.</p> </div> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="July 8, 2026 18:18:24 UTC">July 8, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright © 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3M135.4 416H69V202.2h66.5V416zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77m282.1 320h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9z"/></svg> </a> <a href="https://www.xing.com/profile/Niels_Lohmann" target="_blank" rel="noopener" title="www.xing.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 384 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: Line truncated
|
||||
@@ -0,0 +1,282 @@
|
||||
# Number Handling
|
||||
|
||||
This document describes how the library is handling numbers.
|
||||
|
||||
## Background
|
||||
|
||||
This section briefly summarizes how the JSON specification describes how numbers should be handled.
|
||||
|
||||
### JSON number syntax
|
||||
|
||||
JSON defines the syntax of numbers as follows:
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6
|
||||
|
||||
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.
|
||||
|
||||
A fraction part is a decimal point followed by one or more digits.
|
||||
|
||||
An exponent part begins with the letter E in uppercase or lowercase, which may be followed by a plus or minus sign. The E and optional sign are followed by one or more digits.
|
||||
|
||||
The following railroad diagram from [json.org](https://json.org) visualizes the number syntax:
|
||||
|
||||
### Number interoperability
|
||||
|
||||
On number interoperability, the following remarks are made:
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6
|
||||
|
||||
This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754 binary64 (double precision) numbers [IEEE754] is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision. A JSON number such as 1E400 or 3.141592653589793238462643383279 may indicate potential interoperability problems, since it suggests that the software that created it expects receiving software to have greater capabilities for numeric magnitude and precision than is widely available.
|
||||
|
||||
Note that when such software is used, numbers that are integers and are in the range [-2^{53}+1, 2^{53}-1] are interoperable in the sense that implementations will agree exactly on their numeric values.
|
||||
|
||||
## Library implementation
|
||||
|
||||
This section describes how this library implements the above number specification.
|
||||
|
||||
### Number storage
|
||||
|
||||
In the default [`json`](https://json.nlohmann.me/api/json/index.md) type, numbers are stored as `std::uint64_t`, `std::int64_t`, and `double`, respectively. Thereby, `std::uint64_t` and `std::int64_t` are used only if they can store the number without loss of precision. If this is impossible (e.g., if the number is too large), the number is stored as `double`.
|
||||
|
||||
Notes
|
||||
|
||||
- Numbers with a decimal digit or scientific notation are always stored as `double`.
|
||||
- The number types can be changed, see [Template number types](#template-number-types).
|
||||
- As of version 3.9.1, the conversion is realized by [`std::strtoull`](https://en.cppreference.com/w/cpp/string/byte/strtoul), [`std::strtoll`](https://en.cppreference.com/w/cpp/string/byte/strtol), and [`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof), respectively.
|
||||
|
||||
Examples
|
||||
|
||||
- Integer `-12345678912345789123456789` is smaller than `INT64_MIN` and will be stored as floating-point number `-1.2345678912345788e+25`.
|
||||
- Integer `1E3` will be stored as floating-point number `1000.0`.
|
||||
|
||||
### Number limits
|
||||
|
||||
- Any 64-bit signed or unsigned integer can be stored without loss of precision.
|
||||
- Numbers exceeding the limits of `double` (i.e., numbers that after conversion via [`std::strtod`](https://en.cppreference.com/w/cpp/string/byte/strtof) are not satisfying [`std::isfinite`](https://en.cppreference.com/w/cpp/numeric/math/isfinite) such as `1E400`) will throw exception [`json.exception.out_of_range.406`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range406) during parsing.
|
||||
- Floating-point numbers are rounded to the next number representable as `double`. For instance `3.141592653589793238462643383279` is stored as [`0x400921fb54442d18`](https://float.exposed/0x400921fb54442d18). This is the same behavior as the code `double x = 3.141592653589793238462643383279;`.
|
||||
|
||||
Interoperability
|
||||
|
||||
- The library is interoperable with respect to the specification, because its supported range [-2^{63}, 2^{64}-1] is larger than the described range [-2^{53}+1, 2^{53}-1].
|
||||
- All integers outside the range [-2^{63}, 2^{64}-1], as well as floating-point numbers are stored as `double`. This also concurs with the specification above.
|
||||
|
||||
### Zeros
|
||||
|
||||
The JSON number grammar allows for different ways to express zero, and this library will store zeros differently:
|
||||
|
||||
| Literal | Stored value and type | Serialization |
|
||||
| ------- | --------------------- | ------------- |
|
||||
| `0` | `std::uint64_t(0)` | `0` |
|
||||
| `-0` | `std::int64_t(0)` | `0` |
|
||||
| `0.0` | `double(0.0)` | `0.0` |
|
||||
| `-0.0` | `double(-0.0)` | `-0.0` |
|
||||
| `0E0` | `double(0.0)` | `0.0` |
|
||||
| `-0E0` | `double(-0.0)` | `-0.0` |
|
||||
|
||||
That is, `-0` is stored as a signed integer, but the serialization does not reproduce the `-`.
|
||||
|
||||
### Number serialization
|
||||
|
||||
- Integer numbers are serialized as is; that is, no scientific notation is used.
|
||||
- Floating-point numbers are serialized as specified by the `%g` printf modifier with [`std::numeric_limits<double>::max_digits10`](https://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10) significant digits. The rationale is to use the shortest representation while still allowing round-tripping.
|
||||
|
||||
Notes regarding precision of floating-point numbers
|
||||
|
||||
As described above, floating-point numbers are rounded to the nearest double and serialized with the shortest representation to allow round-tripping. This can yield confusing examples:
|
||||
|
||||
- The serialization can have fewer decimal places than the input: `2555.5599999999999` will be serialized as `2555.56`. The reverse can also be true.
|
||||
|
||||
- The serialization can be in scientific notation even if the input is not: `0.0000972439793401814` will be serialized as `9.72439793401814e-05`. The reverse can also be true: `12345E-5` will be serialized as `0.12345`.
|
||||
|
||||
- Conversions from `float` to `double` can also introduce rounding errors:
|
||||
|
||||
```
|
||||
float f = 0.3;
|
||||
json j = f;
|
||||
std::cout << j << '\n';
|
||||
```
|
||||
|
||||
yields `0.30000001192092896`.
|
||||
|
||||
All examples here can be reproduced by passing the original double value to
|
||||
|
||||
```
|
||||
std::printf("%.*g\n", std::numeric_limits<double>::max_digits10, double_value);
|
||||
```
|
||||
|
||||
#### NaN handling
|
||||
|
||||
NaN (not-a-number) cannot be expressed with the number syntax described above and are in fact explicitly excluded:
|
||||
|
||||
[RFC 8259](https://tools.ietf.org/html/rfc8259#section-6), Section 6
|
||||
|
||||
Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.
|
||||
|
||||
That is, there is no way to *parse* a NaN value. However, assignments can store NaN values in a JSON value.
|
||||
|
||||
This library serializes NaN values as `null`. This corresponds to the behavior of JavaScript's [`JSON.stringify`](https://www.w3schools.com/js/js_json_stringify.asp) function.
|
||||
|
||||
Example
|
||||
|
||||
The following example shows how a NaN value is stored in a `json` value.
|
||||
|
||||
```
|
||||
int main()
|
||||
{
|
||||
double val = std::numeric_limits<double>::quiet_NaN();
|
||||
std::cout << "val=" << val << std::endl;
|
||||
json j = val;
|
||||
std::cout << "j=" << j.dump() << std::endl;
|
||||
val = j;
|
||||
std::cout << "val=" << val << std::endl;
|
||||
}
|
||||
```
|
||||
|
||||
output:
|
||||
|
||||
```
|
||||
val=nan
|
||||
j=null
|
||||
val=nan
|
||||
```
|
||||
|
||||
### Number comparison
|
||||
|
||||
Floating-point numbers inside JSON values are compared with `json::number_float_t::operator==` which is `double::operator==` by default.
|
||||
|
||||
Alternative comparison functions
|
||||
|
||||
To compare floating-point while respecting an epsilon, an alternative [comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39) could be used, for instance
|
||||
|
||||
```
|
||||
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
|
||||
inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
|
||||
{
|
||||
return std::abs(a - b) <= epsilon;
|
||||
}
|
||||
```
|
||||
|
||||
Or you can self-define an operator equal function like this:
|
||||
|
||||
```
|
||||
bool my_equal(const_reference lhs, const_reference rhs)
|
||||
{
|
||||
const auto lhs_type = lhs.type();
|
||||
const auto rhs_type = rhs.type();
|
||||
if (lhs_type == rhs_type)
|
||||
{
|
||||
switch(lhs_type)
|
||||
{
|
||||
// self_defined case
|
||||
case value_t::number_float:
|
||||
return std::abs(lhs - rhs) <= std::numeric_limits<float>::epsilon();
|
||||
|
||||
// other cases remain the same with the original
|
||||
...
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
(see [#703](https://github.com/nlohmann/json/issues/703) for more information.)
|
||||
|
||||
Note
|
||||
|
||||
NaN values never compare equal to themselves or to other NaN values. See [#514](https://github.com/nlohmann/json/issues/514).
|
||||
|
||||
### Number conversion
|
||||
|
||||
Just like the C++ language itself, the `get` family of functions allows conversions between unsigned and signed integers, and between integers and floating-point values. This behavior may be surprising.
|
||||
|
||||
Unconditional number conversions
|
||||
|
||||
```
|
||||
double d = 42.3; // non-integer double value 42.3
|
||||
json jd = d; // stores double value 42.3
|
||||
std::int64_t i = jd.get<std::int64_t>(); // now i==42; no warning or error is produced
|
||||
```
|
||||
|
||||
Note the last line with throw a [`json.exception.type_error.302`](https://json.nlohmann.me/home/exceptions/#jsonexceptiontype_error302) exception if `jd` is not a numerical type, for instance a string.
|
||||
|
||||
Numeric conversions are performed according to the corresponding C++ conversion rules. The library does not perform range checks when converting between numeric types.
|
||||
|
||||
In particular, conversions from floating-point values to integer types, or conversions to integer types with a smaller range than the stored value, may produce implementation-defined or undefined behavior if the source value cannot be represented by the target type.
|
||||
|
||||
Applications requiring checked conversions should inspect the stored number type with [`is_number_float()`](https://json.nlohmann.me/api/basic_json/is_number_float/index.md), [`is_number_integer()`](https://json.nlohmann.me/api/basic_json/is_number_integer/index.md), [`is_number_unsigned()`](https://json.nlohmann.me/api/basic_json/is_number_unsigned/index.md), or [`type()`](https://json.nlohmann.me/api/basic_json/type/index.md), and perform explicit range checks before converting to a narrower type.
|
||||
|
||||
The rationale is twofold:
|
||||
|
||||
1. JSON does not define a number type or precision (see above).
|
||||
1. C++ also allows silently converting between number types.
|
||||
|
||||
Conditional number conversion
|
||||
|
||||
The code above can be solved by explicitly checking the nature of the value with members such as [`is_number_integer()`](https://json.nlohmann.me/api/basic_json/is_number_integer/index.md) or [`is_number_unsigned()`](https://json.nlohmann.me/api/basic_json/is_number_unsigned/index.md):
|
||||
|
||||
```
|
||||
// check if jd is really integer-valued
|
||||
if (jd.is_number_integer())
|
||||
{
|
||||
// if so, do the conversion and use i
|
||||
std::int64_t i = jd.get<std::int64_t>();
|
||||
// ...
|
||||
}
|
||||
else
|
||||
{
|
||||
// otherwise, take appropriate action
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Note this approach also has the advantage that it can react on non-numerical JSON value types such as strings.
|
||||
|
||||
(Example taken from [#777](https://github.com/nlohmann/json/issues/777#issuecomment-459968458).)
|
||||
|
||||
### Determine number types
|
||||
|
||||
As the example in [Number conversion](#number-conversion) shows, there are different functions to determine the type of the stored number:
|
||||
|
||||
- [`is_number()`](https://json.nlohmann.me/api/basic_json/is_number/index.md) returns `true` for any number type
|
||||
- [`is_number_integer()`](https://json.nlohmann.me/api/basic_json/is_number_integer/index.md) returns `true` for signed and unsigned integers
|
||||
- [`is_number_unsigned()`](https://json.nlohmann.me/api/basic_json/is_number_unsigned/index.md) returns `true` for unsigned integers only
|
||||
- [`is_number_float()`](https://json.nlohmann.me/api/basic_json/is_number_float/index.md) returns `true` for floating-point numbers
|
||||
- [`type_name()`](https://json.nlohmann.me/api/basic_json/type_name/index.md) returns `"number"` for any number type
|
||||
- [`type()`](https://json.nlohmann.me/api/basic_json/type/index.md) returns a different enumerator of [`value_t`](https://json.nlohmann.me/api/basic_json/value_t/index.md) for all number types
|
||||
|
||||
| function | unsigned integer | signed integer | floating-point | string |
|
||||
| --------------------------------------------------------------------------------------------- | ----------------- | ---------------- | -------------- | ---------- |
|
||||
| [`is_number()`](https://json.nlohmann.me/api/basic_json/is_number/index.md) | `true` | `true` | `true` | `false` |
|
||||
| [`is_number_integer()`](https://json.nlohmann.me/api/basic_json/is_number_integer/index.md) | `true` | `true` | `false` | `false` |
|
||||
| [`is_number_unsigned()`](https://json.nlohmann.me/api/basic_json/is_number_unsigned/index.md) | `true` | `false` | `false` | `false` |
|
||||
| [`is_number_float()`](https://json.nlohmann.me/api/basic_json/is_number_float/index.md) | `false` | `false` | `true` | `false` |
|
||||
| [`type_name()`](https://json.nlohmann.me/api/basic_json/type_name/index.md) | `"number"` | `"number"` | `"number"` | `"string"` |
|
||||
| [`type()`](https://json.nlohmann.me/api/basic_json/type/index.md) | `number_unsigned` | `number_integer` | `number_float` | `string` |
|
||||
|
||||
### Template number types
|
||||
|
||||
The number types can be changed with template parameters.
|
||||
|
||||
| position | number type | default type | possible values |
|
||||
| -------- | ----------------- | --------------- | -------------------------------------- |
|
||||
| 5 | signed integers | `std::int64_t` | `std::int32_t`, `std::int16_t`, etc. |
|
||||
| 6 | unsigned integers | `std::uint64_t` | `std::uint32_t`, `std::uint16_t`, etc. |
|
||||
| 7 | floating-point | `double` | `float`, `long double` |
|
||||
|
||||
Constraints on number types
|
||||
|
||||
- The type for signed integers must be convertible from `long long`. The type for floating-point numbers is used in case of overflow.
|
||||
- The type for unsigned integers must be convertible from `unsigned long long`. The type for floating-point numbers is used in case of overflow.
|
||||
- The types for signed and unsigned integers must be distinct, see [#2573](https://github.com/nlohmann/json/issues/2573).
|
||||
- Only `double`, `float`, and `long double` are supported for floating-point numbers.
|
||||
|
||||
Example
|
||||
|
||||
A `basic_json` type that uses `long double` as floating-point type.
|
||||
|
||||
```
|
||||
using json_ld = nlohmann::basic_json<std::map, std::vector, std::string, bool,
|
||||
std::int64_t, std::uint64_t, long double>;
|
||||
```
|
||||
|
||||
Note values should then be parsed with `json_ld::parse` rather than `json::parse` as the latter would parse floating-point values to `double` before then converting them to `long double`.
|
||||
Reference in new issue
Block a user