MayaFlux 0.4.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 207 of file JSONSerializer.hpp.

208 {
209 if constexpr (Reflectable<T>) {
210 if (!j.is_object()) {
211 auto e = nlohmann::json::type_error::create(
212 302, "expected object for Reflectable type", &j);
213 error<std::runtime_error>(
216 std::source_location::current(),
217 "JSON type error: {}", e.what());
218 }
219 std::apply(
220 [&](const auto&... props) {
221 (decode_property(j, out, props), ...);
222 },
223 T::describe());
224 } else if constexpr (is_optional_v<T>) {
225 using Inner = typename is_optional<T>::inner;
226 if (j.is_null()) {
227 out = std::nullopt;
228 } else {
229 Inner inner {};
230 from_json(j, inner);
231 out = std::move(inner);
232 }
233 } else if constexpr (is_vector_v<T>) {
234 using V = typename is_vector<T>::element;
235 if (!j.is_array()) {
236 auto e = nlohmann::json::type_error::create(302, "expected array", &j);
237 error<std::runtime_error>(
240 std::source_location::current(),
241 "JSON type error: {}", e.what());
242 }
243 out.clear();
244 out.reserve(j.size());
245 for (const auto& item : j) {
246 V element {};
247 from_json(item, element);
248 out.push_back(std::move(element));
249 }
250 } else if constexpr (is_string_map_v<T>) {
251 using V = typename is_string_map<T>::element;
252 if (!j.is_object()) {
253 auto e = nlohmann::json::type_error::create(302, "expected object for map", &j);
254 error<std::runtime_error>(
257 std::source_location::current(),
258 "JSON type error: {}", e.what());
259 }
260 out.clear();
261 for (const auto& [k, v] : j.items()) {
262 V val {};
263 from_json(v, val);
264 out.emplace(k, std::move(val));
265 }
266 } else if constexpr (GlmSerializable<T>) {
267 decode_glm(j, out);
268 } else if constexpr (std::is_enum_v<T>) {
269 out = static_cast<T>(j.get<std::underlying_type_t<T>>());
270 } else if constexpr (std::is_same_v<T, nlohmann::json>) {
271 out = j;
272 } else {
273 out = j.get<T>();
274 }
275 }
static void from_json(const nlohmann::json &j, T &out)
static void decode_glm(const nlohmann::json &j, T &out)
static void decode_property(const nlohmann::json &j, Class &obj, const Property< Class, T > &prop)
@ Runtime
General runtime operations (default fallback)
@ IO
Networking, file handling, streaming.

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