mirror of
https://github.com/nlohmann/json.git
synced 2026-10-21 21:48:14 +00:00
3.4 KiB
3.4 KiB
nlohmann::basic_json_view::number_format
enum class number_format {
shortest,
source
};
This enumeration is used in dump to choose how numbers are written. Two values are differentiated:
- shortest
- integers are copied from the source text -- already canonical in JSON -- except that
#!cpp -0becomes#!cpp 0, the wayBasicJsonType::parse()reads it; floats are written with the library's shortest round-trip conversion, exactly asBasicJsonType::dump()would (e.g.#!cpp 1.5,#!cpp 100.0,#!cpp 1e+100) - source
- every number is copied exactly as it appears in the source text --
#!cpp 1.50,#!cpp 1E2,#!cpp -0, all digits of an integer literal with more digits than any number type holds -- somethingBasicJsonTypecannot do, since parsing already reduces every number to its parsed value
Examples
??? example
The example below writes back a price list received from a supplier: with `number_format::shortest` (the
default), a trailing zero and scientific notation are normalized away and a long account number that overflows
every number type is rounded, the same way `#!cpp materialize().dump()` (or `basic_json::dump()`) would;
`number_format::source` keeps every number exactly as it was written in the source text instead.
```cpp
#include <iostream>
#include <nlohmann/json_view.hpp>
using json_document = nlohmann::json_document;
using json_view = nlohmann::json_view;
int main()
{
// a price list received from a supplier feed -- prices and account
// numbers must be forwarded exactly, e.g. into an invoice
const json_document doc = json_document::parse(R"(
[{"sku": "A1", "price": 19.90, "account_id": 12345678901234567890123456},
{"sku": "A2", "price": 1E2, "account_id": 98765432109876543210987654}]
)");
const json_view list = doc.root();
// number_format::shortest (the default) writes numbers the way
// basic_json::dump() would: "19.90" becomes "19.9", "1E2" becomes
// "100.0", and each account number -- far beyond any 64-bit integer --
// is rounded to the nearest double, exactly as materialize().dump()
// (or a plain nlohmann::json) would round it
std::cout << list.dump() << '\n';
// number_format::source copies every number exactly as it was written
// in the source text instead -- something basic_json cannot do at all,
// since parsing already reduces every number to its parsed value
std::cout << list.dump(-1, ' ', false, json_view::number_format::source) << '\n';
}
```
Output:
```json
[{"sku":"A1","price":19.9,"account_id":1.2345678901234568e+25},{"sku":"A2","price":100.0,"account_id":9.876543210987655e+25}]
[{"sku":"A1","price":19.90,"account_id":12345678901234567890123456},{"sku":"A2","price":1E2,"account_id":98765432109876543210987654}]
```
See also
- dump - serialize to a JSON-formatted string
- number_token - get a single number's token text without dumping the whole value
BasicJsonType::error_handler_t- the analogous enumeration forBasicJsonType::dump's decoding-error behavior
Version history
- Added in version 3.13.0.