-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.h
134 lines (116 loc) · 3.76 KB
/
Converter.h
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
/*
* File: Converter.h
* Author: hugh
*
* Created on July 11, 2016, 6:09 PM
*/
#ifndef CONVERTER_H
#define CONVERTER_H
#include "Node.h"
#include "SchemaTree.h"
using namespace std;
using namespace boost::algorithm;
using json = nlohmann::json;
class Converter {
private:
map<string, deque<string>> embeddedColumns;
Node* header;
string delimiter, replaceStr;
SchemaTree* h;
public:
Converter(string delimiter, Node* header, SchemaTree* h)
{
this->delimiter = delimiter;
this->header = header;
this->h = h;
}
static int getMaxList(map<string,deque<string>> ras)
{
size_t max = 0;
deque<string> ra;
map<string,deque<string>>::iterator it;
for (it = ras.begin(); it != ras.end(); ++it)
{
ra = ras[it->first];
if (ra[0].substr(0,1) == Main::internalDelimiterString())
ra = Main::split(ra[0].substr(1, ra[0].size()-2),
Main::internalDelimiterChar());
if (max < ra.size())
max = ra.size();
}
return max;
}
Node* searchHeaderTree(Node* rootNode, string searchPath)
{
searchPath = boost::trim_copy(searchPath);
if (searchPath.length() == 0 || searchPath == "_")
return rootNode;
deque<Node*> schemaNodes;
this->h->getHeaderPaths(rootNode, schemaNodes);
assert (schemaNodes.size() > 0);
deque<Node*>::iterator it;
for (it = schemaNodes.begin(); it != schemaNodes.end(); ++it)
{
if (*it != NULL && (*it)->getPath() == searchPath)
{
return *it;
}
}
cout << "search for " << searchPath << " failed!";
return NULL;
}
void simpleAppend(string key, string value)
{
std::ofstream out;
string filepath = Main::getResultsDirectory() + "/" + key + ".txt";
out.open(filepath, std::ios::app);
out << value + "\n";
out.close();
}
void convert(json node, deque<string> currentPath)
{
string pathString = "_" + join(currentPath, "_");
Node* schemaNode = this->searchHeaderTree(header, pathString);
assert (schemaNode != NULL);
if (node.is_object())
{
LinkedList<Node>* en = schemaNode->getChildren();
while (en != NULL && !en->is_null())
{
Node* child = (Node*)en->data();
json value = node[ child->getName() ];
if (child->getChildren()->getSize() == 0 && (value == NULL
|| value.is_null()))
{
this->simpleAppend(child->getPath(), "\"null\"");
} else if (value.is_primitive())
{
this->simpleAppend(child->getPath(), "\"" + value.dump() + "\"");
} else {
currentPath.push_back(child->getName());
this->convert(value, currentPath);
if (currentPath.size() > 0) currentPath.pop_back();
}
en = en->nextNode();
}
} else if (node.is_array())
{
for (json item : node)
{
if (item.is_primitive())
{
embeddedColumns[pathString].push_back("\"" + item.dump() + "\"");
} else {
this->convert(item, currentPath);
}
}
}
}
map<string,deque<string>> getEmbeddedColumns()
{
return this->embeddedColumns;
}
~Converter()
{ } // see ~SchemaTree & ~Node
};
#endif /* CONVERTER_H */