summaryrefslogtreecommitdiffhomepage
path: root/serialize/magnum-vector.hpp
blob: 61e127b12e25403f5f46df03f598d11997820f6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>
#include <Magnum/Math/Vector4.h>
#include <Magnum/Math/Color.h>
#include <nlohmann/json.hpp>

namespace nlohmann {

template<std::size_t N, typename T>
struct adl_serializer<Magnum::Math::Vector<N, T>> {
    using vec = Magnum::Math::Vector<N, T>;
    static void to_json(json& j, const vec& val);
    static void from_json(const json& j, vec& val);
};

template <std::size_t N, typename T>
void adl_serializer<Magnum::Math::Vector<N, T>>::to_json(json& j, const vec& val)
{
    std::array<T, N> array{};
    for (std::size_t i = 0; i < N; i++)
        array[i] = val[i];
    using nlohmann::to_json;
    to_json(j, array);
}

template <std::size_t N, typename T>
void adl_serializer<Magnum::Math::Vector<N, T>>::from_json(const json& j, vec& val)
{
    std::array<T, N> array{};
    using nlohmann::from_json;
    from_json(j, array);
    for (std::size_t i = 0; i < N; i++)
        val[i] = array[i];
}

template<typename T> struct adl_serializer<Magnum::Math::Vector3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {};
template<typename T> struct adl_serializer<Magnum::Math::Vector4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {};
template<typename T> struct adl_serializer<Magnum::Math::Color3<T>>  : adl_serializer<Magnum::Math::Vector<3, T>> {};
template<typename T> struct adl_serializer<Magnum::Math::Color4<T>>  : adl_serializer<Magnum::Math::Vector<4, T>> {};
template<typename T> struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {};

} // namespace nlohmann