2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-04-21 10:58:44 +00:00

Merge pull request #746 from Laupetin/fix/ordered_json_extensions

fix: ordered json extensions
This commit is contained in:
Jan
2026-04-20 21:57:39 +02:00
committed by GitHub
8 changed files with 29 additions and 23 deletions

View File

@@ -325,21 +325,21 @@ namespace gltf
class JsonRoot class JsonRoot
{ {
public: public:
std::optional<std::vector<JsonAccessor>> accessors;
std::optional<std::vector<JsonAnimation>> animations;
JsonAsset asset; JsonAsset asset;
std::optional<std::vector<JsonBuffer>> buffers;
std::optional<std::vector<JsonBufferView>> bufferViews;
std::optional<std::vector<JsonImage>> images;
std::optional<std::vector<JsonMaterial>> materials;
std::optional<std::vector<JsonMesh>> meshes;
std::optional<std::vector<JsonNode>> nodes;
std::optional<std::vector<JsonSkin>> skins;
std::optional<unsigned> scene; std::optional<unsigned> scene;
std::optional<std::vector<JsonScene>> scenes; std::optional<std::vector<JsonScene>> scenes;
std::optional<std::vector<JsonNode>> nodes;
std::optional<std::vector<JsonAnimation>> animations;
std::optional<std::vector<JsonMaterial>> materials;
std::optional<std::vector<JsonMesh>> meshes;
std::optional<std::vector<JsonTexture>> textures; std::optional<std::vector<JsonTexture>> textures;
std::optional<std::vector<JsonImage>> images;
std::optional<std::vector<JsonSkin>> skins;
std::optional<std::vector<JsonAccessor>> accessors;
std::optional<std::vector<JsonBufferView>> bufferViews;
std::optional<std::vector<JsonBuffer>> buffers;
}; };
NLOHMANN_DEFINE_TYPE_EXTENSION( NLOHMANN_DEFINE_TYPE_EXTENSION(
JsonRoot, accessors, animations, asset, buffers, bufferViews, images, materials, meshes, nodes, skins, scene, scenes, textures); JsonRoot, asset, scene, scenes, nodes, animations, materials, meshes, textures, images, skins, accessors, bufferViews, buffers);
} // namespace gltf } // namespace gltf

View File

@@ -33,7 +33,7 @@ std::optional<std::string> BinOutput::CreateBufferUri(const void* buffer, size_t
return std::nullopt; return std::nullopt;
} }
void BinOutput::EmitJson(const nlohmann::json& json) const void BinOutput::EmitJson(const nlohmann::ordered_json& json) const
{ {
static constexpr uint32_t ZERO = 0u; static constexpr uint32_t ZERO = 0u;

View File

@@ -12,7 +12,7 @@ namespace gltf
explicit BinOutput(std::ostream& stream); explicit BinOutput(std::ostream& stream);
std::optional<std::string> CreateBufferUri(const void* buffer, size_t bufferSize) const override; std::optional<std::string> CreateBufferUri(const void* buffer, size_t bufferSize) const override;
void EmitJson(const nlohmann::json& json) const override; void EmitJson(const nlohmann::ordered_json& json) const override;
void EmitBuffer(const void* buffer, size_t bufferSize) const override; void EmitBuffer(const void* buffer, size_t bufferSize) const override;
void Finalize() const override; void Finalize() const override;

View File

@@ -18,7 +18,7 @@ namespace gltf
public: public:
virtual std::optional<std::string> CreateBufferUri(const void* buffer, size_t bufferSize) const = 0; virtual std::optional<std::string> CreateBufferUri(const void* buffer, size_t bufferSize) const = 0;
virtual void EmitJson(const nlohmann::json& json) const = 0; virtual void EmitJson(const nlohmann::ordered_json& json) const = 0;
virtual void EmitBuffer(const void* buffer, size_t bufferSize) const = 0; virtual void EmitBuffer(const void* buffer, size_t bufferSize) const = 0;
virtual void Finalize() const = 0; virtual void Finalize() const = 0;
}; };

