core-adapters 0.1.3
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
Loading...
Searching...
No Matches
decimal_json.hpp
Go to the documentation of this file.
1//
2// decimal_json.hpp
3// core-adapters
4//
5// Copyright 2010-2026 Saxon Herschel Nicholls
6//
7
48
49#ifndef snicholls_core_adapters_nlohmann_decimal_json_hpp
50#define snicholls_core_adapters_nlohmann_decimal_json_hpp
51
52#include <optional>
53#include <string>
54
56#include <snicholls/core/decimal.hpp>
57
58namespace snicholls {
59
65inline void to_json(nlohmann::json& j, const decimal& d) { j = std::string(d.text().view()); }
66
72inline void from_json(const nlohmann::json& j, decimal& d)
73{
74 const std::string& s = detail::json_string_or_throw(j, "snicholls::decimal", "a decimal string");
75 const std::optional<decimal> r = decimal::parse(s);
76 if (!r) [[unlikely]]
77 detail::throw_json_error("snicholls::decimal", "not canonical decimal text in range:", s);
78 d = *r;
79}
80
81} // namespace snicholls
82
83#endif /* snicholls_core_adapters_nlohmann_decimal_json_hpp */
void from_json(const nlohmann::json &j, decimal &d)
Reads canonical decimal text from a JSON string.
void to_json(nlohmann::json &j, const decimal &d)
Writes d as its canonical text, a JSON string.
snicholls::json_error and the nlohmann/json adapter layer: shared by every adapter.