MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ to_json()

template<typename T >
static nlohmann::json MayaFlux::IO::JSONSerializer::to_json ( const T &  val)
inlinestaticprivate

Extension point — specialize Serializer<T> to handle custom types

Definition at line 161 of file JSONSerializer.hpp.

162 {
163 if constexpr (Reflect::Reflectable<T>) {
164 nlohmann::json j = nlohmann::json::object();
165 std::apply(
166 [&](const auto&... props) {
167 (encode_property(j, val, props), ...);
168 },
169 T::describe());
170 return j;
171 } else if constexpr (Reflect::is_optional_v<T>) {
172 if (!val.has_value()) {
173 return nullptr;
174 }
175 return to_json(*val);
176 } else if constexpr (Reflect::is_vector_v<T>) {
177 auto arr = nlohmann::json::array();
178 for (const auto& item : val) {
179 arr.push_back(to_json(item));
180 }
181 return arr;
182 } else if constexpr (Reflect::is_string_map_v<T>) {
183 nlohmann::json j = nlohmann::json::object();
184 for (const auto& [k, v] : val) {
185 j[k] = to_json(v);
186 }
187 return j;
188 } else if constexpr (std::is_enum_v<T>) {
189 return static_cast<std::underlying_type_t<T>>(val);
190 } else if constexpr (std::is_same_v<T, nlohmann::json>) {
191 return val;
192 } else {
193 /// Extension point — specialize Serializer<T> to handle custom types
194 return Serializer<T>::to_json(val);
195 }
196 }
static void encode_property(nlohmann::json &j, const Class &obj, const Reflect::Property< Class, T > &prop)
static nlohmann::json to_json(const T &val)
static nlohmann::json to_json(const T &val)