35inline nlohmann::json leafToJson(
bool v) {
return v; }
36inline nlohmann::json leafToJson(
int v) {
return v; }
37inline nlohmann::json leafToJson(uint32_t v) {
return v; }
38inline nlohmann::json leafToJson(
float v) {
return v; }
39inline nlohmann::json leafToJson(
const std::string& s) {
return s; }
40inline nlohmann::json leafToJson(
const glm::vec2& v) {
return detail::vec2ToJson(v); }
41inline nlohmann::json leafToJson(
const glm::vec3& v) {
return detail::vec3ToJson(v); }
42inline nlohmann::json leafToJson(
const glm::vec4& v) {
return detail::vec4ToJson(v); }
43inline nlohmann::json leafToJson(
const glm::quat& q) {
return detail::quatToJson(q); }
45inline void leafFromJson(
const nlohmann::json& j,
bool& v) { v = j.get<
bool>(); }
46inline void leafFromJson(
const nlohmann::json& j,
int& v) { v = j.get<
int>(); }
47inline void leafFromJson(
const nlohmann::json& j, uint32_t& v) { v = j.get<uint32_t>(); }
48inline void leafFromJson(
const nlohmann::json& j,
float& v) { v = j.get<
float>(); }
49inline void leafFromJson(
const nlohmann::json& j, std::string& s) { s = j.get<std::string>(); }
50inline void leafFromJson(
const nlohmann::json& j, glm::vec2& v) { v = detail::jsonToVec2(j, v); }
51inline void leafFromJson(
const nlohmann::json& j, glm::vec3& v) { v = detail::jsonToVec3(j, v); }
52inline void leafFromJson(
const nlohmann::json& j, glm::vec4& v) { v = detail::jsonToVec4(j, v); }
53inline void leafFromJson(
const nlohmann::json& j, glm::quat& q) { q = detail::jsonToQuat(j, q); }
56template<std::
size_t N>
57inline nlohmann::json leafToJson(
const char (&v)[N]) {
return std::string(v); }
58template<std::
size_t N>
59inline void leafFromJson(
const nlohmann::json& j,
char (&v)[N]) {
60 const std::string s = j.get<std::string>();
61 std::strncpy(v, s.c_str(), N - 1);
67template<
typename E,
typename = std::enable_if_t<std::is_enum_v<E>>>
68inline nlohmann::json leafToJson(E v) {
return Reflect::enumName(v); }
69template<
typename E,
typename = std::enable_if_t<std::is_enum_v<E>>>
70inline void leafFromJson(
const nlohmann::json& j, E& v) {
71 const std::string name = j.get<std::string>();
74 if (!Reflect::enumFromNameChecked(name, v)) {
77 "No enumerator called '%s' in this build; leaving the field at its default",
83template<
typename T>
struct IsHandle : std::false_type {};
84template<
typename A>
struct IsHandle<
Handle<A>> : std::true_type {};
85template<
typename T>
struct IsVector : std::false_type {};
86template<
typename E,
typename A>
struct IsVector<std::vector<E, A>> : std::true_type {};
96 static constexpr bool KEEPS_WRONG_FIELDS =
false;
101 static constexpr bool KEEPS_WRONG_FIELDS =
true;
104template<
typename T,
typename Ctx>
105nlohmann::json saveFields(
const T& obj,
const Ctx& ctx);
106template<
typename T,
typename Ctx>
107void loadFields(
const nlohmann::json& j, T& obj,
const Ctx& ctx,
const char* path =
nullptr);
116inline std::string
childPath(
const char* parent,
const std::string& step) {
117 return (parent && *parent) ? std::string(parent) +
"." + step : step;
124template<
typename V,
typename Ctx>
126 if constexpr (IsHandle<V>::value) {
127 static_assert(!std::is_base_of_v<PlainData, Ctx>,
"a Handle<T> needs a context that names assets");
128 return ctx.handleToJson(val);
129 }
else if constexpr (IsVector<V>::value) {
130 nlohmann::json arr = nlohmann::json::array();
131 for (
const auto& element : val) arr.push_back(
valueToJson(element, ctx));
133 }
else if constexpr (Reflect::IS_REFLECTED<V>) {
134 return saveFields(val, ctx);
136 return leafToJson(val);
146template<
typename V,
typename Ctx>
147void valueFromJson(
const nlohmann::json& j, V& val,
const Ctx& ctx,
const char* field) {
148 if constexpr (IsHandle<V>::value) {
149 static_assert(!std::is_base_of_v<PlainData, Ctx>,
"a Handle<T> needs a context that names assets");
150 ctx.handleFromJson(j, val, field);
151 }
else if constexpr (IsVector<V>::value && Ctx::KEEPS_WRONG_FIELDS) {
157 const nlohmann::json::array_t& elements = j.get_ref<
const nlohmann::json::array_t&>();
158 read.reserve(elements.size());
159 for (
const nlohmann::json& element : elements) {
160 typename V::value_type item{};
162 read.push_back(std::move(item));
164 val = std::move(read);
165 }
else if constexpr (IsVector<V>::value) {
167 const nlohmann::json::array_t& elements = j.get_ref<
const nlohmann::json::array_t&>();
169 val.reserve(elements.size());
170 for (
size_t i = 0; i < elements.size(); ++i) {
171 typename V::value_type item{};
173 val.push_back(std::move(item));
175 }
else if constexpr (Reflect::IS_REFLECTED<V>) {
176 loadFields(j, val, ctx, field);
178 leafFromJson(j, val);
182template<
typename T,
typename Ctx>
183nlohmann::json saveFields(
const T& obj,
const Ctx& ctx) {
184 nlohmann::json out = nlohmann::json::object();
185 Reflect::forEachField(obj, [&](std::string_view name,
const auto& val) {
191template<
typename T,
typename Ctx>
192void loadFields(
const nlohmann::json& j, T& obj,
const Ctx& ctx,
const char* path) {
193 if (!j.is_object()) {
195 if constexpr (Ctx::KEEPS_WRONG_FIELDS) {
196 LOG_WARNING_C(
"IO",
"a struct holds no object; leaving it as it was");
198 (void)j.get_ref<
const nlohmann::json::object_t&>();
202 Reflect::forEachField(obj, [&](std::string_view name,
auto& val) {
203 const std::string key(name);
204 const auto it = j.find(key);
205 if (it == j.end())
return;
206 const std::string field =
childPath(path, key);
207 if constexpr (Ctx::KEEPS_WRONG_FIELDS) {
210 }
catch (
const nlohmann::json::exception&) {
213 "'%s' holds a value of the wrong type; leaving the field as it was",
231 return saveFields(obj,
OwnFile{});
nlohmann::json valueToJson(const V &val, const Ctx &ctx)
One value to JSON, whatever kind of value it is: a handle its name, a vector an array,...
Definition reflect_json.h:125