json w.i.p

This commit is contained in:
Syoyo Fujita
2025-07-07 07:41:22 +09:00
parent b00c2d11f6
commit 89e4dce269
5 changed files with 96 additions and 15 deletions

View File

@@ -326,6 +326,10 @@ if(TINYUSDZ_USE_CCACHE)
endif()
if (TINYUSDZ_WITH_MCP_SERVER)
set(TINYUSDZ_WITH_JSON On)
endif()
if(TINYUSDZ_WITH_PYTHON)
# For the time beging, we stick with pybind11, since PyPI manylinux2014(probably mostly used architectrue as of 2022/Aug) does not support C++17.
@@ -475,6 +479,7 @@ if(TINYUSDZ_WITH_JSON)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/json-to-usd.cc)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/usd-to-json.cc)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/json-writer.cc)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/json-util.cc)
endif()
if(TINYUSDZ_WITH_USDFBX)
@@ -559,6 +564,7 @@ if(TINYUSDZ_WITH_ALAC_AUDIO)
endif(TINYUSDZ_WITH_ALAC_AUDIO)
if(TINYUSDZ_WITH_MCP_SERVER)
list(APPEND TINYUSDZ_DEP_SOURCES
${PROJECT_SOURCE_DIR}/src/external/civetweb/civetweb.c
)

17
src/json-util.cc Normal file
View File

@@ -0,0 +1,17 @@
#include "json-util.hh"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif
#include "external/jsonhpp/nlohmann/json.hpp"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
namespace tinyusdz {
} // namespace tinyusdz

63
src/json-util.hh Normal file
View File

@@ -0,0 +1,63 @@
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif
#include "external/jsonhpp/nlohmann/json.hpp"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#pragma once
namespace tinyusdz {
using json = nlohmann::json;
#if 0
namespace json {
struct JsonValue
{
public:
enum class ValueType {
NullType,
BooleanType,
StringType,
NumberType,
ArrayType,
ObjectType
};
typedef std::vector<JsonValue> Array;
typedef std::map<std::string, JsonValue> Object;
JsonValue() = default;
explicit JsonValue(bool b) : type_{BoolType} {
boolean_value_ = b;
}
explicit JsonValue(const std::string &s) : type_{StringType} {
string_value_ = b;
}
template<typename T>
private:
ValueType type_{NullType};
double number_value_{0.0};
std::string string_value;
Array array_value_;
Object object_value_;
bool boolean_value_{false};
};
} // namespace json
#endif
} // namespace tinyusdz

View File

@@ -2,16 +2,7 @@
#include "command-and-history.hh"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif
#include "external/jsonhpp/nlohmann/json.hpp"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include "json-util.hh"
namespace tinyusdz {
@@ -54,8 +45,8 @@ bool HistoryQueue::redo() {
return true;
}
std::string HistoryQueue::to_json_string() const {
nlohmann::json j;
std::string HistoryQueue::to_json_string(bool dump_layer) const {
json j;
j["history"] = nlohmann::json::array();
for (const auto &hist : history_queues) {
@@ -64,8 +55,11 @@ std::string HistoryQueue::to_json_string() const {
hist_json["op"] = static_cast<int>(hist.op);
hist_json["arg"] = hist.arg;
hist_json["id"] = hist.id;
// Serialize layer if needed
// hist_json["layer"] = ...; // Implement layer serialization if necessary
if (dump_layer) {
// TODO: Serialize layer if needed
// hist_json["layer"] = ...; // Implement layer serialization if necessary
}
j["history"].push_back(hist_json);
}

View File

@@ -57,7 +57,8 @@ class HistoryQueue
bool redo();
// Serialize the history queue to a JSON string
std::string to_json_string() const;
// `dump_layer` : Include JSON dump of `Layer` in each EditHistory.
std::string to_json_string(bool dump_layer = false) const;
private:
std::deque<EditHistory> history_queues;