Skip to content

Commit

Permalink
Merge pull request #2050 from zenustech/json_get_keys
Browse files Browse the repository at this point in the history
JsonGetKeys
  • Loading branch information
zhxx1987 authored Nov 5, 2024
2 parents bc0554b + 566b113 commit 8ef03c8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
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

0 comments on commit 8ef03c8

Please sign in to comment.