MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Serializer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <nlohmann/json.hpp>
4
5namespace MayaFlux::IO {
6
7/**
8 * @struct Serializer
9 * @brief Extension point for JSONSerializer.
10 *
11 * Specialize this struct for any type that JSONSerializer does not
12 * handle natively. The specialization must provide:
13 * static nlohmann::json to_json(const T&);
14 * static void from_json(const nlohmann::json&, T&);
15 *
16 * Example — adding support for a custom Vec3 type:
17 * @code
18 * template <>
19 * struct Serializer<MyVec3> {
20 * static nlohmann::json to_json(const MyVec3& v) {
21 * return nlohmann::json::array({ v.x, v.y, v.z });
22 * }
23 * static void from_json(const nlohmann::json& j, MyVec3& out) {
24 * out = { j[0], j[1], j[2] };
25 * }
26 * };
27 * @endcode
28 */
29template <typename T>
30struct Serializer {
31 static nlohmann::json to_json(const T& val) { return val; }
32 static void from_json(const nlohmann::json& j, T& out) { out = j.get<T>(); }
33};
34
35} // namespace MayaFlux::IO
static void from_json(const nlohmann::json &j, T &out)
static nlohmann::json to_json(const T &val)
Extension point for JSONSerializer.