| 11 | namespace chi { |
| 12 | |
| 13 | nlohmann::json graphFunctionToJson(const GraphFunction& func) { |
| 14 | auto jsonData = nlohmann::json{}; |
| 15 | |
| 16 | jsonData["type"] = "function"; |
| 17 | jsonData["name"] = func.name(); |
| 18 | jsonData["description"] = func.description(); |
| 19 | |
| 20 | auto& datainputsjson = jsonData["data_inputs"]; |
| 21 | datainputsjson = nlohmann::json::array(); |
| 22 | |
| 23 | for (auto& in : func.dataInputs()) { |
| 24 | datainputsjson.push_back({{in.name, in.type.qualifiedName()}}); |
| 25 | } |
| 26 | |
| 27 | // serialize the local variables |
| 28 | auto& localsjson = jsonData["local_variables"]; |
| 29 | localsjson = nlohmann::json::object(); |
| 30 | for (const auto& local : func.localVariables()) { |
| 31 | localsjson[local.name] = local.type.qualifiedName(); |
| 32 | } |
| 33 | |
| 34 | auto& dataoutputsjson = jsonData["data_outputs"]; |
| 35 | dataoutputsjson = nlohmann::json::array(); |
| 36 | |
| 37 | for (auto& out : func.dataOutputs()) { |
| 38 | dataoutputsjson.push_back({{out.name, out.type.qualifiedName()}}); |
| 39 | } |
| 40 | |
| 41 | auto& execinputsjson = jsonData["exec_inputs"]; |
| 42 | execinputsjson = nlohmann::json::array(); |
| 43 | |
| 44 | for (auto& in : func.execInputs()) { execinputsjson.push_back(in); } |
| 45 | |
| 46 | auto& execoutputsjson = jsonData["exec_outputs"]; |
| 47 | execoutputsjson = nlohmann::json::array(); |
| 48 | |
| 49 | for (auto& out : func.execOutputs()) { execoutputsjson.push_back(out); } |
| 50 | |
| 51 | // serialize the nodes |
| 52 | auto& jsonNodes = jsonData["nodes"]; |
| 53 | jsonNodes = nlohmann::json::object(); // make sure even if it's empty it's an object |
| 54 | auto& jsonConnections = jsonData["connections"]; |
| 55 | jsonConnections = nlohmann::json::array(); // make sure even if it's empty it's an aray |
| 56 | |
| 57 | for (const auto& nodepair : func.nodes()) { |
| 58 | auto& node = nodepair.second; |
| 59 | std::string nodeID = boost::uuids::to_string(nodepair.first); |
| 60 | |
| 61 | nlohmann::json nodeJson = node->type().toJSON(); |
| 62 | jsonNodes[nodeID] = {{"type", node->type().qualifiedName()}, |
| 63 | {"location", {node->x(), node->y()}}, |
| 64 | {"data", nodeJson}}; |
| 65 | // add its connections. Just out the outputs to avoid duplicates |
| 66 | |
| 67 | // add the exec outputs |
| 68 | for (auto conn_id = 0ull; conn_id < node->outputExecConnections.size(); ++conn_id) { |
| 69 | auto& conn = node->outputExecConnections[conn_id]; |
| 70 | // if there is actually a connection |
no test coverage detected