forked from black-shadows/InterviewBit-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrettyJson.cpp
More file actions
64 lines (55 loc) · 1.73 KB
/
PrettyJson.cpp
File metadata and controls
64 lines (55 loc) · 1.73 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
vector<string> kirp(string str){
vector<string> result;
if (str.length() == 0) return result;
str.erase(remove(str.begin(), str.end(), ' '), str.end());
string indent = "";
string curr = "";
int i = 0, len = str.length();
while (i < len){
curr.push_back(str[i]);
switch(str[i]){
case ',':
result.push_back(curr);
curr = indent;
i++;
break;
case ':':
i++;
if (str[i] == '{' || str[i] == '['){
result.push_back(curr);
curr = indent;
}
break;
case '{':
case '[':
i++;
result.push_back(curr);
if (i < len && (str[i] != '}' || str[i] != ']')){
indent.push_back('\t');
}
curr = indent;
break;
case '}':
case ']':
i++;
if (i < len && str[i] == ',') break;
result.push_back(curr);
if (i < len && (str[i] == '}' || str[i] == ']')){
if (!indent.empty()) indent.pop_back();
}
curr = indent;
break;
default :
i++;
if (i < len && (str[i] == '}' || str[i] == ']')){
result.push_back(curr);
if (!indent.empty()) indent.pop_back();
curr = indent;
}
}
}
return result;
}
vector<string> Solution::prettyJSON(string A) {
return kirp(A);
}