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 203 of file JSONSerializer.hpp.

204 {
205 if constexpr (Reflectable<T>) {
206 if (!j.is_object()) {
207 throw nlohmann::json::type_error::create(
208 302, "expected object for Reflectable type", &j);
209 }
210 std::apply(
211 [&](const auto&... props) {
212 (decode_property(j, out, props), ...);
213 },
214 T::describe());
215 } else if constexpr (is_optional_v<T>) {
216 using Inner = typename is_optional<T>::inner;
217 if (j.is_null()) {
218 out = std::nullopt;
219 } else {
220 Inner inner {};
221 from_json(j, inner);
222 out = std::move(inner);
223 }
224 } else if constexpr (is_vector_v<T>) {
225 using V = typename is_vector<T>::element;
226 if (!j.is_array()) {
227 throw nlohmann::json::type_error::create(302, "expected array", &j);
228 }
229 out.clear();
230 out.reserve(j.size());
231 for (const auto& item : j) {
232 V element {};
233 from_json(item, element);
234 out.push_back(std::move(element));
235 }
236 } else if constexpr (is_string_map_v<T>) {
237 using V = typename is_string_map<T>::element;
238 if (!j.is_object()) {
239 throw nlohmann::json::type_error::create(302, "expected object for map", &j);
240 }
241 out.clear();
242 for (const auto& [k, v] : j.items()) {
243 V val {};
244 from_json(v, val);
245 out.emplace(k, std::move(val));
246 }
247 } else if constexpr (GlmSerializable<T>) {
248 decode_glm(j, out);
249 } else if constexpr (std::is_enum_v<T>) {
250 out = static_cast<T>(j.get<std::underlying_type_t<T>>());
251 } else {
252 out = j.get<T>();
253 }
254 }
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)

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