From: erickson Date: Tue, 1 Mar 2005 23:50:29 +0000 (+0000) Subject: added json pretty printer for formatting result content data X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=2e5a117d7827eed93d2f5267b3f901f4c464a1b9;p=opensrf%2Fbjwebb.git added json pretty printer for formatting result content data git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@145 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/srfsh/srfsh.c b/src/srfsh/srfsh.c index 36175df..fa3043e 100644 --- a/src/srfsh/srfsh.c +++ b/src/srfsh/srfsh.c @@ -21,6 +21,8 @@ int parse_error( char* words[] ); int router_query_servers( char* server ); int srfsh_client_connect(); void print_help(); +char* json_printer( json* object ); +char* tabs(int count); int main( int argc, char* argv[] ) { @@ -231,8 +233,11 @@ int send_request( char* server, char* method, growing_buffer* buffer ) { while(omsg) { - if(omsg->result_content) - printf( "Received Data: %s\n",json_object_to_json_string(omsg->result_content) ); + if(omsg->result_content) { + char* content = json_printer( omsg->result_content ); + printf( "Received Data: %s\n",content ); + free(content); + } else printf( "Received Message but no result data\n"); @@ -334,3 +339,94 @@ void print_help() { ); } + + + +char* tabs(int count) { + growing_buffer* buf = buffer_init(24); + int i; + for(i=0;i!=count;i++) + buffer_add(buf, " "); + + char* final = buffer_data( buf ); + buffer_free( buf ); + return final; +} + +char* json_printer( json* object ) { + + if(object == NULL) + return NULL; + char* string = json_object_to_json_string(object); + + growing_buffer* buf = buffer_init(64); + int i; + int tab_var = 0; + for(i=0; i!= strlen(string); i++) { + + if( string[i] == '{' ) { + + buffer_add(buf, "\n"); + char* tab = tabs(tab_var); + buffer_add(buf, tab); + free(tab); + buffer_add( buf, "{"); + tab_var++; + buffer_add( buf, "\n" ); + tab = tabs(tab_var); + buffer_add( buf, tab ); + free(tab); + + } else if( string[i] == '[' ) { + + buffer_add(buf, "\n"); + char* tab = tabs(tab_var); + buffer_add(buf, tab); + free(tab); + buffer_add( buf, "["); + tab_var++; + buffer_add( buf, "\n" ); + tab = tabs(tab_var); + buffer_add( buf, tab ); + free(tab); + + } else if( string[i] == '}' ) { + + tab_var--; + buffer_add(buf, "\n"); + char* tab = tabs(tab_var); + buffer_add(buf, tab); + free(tab); + buffer_add( buf, "}"); + buffer_add( buf, "\n" ); + tab = tabs(tab_var); + buffer_add( buf, tab ); + free(tab); + + } else if( string[i] == ']' ) { + + tab_var--; + buffer_add(buf, "\n"); + char* tab = tabs(tab_var); + buffer_add(buf, tab); + free(tab); + buffer_add( buf, "]"); + buffer_add( buf, "\n" ); + tab = tabs(tab_var); + buffer_add( buf, tab ); + free(tab); + + } else { + char b[2]; + b[0] = string[i]; + b[1] = '\0'; + buffer_add( buf, b ); + } + + } + + char* result = buffer_data(buf); + buffer_free(buf); + return result; + +}