Last active
May 25, 2019 05:39
-
-
Save berserker1/a1072689c4818771bd58830379eb0b58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Test case you can try ["foo", {"bar":["baz",null,1.0,2]}] | |
//Try another examples also | |
#include<bits/stdc++.h> | |
using namespace std; | |
int prettyjson(string s,stack<char> st,int length,int i,int tabs) | |
{ | |
if(i<length) | |
{ | |
if((s[i]=='[') || (s[i]=='{')) | |
{ | |
for(int k=0;k<tabs;k++) | |
{ | |
printf("\t"); | |
} | |
printf("%c\n",s[i]); | |
tabs++; | |
i++; | |
st.push(s[i]); | |
prettyjson(s,st,length,i,tabs); | |
} | |
else if((s[i]=='}') || (s[i]==']')) | |
{ | |
st.pop(); | |
for(int k=0;k<tabs-1;k++) | |
{ | |
printf("\t"); | |
} | |
printf("%c\n",s[i]); | |
tabs--; | |
i++; | |
//printf("%d %d %d %ld\n",i,length,tabs,st.size()); | |
prettyjson(s,st,length,i,tabs); | |
} | |
else | |
{ | |
string current; | |
for(int k=0;k<tabs;k++) | |
{ | |
printf("\t"); | |
} | |
for(;(s[i]!='{') && (s[i]!='[') && (s[i]!='}') && (s[i]!=']');i++) | |
{ | |
current.push_back(s[i]); | |
} | |
int l = current.length(); | |
int io = -1; | |
for(int k=0;k<l;k++) | |
{ | |
if(current[k] == ' ') | |
{ | |
io = k; | |
break; | |
} | |
} | |
if(io>=0) | |
{ | |
current.erase(current.begin() + io); | |
} | |
l = current.length(); | |
for(int k=0;k<l;k++) | |
{ | |
if(current[k]==',') | |
{ | |
printf("%c\n",current[k]); | |
if(k!=l-1) | |
{ | |
for(int h=0;h<tabs;h++) | |
{ | |
printf("\t"); | |
} | |
} | |
} | |
else | |
{ | |
if(k==l-1) | |
{ | |
printf("%c\n",current[k]); | |
} | |
else | |
{ | |
printf("%c",current[k]); | |
} | |
} | |
} | |
//printf("\n"); | |
// cout << current << endl; | |
current.erase(); | |
prettyjson(s,st,length,i,tabs); | |
} | |
} | |
else | |
{ | |
return 0; | |
} | |
return 0; | |
} | |
int main() | |
{ | |
string s; | |
getline(cin,s); | |
int length = s.length(); | |
//cout << length << endl; | |
stack <char> st; | |
string current; | |
prettyjson(s,st,length,0,0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment