made json formatter a little smarter/better
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 24 Aug 2005 13:40:10 +0000 (13:40 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 24 Aug 2005 13:40:10 +0000 (13:40 +0000)
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
src/objson/object.h
src/srfsh/srfsh.c
src/srfsh/srfsh.h

index baef3d0..ff41eaa 100644 (file)
@@ -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 */
index 5b5a28b..f9925c2 100644 (file)
@@ -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
index d1cdf01..47ff5a1 100644 (file)
@@ -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]) );
index 28a62c0..5f2d94f 100644 (file)
@@ -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 );