From: erickson Date: Tue, 23 Aug 2005 15:08:44 +0000 (+0000) Subject: added the pretty printer here X-Git-Tag: osrf_rel_2_0_1~1375 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=9a98a8cb4bc95ff5ceb645ea1a7be0c9e67c3de1;p=OpenSRF.git added the pretty printer here git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@498 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/objson/object.c b/src/objson/object.c index 8a8babe..baef3d0 100644 --- a/src/objson/object.c +++ b/src/objson/object.c @@ -23,6 +23,9 @@ GNU General Public License for more details. /* See object.h for function info */ /* ---------------------------------------------------------------------- */ + +char* __tabs(int count); + object* new_object(char* string_value) { return _init_object(string_value); } @@ -461,12 +464,14 @@ char* object_to_json(object* obj) { int i; for( i = 0; i!= obj->size; i++ ) { char* data = object_to_json(obj->get_index(obj,i)); + #ifdef STRICT_JSON_WRITE buffer_add(buf, data); #else if(strcmp(data,"null")) /* only add the string if it isn't null */ buffer_add(buf, data); #endif + free(data); if(i != obj->size - 1) buffer_add(buf, ","); @@ -474,9 +479,11 @@ char* object_to_json(object* obj) { buffer_add(buf, "]"); } else if(obj->is_hash) { + buffer_add(buf, "{"); object_iterator* itr = new_iterator(obj); object_node* tmp; + while( (tmp = itr->next(itr)) ) { buffer_add(buf, "\""); buffer_add(buf, tmp->key); @@ -534,6 +541,100 @@ 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, " "); + char* final = buffer_data( buf ); + buffer_free( buf ); + return final; +} + +char* json_string_format(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, " {"); + 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 ); + free(tab); + + } else if( 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 ); + 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; + +} + + /* ---------------------------------------------------------------------- */ /* Iterator */ diff --git a/src/objson/object.h b/src/objson/object.h index 0f6dd4b..5b5a28b 100644 --- a/src/objson/object.h +++ b/src/objson/object.h @@ -31,7 +31,7 @@ GNU General Public License for more details. #ifndef OBJECT_H #define OBJECT_H -/* top level generic object structuure */ +/* top level generic object structure */ struct object_struct { /* how many sub-objects do we contain. Note that this includes null @@ -213,6 +213,8 @@ void object_set_comment(object*, char*); */ 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); #endif