View File

@@ -32,7 +32,7 @@ std::optional<std::string> TextOutput::CreateBufferUri(const void* buffer, const
return output; return output;
} }
void TextOutput::EmitJson(const nlohmann::json& json) const void TextOutput::EmitJson(const nlohmann::ordered_json& json) const
{ {
m_stream << std::setw(4) << json; m_stream << std::setw(4) << json;
} }

View File

@@ -12,7 +12,7 @@ namespace gltf
explicit TextOutput(std::ostream& stream); explicit TextOutput(std::ostream& stream);
std::optional<std::string> CreateBufferUri(const void* buffer, size_t bufferSize) const override; std::optional<std::string> CreateBufferUri(const void* buffer, size_t bufferSize) const override;
void EmitJson(const nlohmann::json& json) const override; void EmitJson(const nlohmann::ordered_json& json) const override;
void EmitBuffer(const void* buffer, size_t bufferSize) const override; void EmitBuffer(const void* buffer, size_t bufferSize) const override;
void Finalize() const override; void Finalize() const override;

View File

@@ -98,7 +98,7 @@ namespace
FillBufferData(gltf, xmodel, bufferData); FillBufferData(gltf, xmodel, bufferData);
CreateBuffer(gltf, xmodel, bufferData); CreateBuffer(gltf, xmodel, bufferData);
const json jRoot = gltf; const ordered_json jRoot = gltf;
m_output->EmitJson(jRoot); m_output->EmitJson(jRoot);
if (!bufferData.empty()) if (!bufferData.empty())

View File

@@ -14,17 +14,19 @@
// partial specialization (full specialization works too) // partial specialization (full specialization works too)
namespace nlohmann namespace nlohmann
{ {
template<class T> void optional_to_json(nlohmann::json& j, const char* name, const std::optional<T>& value) template<class T, typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
void optional_to_json(BasicJsonType& j, const char* name, const std::optional<T>& value)
{ {
if (value) if (value)
j[name] = *value; j[name] = *value;
} }
template<class T> void optional_from_json(const nlohmann::json& j, const char* name, std::optional<T>& value) template<class T, typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
void optional_from_json(const BasicJsonType& j, const char* name, std::optional<T>& value)
{ {
const auto it = j.find(name); const auto it = j.find(name);
if (it != j.end() && !it->is_null()) if (it != j.end() && !it->is_null())
value = it->get<T>(); value = it->template get<T>();
else else
value = std::nullopt; value = std::nullopt;
} }
@@ -32,7 +34,8 @@ namespace nlohmann
template<typename> constexpr bool is_optional = false; template<typename> constexpr bool is_optional = false;
template<typename T> constexpr bool is_optional<std::optional<T>> = true; 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) template<typename T, typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
void extended_to_json(const char* key, BasicJsonType& j, const T& value)
{ {
if constexpr (is_optional<T>) if constexpr (is_optional<T>)
nlohmann::optional_to_json(j, key, value); nlohmann::optional_to_json(j, key, value);
@@ -40,7 +43,8 @@ namespace nlohmann
j[key] = value; j[key] = value;
} }
template<typename T> void extended_from_json(const char* key, const nlohmann::json& j, T& value) template<typename T, typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
void extended_from_json(const char* key, const BasicJsonType& j, T& value)
{ {
if constexpr (is_optional<T>) if constexpr (is_optional<T>)
nlohmann::optional_from_json(j, key, value); nlohmann::optional_from_json(j, key, value);
@@ -53,11 +57,13 @@ namespace nlohmann
#define EXTEND_JSON_FROM(v1) extended_from_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, ...) \ #define NLOHMANN_DEFINE_TYPE_EXTENSION(Type, ...) \
inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) \ template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
inline void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
{ \ { \
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_TO, __VA_ARGS__)) \ 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) \ template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
inline void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
{ \ { \
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_FROM, __VA_ARGS__)) \ NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_FROM, __VA_ARGS__)) \
} }