diff --git a/zeno/include/zeno/funcs/LiterialConverter.h b/zeno/include/zeno/funcs/LiterialConverter.h index 47ddb9d114..d0c7690900 100644 --- a/zeno/include/zeno/funcs/LiterialConverter.h +++ b/zeno/include/zeno/funcs/LiterialConverter.h @@ -23,6 +23,22 @@ inline bool objectIsLiterial(std::shared_ptr const &ptr) { } } +template +inline bool objectIsRawLiterial(std::shared_ptr const &ptr) { + if constexpr (std::is_base_of_v) { + return dynamic_cast(ptr.get()); + } else if constexpr (std::is_same_v) { + return dynamic_cast(ptr.get()); + } else if constexpr (std::is_same_v) { + return dynamic_cast(ptr.get()); + } else { + auto p = dynamic_cast(ptr.get()); + return p && std::visit([&] (auto const &val) -> bool { + return std::is_same_v>; + }, p->value); + } +} + template inline auto objectToLiterial(std::shared_ptr const &ptr, std::string const &msg = "objectToLiterial") { if constexpr (std::is_base_of_v) { diff --git a/zeno/src/nodes/JsonProcess.cpp b/zeno/src/nodes/JsonProcess.cpp index 0518ab6e3a..da433aaf71 100644 --- a/zeno/src/nodes/JsonProcess.cpp +++ b/zeno/src/nodes/JsonProcess.cpp @@ -13,7 +13,7 @@ #include #include -using Json = nlohmann::json; +using Json = nlohmann::ordered_json; namespace zeno { struct JsonObject : IObjectClone { @@ -64,37 +64,37 @@ ZENDEFNODE(WriteJson, { }); static Json iobject_to_json(std::shared_ptr iObject) { Json json; - if (objectIsLiterial(iObject)) { + if (objectIsRawLiterial(iObject)) { json = objectToLiterial(iObject); } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { auto value = objectToLiterial(iObject); json = { value[0], value[1]}; } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { auto value = objectToLiterial(iObject); json = { value[0], value[1], value[2]}; } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { auto value = objectToLiterial(iObject); json = { value[0], value[1], value[2], value[3]}; } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { json = objectToLiterial(iObject); } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { auto value = objectToLiterial(iObject); json = { value[0], value[1]}; } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { auto value = objectToLiterial(iObject); json = { value[0], value[1], value[2]}; } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { auto value = objectToLiterial(iObject); json = { value[0], value[1], value[2], value[3]}; } - else if (objectIsLiterial(iObject)) { + else if (objectIsRawLiterial(iObject)) { json = objectToLiterial(iObject); } else if (auto list = std::dynamic_pointer_cast(iObject)) { @@ -415,6 +415,29 @@ ZENDEFNODE(JsonGetString, { "deprecated" }, }); + +struct JsonGetKeys : zeno::INode { + virtual void apply() override { + auto json = get_input("json"); + auto list = std::make_shared(); + for (auto& [key, _] : json->json.items()) { + list->arr.emplace_back(std::make_shared(key)); + } + set_output2("keys", list); + } +}; +ZENDEFNODE(JsonGetKeys, { + { + {"json"}, + }, + { + "keys", + }, + {}, + { + "json" + }, +}); struct JsonGetTypeName : zeno::INode { virtual void apply() override { auto json = get_input("json");