-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_parser.cpp
67 lines (58 loc) · 1.84 KB
/
json_parser.cpp
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
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include <fstream>
using namespace std;
vector<string> split(char delimiter, string str);
vector<vector<string>> parse_json(string json);
vector<string> removeWhitespaceEntries(vector<string> items);
vector<vector<string>> fromFile(string filename) {
ifstream file(filename);
string json = "";
string str;
while (getline(file, str))
{
json += str;
}
return parse_json(json);
}
vector<vector<string>> parse_json(string json) {
vector<vector<string>> clusterSet;
vector<string> clusters = split(',', json);
for(string cluster : clusters) {
vector<string> itemVal = split(':', cluster);
string allValues = itemVal[1];
allValues.erase (remove(allValues.begin(), allValues.end(), '\"'), allValues.end());
allValues.erase (remove(allValues.begin(), allValues.end(), '}'), allValues.end());
vector<string> items = split(' ', allValues);
items = removeWhitespaceEntries(items);
sort(items.begin(), items.end());
clusterSet.push_back(items);
}
return clusterSet;
}
vector<string> removeWhitespaceEntries(vector<string> items) {
vector<string> items_no_ws;
for(unsigned int i=0; i<items.size(); i++) {
if(items[i].find_first_not_of(' ') != std::string::npos)
{
items_no_ws.push_back(items[i]);
}
}
return items_no_ws;
}
vector<string> split(char delimiter, string str) {
vector<string> chunks;
string token;
size_t pos = 0;
size_t prevPos = 0;
while ((pos = str.find(delimiter, prevPos)) != string::npos) {
token = str.substr(prevPos, pos-prevPos);
chunks.push_back(token);
prevPos = pos +1;
}
token = str.substr(prevPos, str.length());
chunks.push_back(token);
return chunks;
}