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
time_json.hpp
Go to the documentation of this file.
1//
2// time_json.hpp
3// core-adapters
4//
5// Copyright 2010-2026 Saxon Herschel Nicholls
6//
7
49
50#ifndef snicholls_core_adapters_nlohmann_time_json_hpp
51#define snicholls_core_adapters_nlohmann_time_json_hpp
52
53#include <string>
54#include <system_error>
55
57#include <snicholls/core/time/rfc3339.hpp>
58#include <snicholls/core/time/utc.hpp>
59
60namespace snicholls {
61
68template <utc_duration D>
69void to_json(nlohmann::json& j, const utc<D>& t)
70{
71 const rfc3339_text text = to_text(t);
72 if (!text) [[unlikely]]
73 throw json_error("snicholls::utc to_json: a year outside 0000..9999 has no RFC 3339 form");
74 j = std::string(text.view());
75}
76
83template <utc_duration D>
84void from_json(const nlohmann::json& j, utc<D>& t)
85{
86 const std::string& s = detail::json_string_or_throw(j, "snicholls::utc", "an RFC 3339 string");
87 const parse_result<utc<D>> r = parse_utc<D>(s);
88 if (!r) [[unlikely]]
89 detail::throw_json_error("snicholls::utc",
90 r.error() == std::errc::result_out_of_range ? "out of range or too precise:"
91 : "not RFC 3339:",
92 s);
93 t = *r;
94}
95
96} // namespace snicholls
97
98#endif /* snicholls_core_adapters_nlohmann_time_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.
A JSON value that does not convert to or from a core type.
Definition json.hpp:76