Skip to content

Commit

Permalink
Merge pull request #1612 from zenustech/improve-json
Browse files Browse the repository at this point in the history
Improve json
  • Loading branch information
zhxx1987 authored Dec 11, 2023
2 parents b8e7050 + cce6a9c commit 9f5fa13
Showing 1 changed file with 88 additions and 5 deletions.
93 changes: 88 additions & 5 deletions zeno/src/nodes/JsonProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <tinygltf/json.hpp>
#include "zeno/utils/fileio.h"
#include "zeno/utils/string.h"
#include "zeno/types/ListObject.h"
#include "zeno/types/DictObject.h"
#include <iostream>
#include <sstream>
#include <string>

using Json = nlohmann::json;

namespace zeno {
Expand Down Expand Up @@ -138,7 +144,7 @@ ZENDEFNODE(JsonGetChild, {
},
{},
{
"json"
"deprecated"
},
});
struct JsonGetInt : zeno::INode {
Expand All @@ -156,7 +162,7 @@ ZENDEFNODE(JsonGetInt, {
},
{},
{
"json"
"deprecated"
},
});

Expand All @@ -175,7 +181,7 @@ ZENDEFNODE(JsonGetFloat, {
},
{},
{
"json"
"deprecated"
},
});

Expand All @@ -194,7 +200,7 @@ ZENDEFNODE(JsonGetString, {
},
{},
{
"json"
"deprecated"
},
});
struct JsonGetTypeName : zeno::INode {
Expand All @@ -212,7 +218,7 @@ ZENDEFNODE(JsonGetTypeName, {
},
{},
{
"json"
"deprecated"
},
});

Expand Down Expand Up @@ -266,6 +272,83 @@ ZENDEFNODE(JsonData, {
"out",
},
{},
{
"deprecated"
},
});

struct JsonGetData : zeno::INode {
virtual void apply() override {
auto in_json = get_input<JsonObject>("json");
auto multi_path = get_input2<std::string>("paths");
std::istringstream iss(multi_path);
std::vector<std::string> paths;
std::string line;
while (std::getline(iss, line)) {
line = zeno::trim_string(line);
if (line.size()) {
paths.push_back(line);
}
}

auto dict = std::make_shared<zeno::DictObject>();
for (auto &path: paths) {
auto json = std::make_shared<JsonObject>();
json->json = in_json->json;
auto strings = zeno::split_str(path, ':');
auto type = strings[1];
path = strings[0];
auto names = split_str(path, '/');

for (auto & name : names) {
json->json = json->json[name];
}

if (type == "json") {
auto out_json = std::make_shared<JsonObject>();
out_json->json = json->json;
dict->lut[path] = out_json;
}
else if (type == "int") {
dict->lut[path] = std::make_shared<NumericObject>(int(json->json));
}
else if (type == "float") {
dict->lut[path] = std::make_shared<NumericObject>(float(json->json));
}
else if (type == "string") {
dict->lut[path] = std::make_shared<StringObject>(std::string(json->json));
}
else if (type == "vec2f") {
float x = float(json->json[0]);
float y = float(json->json[1]);
dict->lut[path] = std::make_shared<NumericObject>(vec2f(x, y));
}
else if (type == "vec3f") {
float x = float(json->json[0]);
float y = float(json->json[1]);
float z = float(json->json[2]);
dict->lut[path] = std::make_shared<NumericObject>(vec3f(x, y, z));
}
else if (type == "vec4f") {
float x = float(json->json[0]);
float y = float(json->json[1]);
float z = float(json->json[2]);
float w = float(json->json[3]);
dict->lut[path] = std::make_shared<NumericObject>(vec4f(x, y, z, w));
}
}
set_output("outs", dict);
}
};
ZENDEFNODE(JsonGetData, {
{
{"json"},
{"multiline_string", "paths"},
},
{
{"DictObject", "outs"}
},
{},
{
"json"
},
Expand Down

0 comments on commit 9f5fa13

Please sign in to comment.