-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaTree.h
More file actions
172 lines (152 loc) · 4.69 KB
/
SchemaTree.h
File metadata and controls
172 lines (152 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* File: SchemaTree.h
* Author: hugh
*
* Created on July 3, 2016, 11:47 PM
*/
#ifndef SCHEMATREE_H
#define SCHEMATREE_H
#include "Main.h"
#include "Node.h"
using namespace std;
using json = nlohmann::json;
class SchemaTree {
private:
Node* schema;
char* delimiter;
const char* replaceStr;
deque<string> header;
public:
SchemaTree(char* delimiter)
{
this->delimiter = delimiter;
schema = NULL;
this->replaceStr = (delimiter == (char*)",")? ";" : "|" ;
}
string readJSONString(const char* filePath)
{
std::ifstream t(filePath);
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return str;
}
Node* getRootNode()
{
return schema;
}
deque<string> getHeader()
{
return header;
}
deque<string> constructHeader(json rootJson)
{
this->schema = new Node("root", "");
this->buildHeaderPaths(rootJson, this->schema);
this->collectKeys(this->schema);
return this->header;
}
/**
*
* @param root
*/
deque<string> collectKeys(Node* root)
{
if (root->getChildren()->getSize() > 0)
{
for (LinkedList<Node>* temp = root->getChildren();
temp != NULL;
temp = temp->nextNode() )
{
if (temp->data() == NULL ||
temp->data()->getChildren() == NULL)
{
continue;
} else if(temp->data()->getChildren()->getSize() == 0)
{
this->header.push_back(temp->data()->getPath());
}
collectKeys(temp->data());
}
}
return this->header;
}
void setSchema(Node* schema)
{
this->schema = schema;
}
const char* getDelimiter()
{
return this->delimiter;
}
/**
* left off here
* @param element
* @param currentNode
*/
void buildHeaderPaths(json element, Node* currentNode)
{
if (element.is_object())
{
Node* child;
//iterate over the json object
for (json::iterator entry = element.begin();
entry != element.end(); ++entry)
{
//handle key
child = new Node(entry.key(), currentNode->getPath()
+ "_" + entry.key());
currentNode->add(child);
if (!entry.value().is_primitive())
{
buildHeaderPaths(entry.value(), child);
}
}
child = NULL;
} else if (element.is_array())
{
// iterate over the array
for (json::iterator entry = element.begin();
entry != element.end(); ++entry)
{
if (!entry.value().is_primitive())
{
buildHeaderPaths(entry.value(), currentNode);
}
}
}
}
void getHeaderPaths(Node* root, deque<Node*> & results)
{
results.push_back(root);
LinkedList<Node>* children = root -> getChildren();
if (children->getSize() == 0)
return;
else {
for (; children != NULL && !children->is_null(); children = children->nextNode())
{
this->getHeaderPaths(children -> data(), results);
}
}
children = NULL;
}
void printHeaderPaths(Node* root)
{
cout << root->getPath() << endl;
LinkedList<Node>* children = root -> getChildren();
if (children->getSize() == 0)
return;
else {
for (; children != NULL && !children->is_null(); children = children->nextNode())
{
this->printHeaderPaths(children->data());
}
}
}
~SchemaTree()
{
if (this->schema != NULL)
delete this->schema;
header.erase(header.begin(), header.end());
}
};
#endif /* SCHEMATREE_H */