Hello. There is a string of JSON type:

'users'=(('id'=10; 'name'='Serge'; 'roles'=('visitor'; 'moderator' )); ('id'=11; 'name'='Biales' ); true ) 

and you need to make it like this:

 'users'= ( ( 'id'=10; 'name'='Serge'; 'roles'= ( 'visitor'; 'moderator' ) ); ( 'id'=11; 'name'='Biales' ); true ) 

tell me the algorithm or even tell me)

  • That is just formatted? Then see the sources of the corresponding utilities. There are many of them, AStyle , for example, is just a plus. - PinkTux
  • I need to write this in the code to write a function on c ++ myself. - Maxim Bondarenko
  • Specify. If you just indent - clearly state the conditions (by brackets, somehow, etc.) Then maybe it will do with one function :) - PinkTux

1 answer 1

Set a variable for the indent level. You meet ( - increase it, go to a new line, indent for each next line 4 * level (or how much you need there).
You meet ) - output it on a new line with the old indentation, reduce the variable by one.

Like that.

Sample code (clean it yourself):

 char * s = R"aa('users'=(('id'=10; 'name'='Serge'; 'roles'=('visitor'; 'moderator' )); ('id'=11; 'name'='Biales' ); true ))aa"; void space(int level) { const int ident = 4; for(int i = 0; i < level * ident; ++i) cout << ' '; } int main(int argc, const char * argv[]) { int level = 0; for(char * c = s; *c; ++c) { switch(*c) { case '(': cout << '\n'; space(level++); cout << "(\n"; space(level); break; case ')': cout << '\n'; space(--level); cout << ')'; break; case '\n': cout << '\n'; space(level); break; default: cout << *c; } } } 
  • and what does it mean or does - "R" aa ("? I haven’t seen it yet - Maxim Bondarenko
  • @MaximBondarenko, this is a new form of writing string literals, which appeared in C ++ 11. - Embedder Nov.
  • @MaximBondarenko See, for example, about raw literals here or here . - Harry