Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonGetKeys #2050

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions zeno/include/zeno/funcs/LiterialConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ inline bool objectIsLiterial(std::shared_ptr<IObject> const &ptr) {
}
}

template <class T>
inline bool objectIsRawLiterial(std::shared_ptr<IObject> const &ptr) {
if constexpr (std::is_base_of_v<IObject, T>) {
return dynamic_cast<T *>(ptr.get());
} else if constexpr (std::is_same_v<std::string, T>) {
return dynamic_cast<StringObject *>(ptr.get());
} else if constexpr (std::is_same_v<NumericValue, T>) {
return dynamic_cast<NumericObject *>(ptr.get());
} else {
auto p = dynamic_cast<NumericObject *>(ptr.get());
return p && std::visit([&] (auto const &val) -> bool {
return std::is_same_v<T, std::decay_t<decltype(val)>>;
}, p->value);
}
}

template <class T>
inline auto objectToLiterial(std::shared_ptr<IObject> const &ptr, std::string const &msg = "objectToLiterial") {
if constexpr (std::is_base_of_v<IObject, T>) {
Expand Down
43 changes: 33 additions & 10 deletions zeno/src/nodes/JsonProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <tinygltf/json.hpp>
#include <zeno/zeno.h>

using Json = nlohmann::json;
using Json = nlohmann::ordered_json;

namespace zeno {
struct JsonObject : IObjectClone<JsonObject> {
Expand Down Expand Up @@ -64,37 +64,37 @@ ZENDEFNODE(WriteJson, {
});
static Json iobject_to_json(std::shared_ptr<IObject> iObject) {
Json json;
if (objectIsLiterial<int>(iObject)) {
if (objectIsRawLiterial<int>(iObject)) {
json = objectToLiterial<int>(iObject);
}
else if (objectIsLiterial<vec2i>(iObject)) {
else if (objectIsRawLiterial<vec2i>(iObject)) {
auto value = objectToLiterial<vec2i>(iObject);
json = { value[0], value[1]};
}
else if (objectIsLiterial<vec3i>(iObject)) {
else if (objectIsRawLiterial<vec3i>(iObject)) {
auto value = objectToLiterial<vec3i>(iObject);
json = { value[0], value[1], value[2]};
}
else if (objectIsLiterial<vec4i>(iObject)) {
else if (objectIsRawLiterial<vec4i>(iObject)) {
auto value = objectToLiterial<vec4i>(iObject);
json = { value[0], value[1], value[2], value[3]};
}
else if (objectIsLiterial<float>(iObject)) {
else if (objectIsRawLiterial<float>(iObject)) {
json = objectToLiterial<float>(iObject);
}
else if (objectIsLiterial<vec2f>(iObject)) {
else if (objectIsRawLiterial<vec2f>(iObject)) {
auto value = objectToLiterial<vec2f>(iObject);
json = { value[0], value[1]};
}
else if (objectIsLiterial<vec3f>(iObject)) {
else if (objectIsRawLiterial<vec3f>(iObject)) {
auto value = objectToLiterial<vec3f>(iObject);
json = { value[0], value[1], value[2]};
}
else if (objectIsLiterial<vec4f>(iObject)) {
else if (objectIsRawLiterial<vec4f>(iObject)) {
auto value = objectToLiterial<vec4f>(iObject);
json = { value[0], value[1], value[2], value[3]};
}
else if (objectIsLiterial<std::string>(iObject)) {
else if (objectIsRawLiterial<std::string>(iObject)) {
json = objectToLiterial<std::string>(iObject);
}
else if (auto list = std::dynamic_pointer_cast<ListObject>(iObject)) {
Expand Down Expand Up @@ -415,6 +415,29 @@ ZENDEFNODE(JsonGetString, {
"deprecated"
},
});

struct JsonGetKeys : zeno::INode {
virtual void apply() override {
auto json = get_input<JsonObject>("json");
auto list = std::make_shared<ListObject>();
for (auto& [key, _] : json->json.items()) {
list->arr.emplace_back(std::make_shared<zeno::StringObject>(key));
}
set_output2("keys", list);
}
};
ZENDEFNODE(JsonGetKeys, {
{
{"json"},
},
{
"keys",
},
{},
{
"json"
},
});
struct JsonGetTypeName : zeno::INode {
virtual void apply() override {
auto json = get_input<JsonObject>("json");
Expand Down
Loading