26 using json = ::nlohmann::json;
28 #include <NTL/vec_long.h>
29 #include <NTL/xdouble.h>
31 #include <helib/exceptions.h>
32 #include <helib/JsonWrapper.h>
33 #include <helib/version.h>
37 void to_json(json& j,
const NTL::xdouble& num);
38 void from_json(
const json& j, NTL::xdouble& num);
40 void to_json(json& j,
const NTL::ZZ& num);
41 void from_json(
const json& j, NTL::ZZ& num);
43 void to_json(json& j,
const NTL::Vec<long>& vec);
44 void from_json(
const json& j, NTL::Vec<long>& vec);
46 void to_json(json& j,
const NTL::ZZX& poly);
47 void from_json(
const json& j, NTL::ZZX& poly);
52 inline void to_json(json& j,
const std::complex<T>& num)
55 j = {num.real(), num.imag()};
62 inline void from_json(
const json& j, std::complex<T>& num)
73 throw helib::IOError(
"Bad complex JSON serialization. Expected a maximum "
74 "of 2 elements, recieved " +
75 std::to_string(j.size()));
80 num.real(j[0].get<T>());
82 num.imag(j[1].get<T>());
102 return std::any_cast<json>(jwrap.
getJSONobj());
103 }
catch (
const std::bad_any_cast& e) {
104 throw LogicError(std::string(
"Cannot unwrap wrapper. Bad cast ") +
109 template <
typename T>
115 for (
const auto& e : j) {
116 v.emplace_back(T::readFromJSON(
wrap(e)));
122 template <
typename T,
typename... TArgs>
124 const TArgs&... args)
129 for (
const auto& e : j) {
130 v.emplace_back(T::readFromJSON(
wrap(e), args...));
136 template <
typename T>
141 std::vector<json> jvec = j;
143 v.resize(jvec.size(), init);
145 for (std::size_t i = 0; i < jvec.size(); i++) {
146 v[i].readJSON(
wrap(jvec[i]));
150 template <
typename T>
153 std::vector<json> js;
154 for (
const auto& t : ts) {
155 js.emplace_back(
unwrap(t.writeToJSON()));
160 template <
typename T>
161 static inline json toTypedJson(
const json& tc)
163 return {{
"type", T::typeName},
169 template <
typename T>
170 static inline json fromTypedJson(
const json& j)
172 std::string obj_ser_ver = j.at(
"serializationVersion").get<std::string>();
174 std::stringstream sstr;
175 sstr <<
"Serialization version mismatch. Expected: "
177 throw IOError(sstr.str());
180 std::string obj_helib_ver = j.at(
"HElibVersion").get<std::string>();
182 std::stringstream sstr;
184 <<
" actual: " << obj_helib_ver;
185 throw IOError(sstr.str());
188 std::string obj_ty = j.at(
"type").get<std::string>();
189 if (obj_ty != T::typeName) {
190 std::stringstream fmt;
191 fmt <<
"Type mismatch deserializing json object."
192 <<
" Expected: " << T::typeName <<
" actual: " << obj_ty;
193 throw IOError(fmt.str());
195 return j.at(
"content");
198 template <
typename T,
typename TCALL>
203 }
catch (
const nlohmann::detail::exception& e) {
204 throw IOError(std::string(
"Error with JSON IO. ") + e.what());
Inherits from Exception and std::runtime_error.
Definition: exceptions.h:123
Inherits from Exception and std::logic_error.
Definition: exceptions.h:68
void to_json(json &j, const NTL::xdouble &num)
Definition: io.cpp:20
void from_json(const json &j, NTL::xdouble &num)
Definition: io.cpp:26
Definition: apiAttributes.h:21
std::vector< T > readVectorFromJSON(const json::array_t &j)
Definition: io.h:110
json unwrap(const JsonWrapper &jwrap)
Definition: io.h:99
T executeRedirectJsonError(const TCALL &f)
Definition: io.h:199
const std::string_view jsonSerializationVersion
Definition: io.h:92
JsonWrapper wrap(const json &j)
Definition: io.h:94
json writeVectorToJSON(const std::vector< T > &ts)
Definition: io.h:151
void to_json(json &j, const std::complex< T > &num)
Definition: io.h:52
void from_json(const json &j, std::complex< T > &num)
Definition: io.h:62
Definition: JsonWrapper.h:9
const std::any & getJSONobj() const
Definition: JsonWrapper.h:13
static constexpr auto asString
The string representation of this version of HElib.
Definition: version.in.h:44