From 24f0bcc782cfb249c57fdc5a07bf3352356b4864 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 24 Aug 2005 13:40:10 +0000 Subject: [PATCH] made json formatter a little smarter/better srfsh now uses json_format_string found in the json code added a 'clone' method to the json code git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@499 9efc2488-bf62-4759-914b-345cdb29e865 --- src/objson/object.c | 87 +++++++++++++++++--------------------------------- src/objson/object.h | 2 ++ src/srfsh/srfsh.c | 91 +++-------------------------------------------------- src/srfsh/srfsh.h | 1 - 4 files changed, 35 insertions(+), 146 deletions(-) diff --git a/src/objson/object.c b/src/objson/object.c index baef3d0..ff41eaa 100644 --- a/src/objson/object.c +++ b/src/objson/object.c @@ -544,7 +544,7 @@ void object_set_comment(object* obj, char* com) { char* __tabs(int count) { growing_buffer* buf = buffer_init(24); int i; - for(i=0;i!=count;i++) buffer_add(buf, " "); + for(i=0;i!=count;i++) buffer_add(buf, " "); char* final = buffer_data( buf ); buffer_free( buf ); return final; @@ -552,79 +552,39 @@ char* __tabs(int count) { char* json_string_format(char* string) { + if(!string) return strdup(""); + 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, " {"); - buffer_add(buf, "\n"); - char* tab = __tabs(tab_var); - buffer_add(buf, tab); - free(tab); + int depth = 0; - tab_var++; - buffer_add( buf, "\n" ); - tab = __tabs(tab_var); - buffer_add( buf, tab ); - free(tab); + for(i=0; i!= strlen(string); i++) { - } else if( string[i] == '[' ) { + if( string[i] == '{' || string[i] == '[' ) { - buffer_add( buf, "["); - buffer_add(buf, "\n"); - char* tab = __tabs(tab_var); - buffer_add(buf, tab); - free(tab); - tab_var++; - buffer_add( buf, "\n" ); - tab = __tabs(tab_var); - buffer_add( buf, tab ); + char* tab = __tabs(++depth); + buffer_fadd( buf, "%c\n%s", string[i], tab); free(tab); - } else if( string[i] == '}' ) { + } else if( string[i] == '}' || string[i] == ']' ) { - tab_var--; - buffer_add(buf, "\n"); - char* tab = __tabs(tab_var); - buffer_add(buf, tab); + char* tab = __tabs(--depth); + buffer_fadd( buf, "\n%s%c", tab, string[i]); 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); + if(string[i+1] != ',') { + tab = __tabs(depth); + buffer_fadd( buf, "\n%s", tab ); + free(tab); + } } else if( string[i] == ',' ) { - buffer_add( buf, ","); - buffer_add( buf, "\n" ); - char* tab = __tabs(tab_var); - buffer_add(buf, tab); + char* tab = __tabs(depth); + buffer_fadd(buf, ",\n%s", tab); free(tab); - } else { - - char b[2]; - b[0] = string[i]; - b[1] = '\0'; - buffer_add( buf, b ); - } + } else { buffer_add_char(buf, string[i]); } } @@ -635,6 +595,15 @@ char* json_string_format(char* string) { } +object* object_clone(object* o) { + if(!o) return NULL; + char* json = o->to_json(o); + object* newo = json_parse_string(json); + free(json); + return newo; +} + + /* ---------------------------------------------------------------------- */ /* Iterator */ diff --git a/src/objson/object.h b/src/objson/object.h index 5b5a28b..f9925c2 100644 --- a/src/objson/object.h +++ b/src/objson/object.h @@ -216,5 +216,7 @@ void object_shift_index(object*, unsigned long index); /* formats a JSON string from printing. User must free returned string */ char* json_string_format(char* json); +object* object_clone(object* o); + #endif diff --git a/src/srfsh/srfsh.c b/src/srfsh/srfsh.c index d1cdf01..47ff5a1 100644 --- a/src/srfsh/srfsh.c +++ b/src/srfsh/srfsh.c @@ -567,7 +567,8 @@ int send_request( char* server, if( pretty_print && omsg->_result_content ) { char* j = object_to_json(omsg->_result_content); - content = json_printer(j); + //content = json_printer(j); + content = json_string_format(j); free(j); } else content = object_get_string(omsg->_result_content); @@ -599,7 +600,8 @@ int send_request( char* server, if( pretty_print && omsg->_result_content ) { char* j = object_to_json(omsg->_result_content); - content = json_printer(j); + //content = json_printer(j); + content = json_string_format(j); free(j); } else content = object_get_string(omsg->_result_content); @@ -772,96 +774,13 @@ char* tabs(int count) { growing_buffer* buf = buffer_init(24); int i; for(i=0;i!=count;i++) - buffer_add(buf, " "); + buffer_add(buf, " "); char* final = buffer_data( buf ); buffer_free( buf ); return final; } -char* json_printer( char* string ) { - - 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 if( string[i] == ',' ) { - - buffer_add( buf, ","); - buffer_add( buf, "\n" ); - char* 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; - -} - int handle_math( char* words[] ) { if( words[1] && words[2] ) return do_math( atoi(words[1]), atoi(words[2]) ); diff --git a/src/srfsh/srfsh.h b/src/srfsh/srfsh.h index 28a62c0..5f2d94f 100644 --- a/src/srfsh/srfsh.h +++ b/src/srfsh/srfsh.h @@ -64,7 +64,6 @@ int parse_error( char* words[] ); int router_query_servers( char* server ); int srfsh_client_connect(); int print_help(); -char* json_printer( char* string ); char* tabs(int count); void sig_child_handler( int s ); void sig_int_handler( int s ); -- 2.11.0