mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-09-03 23:37:26 +00:00
chore: change std optional json serialization to not include property if unset
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Json/JsonExtension.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
class JsonVec2
|
||||
@@ -9,7 +10,7 @@ public:
|
||||
float y;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(JsonVec2, x, y);
|
||||
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonVec2, x, y);
|
||||
|
||||
class JsonVec3
|
||||
{
|
||||
@@ -19,7 +20,7 @@ public:
|
||||
float z;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(JsonVec3, x, y, z);
|
||||
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonVec3, x, y, z);
|
||||
|
||||
class JsonVec4
|
||||
{
|
||||
@@ -30,4 +31,4 @@ public:
|
||||
float w;
|
||||
};
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(JsonVec4, x, y, z, w);
|
||||
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonVec4, x, y, z, w);
|
||||
|
58
src/ObjCommon/Json/JsonExtension.h
Normal file
58
src/ObjCommon/Json/JsonExtension.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
// Credits to
|
||||
// https://www.kdab.com/jsonify-with-nlohmann-json/
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <optional>
|
||||
|
||||
// partial specialization (full specialization works too)
|
||||
namespace nlohmann
|
||||
{
|
||||
template<class T> void optional_to_json(nlohmann::json& j, const char* name, const std::optional<T>& value)
|
||||
{
|
||||
if (value)
|
||||
j[name] = *value;
|
||||
}
|
||||
|
||||
template<class T> void optional_from_json(const nlohmann::json& j, const char* name, std::optional<T>& value)
|
||||
{
|
||||
const auto it = j.find(name);
|
||||
if (it != j.end())
|
||||
value = it->get<T>();
|
||||
else
|
||||
value = std::nullopt;
|
||||
}
|
||||
|
||||
template<typename> constexpr bool is_optional = false;
|
||||
template<typename T> constexpr bool is_optional<std::optional<T>> = true;
|
||||
|
||||
template<typename T> void extended_to_json(const char* key, nlohmann::json& j, const T& value)
|
||||
{
|
||||
if constexpr (is_optional<T>)
|
||||
nlohmann::optional_to_json(j, key, value);
|
||||
else
|
||||
j[key] = value;
|
||||
}
|
||||
|
||||
template<typename T> void extended_from_json(const char* key, const nlohmann::json& j, T& value)
|
||||
{
|
||||
if constexpr (is_optional<T>)
|
||||
nlohmann::optional_from_json(j, key, value);
|
||||
else
|
||||
j.at(key).get_to(value);
|
||||
}
|
||||
} // namespace nlohmann
|
||||
|
||||
#define EXTEND_JSON_TO(v1) extended_to_json(#v1, nlohmann_json_j, nlohmann_json_t.v1);
|
||||
#define EXTEND_JSON_FROM(v1) extended_from_json(#v1, nlohmann_json_j, nlohmann_json_t.v1);
|
||||
|
||||
#define NLOHMANN_DEFINE_TYPE_EXTENSION(Type, ...) \
|
||||
inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) \
|
||||
{ \
|
||||
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_TO, __VA_ARGS__)) \
|
||||
} \
|
||||
inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) \
|
||||
{ \
|
||||
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_FROM, __VA_ARGS__)) \
|
||||
}
|
@@ -1,37 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <optional>
|
||||
|
||||
// partial specialization (full specialization works too)
|
||||
namespace nlohmann
|
||||
{
|
||||
template<typename T> struct adl_serializer<std::optional<T>>
|
||||
{
|
||||
static void to_json(json& j, const std::optional<T>& opt)
|
||||
{
|
||||
if (!opt.has_value())
|
||||
{
|
||||
j = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
j = *opt; // this will call adl_serializer<T>::to_json which will
|
||||
// find the free function to_json in T's namespace!
|
||||
}
|
||||
}
|
||||
|
||||
static void from_json(const json& j, std::optional<T>& opt)
|
||||
{
|
||||
if (j.is_null())
|
||||
{
|
||||
opt = std::nullopt;
|
||||
}
|
||||
else
|
||||
{
|
||||
opt = j.template get<T>(); // same as above, but with
|
||||
// adl_serializer<T>::from_json
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace nlohmann
|
Reference in New Issue
Block a user