MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GlmSerializer.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7template <typename T>
8concept GlmSerializable = GlmType<T>;
9
10}
11
12namespace MayaFlux::IO {
13
14/**
15 * @brief JSONSerializer extension for GLM vector and matrix types.
16 *
17 * Encodes GLM types as flat JSON arrays of their scalar components.
18 * Include this header in any translation unit that serializes GLM types
19 * via JSONSerializer — the specialization is found automatically.
20 *
21 * Supported types: vec2, vec3, vec4, dvec2, dvec3, dvec4,
22 * mat2, mat3, mat4, dmat2, dmat3, dmat4.
23 */
24
25template <typename T>
27struct Serializer<T> {
28
29 static nlohmann::json to_json(const T& v)
30 {
31 constexpr auto n = glm_component_count<T>();
32 using Comp = glm_component_type<T>;
33 auto arr = nlohmann::json::array();
34 const Comp* ptr = &v[0];
35 for (size_t i = 0; i < n; ++i)
36 arr.push_back(ptr[i]);
37 return arr;
38 }
39
40 static void from_json(const nlohmann::json& j, T& out)
41 {
42 constexpr auto n = glm_component_count<T>();
43 if (!j.is_array()) {
44 throw nlohmann::json::type_error::create(302, "expected array for glm type", &j);
45 }
46
47 if (j.size() != n) {
48 throw nlohmann::json::other_error::create(501,
49 "glm component count mismatch: expected "
50 + std::to_string(n) + ", got " + std::to_string(j.size()),
51 &j);
52 }
53
54 using Comp = glm_component_type<T>;
55 Comp* ptr = &out[0];
56 for (size_t i = 0; i < n; ++i)
57 ptr[i] = j[i].get<Comp>();
58 }
59};
60
61} // namespace MayaFlux::IO
const uint8_t * ptr
static void from_json(const nlohmann::json &j, T &out)
static nlohmann::json to_json(const T &v)
Extension point for JSONSerializer.