#ifndef DESTRUM_GLMJSON_H #define DESTRUM_GLMJSON_H #include #include #include #include #include namespace destrum::json_detail { inline void RequireNumberArray(const nlohmann::json& j, std::size_t size) { if (!j.is_array() || j.size() != size) { throw std::runtime_error( "expected an array of " + std::to_string(size) + " numbers, got " + j.dump()); } for (const auto& element : j) { if (!element.is_number()) { throw std::runtime_error( "expected an array of " + std::to_string(size) + " numbers, got " + j.dump()); } } } } namespace nlohmann { template struct adl_serializer> { static void to_json(json& j, const glm::vec& value) { j = json::array(); for (glm::length_t i = 0; i < L; ++i) { j.push_back(value[i]); } } static void from_json(const json& j, glm::vec& value) { destrum::json_detail::RequireNumberArray(j, static_cast(L)); for (glm::length_t i = 0; i < L; ++i) { value[i] = j.at(static_cast(i)).get(); } } }; // Stored as [x, y, z, w], the same order as scene transforms template struct adl_serializer> { static void to_json(json& j, const glm::qua& value) { j = json::array({value.x, value.y, value.z, value.w}); } static void from_json(const json& j, glm::qua& value) { destrum::json_detail::RequireNumberArray(j, 4); value = glm::qua{ j.at(3).get(), j.at(0).get(), j.at(1).get(), j.at(2).get() }; } }; } #endif // DESTRUM_GLMJSON_H