MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ from_json()

template<typename T >
static void MayaFlux::IO::JSONSerializer::from_json ( const nlohmann::json &  j,
T out 
)
inlinestaticprivate

Definition at line 221 of file JSONSerializer.hpp.

222 {
223 if constexpr (Reflect::Reflectable<T>) {
224 if (!j.is_object()) {
225 throw nlohmann::json::type_error::create(302, "expected object for Reflectable type", &j);
226 }
227 std::apply(
228 [&](const auto&... props) {
229 (decode_property(j, out, props), ...);
230 },
231 T::describe());
232 } else if constexpr (Reflect::is_optional_v<T>) {
233 using Inner = typename Reflect::is_optional<T>::inner;
234 if (j.is_null()) {
235 out = std::nullopt;
236 } else {
237 Inner inner {};
238 from_json(j, inner);
239 out = std::move(inner);
240 }
241 } else if constexpr (Reflect::is_vector_v<T>) {
242 using V = typename Reflect::is_vector<T>::element;
243 if (!j.is_array())
244 throw nlohmann::json::type_error::create(302, "expected array", &j);
245 out.clear();
246 out.reserve(j.size());
247 for (const auto& item : j) {
248 V element {};
249 from_json(item, element);
250 out.push_back(std::move(element));
251 }
252
253 } else if constexpr (Reflect::is_string_map_v<T>) {
254 using V = typename Reflect::is_string_map<T>::element;
255 if (!j.is_object())
256 throw nlohmann::json::type_error::create(302, "expected object for map", &j);
257 out.clear();
258 for (const auto& [k, v] : j.items()) {
259 V val {};
260 from_json(v, val);
261 out.emplace(k, std::move(val));
262 }
263 } else if constexpr (std::is_enum_v<T>) {
264 out = static_cast<T>(j.get<std::underlying_type_t<T>>());
265
266 } else if constexpr (std::is_same_v<T, nlohmann::json>) {
267 out = j;
268
269 } else {
271 }
272 }
float k
static void from_json(const nlohmann::json &j, T &out)
static void decode_property(const nlohmann::json &j, Class &obj, const Reflect::Property< Class, T > &prop)
static void from_json(const nlohmann::json &j, T &out)

References k, MayaFlux::IO::T, and MayaFlux::IO::V.