chigraph  master
Systems programming language written for beginners in LLVM
JsonSerializer.cpp
Go to the documentation of this file.
1 
3 #include "chi/JsonSerializer.hpp"
4 #include "chi/DataType.hpp"
5 #include "chi/GraphFunction.hpp"
6 #include "chi/GraphModule.hpp"
7 #include "chi/GraphStruct.hpp"
8 #include "chi/NodeInstance.hpp"
9 #include "chi/NodeType.hpp"
10 
11 #include <boost/uuid/uuid_io.hpp> // for boost::uuids::to_string
12 
13 namespace chi {
14 
15 nlohmann::json graphFunctionToJson(const GraphFunction& func) {
16  auto jsonData = nlohmann::json{};
17 
18  jsonData["type"] = "function";
19  jsonData["name"] = func.name();
20  jsonData["description"] = func.description();
21 
22  auto& datainputsjson = jsonData["data_inputs"];
23  datainputsjson = nlohmann::json::array();
24 
25  for (auto& in : func.dataInputs()) {
26  datainputsjson.push_back({{in.name, in.type.qualifiedName()}});
27  }
28 
29  // serialize the local variables
30  auto& localsjson = jsonData["local_variables"];
31  localsjson = nlohmann::json::object();
32  for (const auto& local : func.localVariables()) {
33  localsjson[local.name] = local.type.qualifiedName();
34  }
35 
36  auto& dataoutputsjson = jsonData["data_outputs"];
37  dataoutputsjson = nlohmann::json::array();
38 
39  for (auto& out : func.dataOutputs()) {
40  dataoutputsjson.push_back({{out.name, out.type.qualifiedName()}});
41  }
42 
43  auto& execinputsjson = jsonData["exec_inputs"];
44  execinputsjson = nlohmann::json::array();
45 
46  for (auto& in : func.execInputs()) { execinputsjson.push_back(in); }
47 
48  auto& execoutputsjson = jsonData["exec_outputs"];
49  execoutputsjson = nlohmann::json::array();
50 
51  for (auto& out : func.execOutputs()) { execoutputsjson.push_back(out); }
52 
53  // serialize the nodes
54  auto& jsonNodes = jsonData["nodes"];
55  jsonNodes = nlohmann::json::object(); // make sure even if it's empty it's an object
56  auto& jsonConnections = jsonData["connections"];
57  jsonConnections = nlohmann::json::array(); // make sure even if it's empty it's an aray
58 
59  for (const auto& nodepair : func.nodes()) {
60  auto& node = nodepair.second;
61  std::string nodeID = boost::uuids::to_string(nodepair.first);
62 
63  nlohmann::json nodeJson = node->type().toJSON();
64  jsonNodes[nodeID] = {{"type", node->type().qualifiedName()},
65  {"location", {node->x(), node->y()}},
66  {"data", nodeJson}};
67  // add its connections. Just out the outputs to avoid duplicates
68 
69  // add the exec outputs
70  for (auto conn_id = 0ull; conn_id < node->outputExecConnections.size(); ++conn_id) {
71  auto& conn = node->outputExecConnections[conn_id];
72  // if there is actually a connection
73  if (conn.first != nullptr) {
74  jsonConnections.push_back(
75  {{"type", "exec"},
76  {"input", {nodeID, conn_id}},
77  {"output", {boost::uuids::to_string(conn.first->id()), conn.second}}});
78  }
79  }
80 
81  // add the data outputs
82  for (auto conn_id = 0ull; conn_id < node->inputDataConnections.size(); ++conn_id) {
83  // if there is actually a connection
84  auto& connpair = node->inputDataConnections[conn_id];
85  if (connpair.first != nullptr) {
86  jsonConnections.push_back(
87  {{"type", "data"},
88  {"input", {boost::uuids::to_string(connpair.first->id()), connpair.second}},
89  {"output", {nodeID, conn_id}}});
90  }
91  }
92  }
93 
94  return jsonData;
95 }
96 
97 nlohmann::json graphModuleToJson(const GraphModule& mod) {
98  nlohmann::json data;
99 
100  auto& depsjson = data["dependencies"];
101  depsjson = nlohmann::json::array();
102  for (const auto& dep : mod.dependencies()) { depsjson.push_back(dep.generic_string()); }
103 
104  auto& graphsjson = data["graphs"];
105  graphsjson = nlohmann::json::array();
106  for (auto& graph : mod.functions()) { graphsjson.push_back(graphFunctionToJson(*graph)); }
107 
108  auto& structsJson = data["types"];
109  structsJson = nlohmann::json::object();
110  for (const auto& str : mod.structs()) { structsJson[str->name()] = graphStructToJson(*str); }
111 
112  data["has_c_support"] = mod.cEnabled();
113 
114  return data;
115 }
116 
117 nlohmann::json graphStructToJson(const GraphStruct& struc) {
118  nlohmann::json ret = nlohmann::json::array();
119 
120  for (const auto& elem : struc.types()) {
121  ret.push_back({{elem.name, elem.type.qualifiedName()}});
122  }
123 
124  return ret;
125 }
126 
127 } // namespace chi
nlohmann::json graphFunctionToJson(const GraphFunction &func)
Serialize a GraphFunction to json.
Define json serialization functions.
const std::vector< NamedDataType > & dataOutputs() const
Get the function data outputs in the format {type, docstring}.
this is an AST-like representation of a function in a graph It is used for IDE-like behavior...
const std::vector< std::string > & execInputs() const
Get the function exec inputs.
A class holding a compound type defined in a GraphModule.
Definition: GraphStruct.hpp:18
const std::vector< std::string > & execOutputs() const
Get the function exec outputs.
const std::vector< std::unique_ptr< GraphFunction > > & functions() const
Get functions.
Definition: GraphModule.hpp:95
Define GraphStruct.
std::string name() const
Get the name of the function.
nlohmann::json graphStructToJson(const GraphStruct &struc)
Serialize a GraphStruct to json.
bool cEnabled() const
Gets if C support is enabled.
std::unordered_map< boost::uuids::uuid, std::unique_ptr< NodeInstance > > & nodes()
Get the nodes in the function Usually called by connectData or connectExec or GraphFunction.
Defines the NodeType class.
nlohmann::json graphModuleToJson(const GraphModule &mod)
Serialize a JsonModule to json.
const std::vector< NamedDataType > & types() const
Get the types the struct contains.
Definition: GraphStruct.hpp:47
const std::set< boost::filesystem::path > & dependencies() const
Get the dependencies.
Definition: ChiModule.hpp:83
Defines the DataType class.
const std::vector< NamedDataType > & localVariables() const
Get the local variables.
Module that holds graph functions.
Definition: GraphModule.hpp:16
Defines the GraphModule class.
The namespace where chigraph lives.
const std::vector< NamedDataType > & dataInputs() const
Get the function data inputs in the format {type, docstring}.
Declares the GraphFunction class.
Defines the NodeInstance class and related functions.
const std::string & description() const
Get the description of the function.