|
core-adapters 0.1.2
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
|
nlohmann/json hooks for snicholls::decimal: its exact canonical text, as a JSON string.
More...
#include <optional>#include <string>#include <snicholls/core_adapters/nlohmann/json.hpp>#include <snicholls/core/decimal.hpp>Go to the source code of this file.
Functions | |
| void | snicholls::to_json (nlohmann::json &j, const decimal &d) |
Writes d as its canonical text, a JSON string. | |
| void | snicholls::from_json (const nlohmann::json &j, decimal &d) |
| Reads canonical decimal text from a JSON string. | |
nlohmann/json hooks for snicholls::decimal: its exact canonical text, as a JSON string.
nlohmann::json j = d; writes "72.5", "0.0000001", "-5.25": decimal::text(), the one canonical spelling. It is a JSON string, not a number, because a JSON number is read as a double by most consumers (JavaScript among them) and 0.1 has no exact double. j.get<snicholls::decimal>() reads the canonical text only (decimal::parse with decimal_syntax::canonical); a JSON number is refused rather than guessed at.snicholls::decimal::text, snicholls::decimal::parse (decimal.hpp); snicholls::json_error (json.hpp).double).to_json throws only std::bad_alloc.snicholls::json_error: not a string; not canonical decimal text ("72.50", "+1", "1e5" are refused); more than 18 significant digits or an exponent outside [-18, 18].decimal::text / decimal::parse plus one std::string.snicholls::core_json.nlohmann::json::parse with a callback, or a SAX handler) and decimal::parse it with decimal_syntax::plain.Definition in file decimal_json.hpp.