From: scottmk Date: Mon, 5 Oct 2009 02:01:39 +0000 (+0000) Subject: Tweak jsonObjectDecodeClass so that it preserves classnames X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=145d6ca1b5df895dffd4555718876bdad124e5c2;p=working%2FOpenSRF.git Tweak jsonObjectDecodeClass so that it preserves classnames that are already present. The previous version would add new classnames, if it found them encoded, but would drop any old ones. At present this change will have no effect. Apart from a couple of recursive calls, the only function that calls jsonObjectDecodeClass() is jsonParseString(). In that case, we pass a freshly parsed jsonObject that can't possibly have any classnames yet. However this change will enable us to use this function elsewhere (specifically in osrf_message.c), resulting in simpler and faster code. M src/libopensrf/osrf_json_tools.c git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1805 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/libopensrf/osrf_json_tools.c b/src/libopensrf/osrf_json_tools.c index 475f5de..d8e0a8f 100644 --- a/src/libopensrf/osrf_json_tools.c +++ b/src/libopensrf/osrf_json_tools.c @@ -21,6 +21,13 @@ static jsonObject* findMultiPath( const jsonObject* o, static jsonObject* findMultiPathRecurse( const jsonObject* o, const char* root ); static jsonObject* _jsonObjectEncodeClass( const jsonObject* obj, int ignoreClass ); +/** + @brief Append some spaces to a growing_buffer, for indentation. + @param buf Pointer to the growing_buffer. + @param depth Degree of indentation. + + We append 2 spaces per degree of indentation. +*/ static void append_indentation( growing_buffer* buf, int depth ) { size_t n = 2 * depth; char indent[ n ]; @@ -28,6 +35,18 @@ static void append_indentation( growing_buffer* buf, int depth ) { buffer_add_n( buf, indent, n ); } +/** + @brief Make a prettyprint copy of a JSON string. + @param string Pointer to the JSON string to be formatted. + @return Pointer to a newly allocated and reformatted JSON string. + + Create a copy of the input JSON string, with newlines and + indentation for readability. + + If the input pointer is NULL, return an empty string. + + The calling code is responsible for freeing the formatted copy. +*/ char* jsonFormatString( const char* string ) { if(!string) return strdup(""); @@ -81,7 +100,7 @@ jsonObject* jsonObjectDecodeClass( const jsonObject* obj ) { /* do we have a payload */ if( (payloadObj = jsonObjectGetKeyConst( obj, JSON_DATA_KEY )) ) { - newObj = jsonObjectDecodeClass( payloadObj ); + newObj = jsonObjectDecodeClass( payloadObj ); jsonObjectSetClass( newObj, jsonObjectGetString(classObj) ); } else { /* class is defined but there is no payload */ @@ -98,6 +117,8 @@ jsonObject* jsonObjectDecodeClass( const jsonObject* obj ) { jsonObjectSetKey( newObj, itr->key, o ); } jsonIteratorFree(itr); + if( obj->classname ) + jsonObjectSetClass( newObj, obj->classname ); } } else { @@ -108,6 +129,8 @@ jsonObject* jsonObjectDecodeClass( const jsonObject* obj ) { jsonObject* tmp = jsonObjectDecodeClass(jsonObjectGetIndex( obj, i ) ); jsonObjectSetIndex( newObj, i, tmp ); } + if( obj->classname ) + jsonObjectSetClass( newObj, obj->classname ); } else { /* not an aggregate type */ newObj = jsonObjectClone(obj);