Patch from Scott McKellar:
[OpenSRF.git] / src / libopensrf / osrf_legacy_json.c
index 69b9867..17dab25 100644 (file)
@@ -22,11 +22,11 @@ GNU General Public License for more details.
 int current_strlen; 
 
 
-jsonObject* legacy_jsonParseString( char* string) {
-       return json_parse_string( string );
+jsonObject* legacy_jsonParseString( const char* string) {
+       return json_parse_string( (char*) string );
 }
 
-jsonObject* legacy_jsonParseStringFmt( char* string, ... ) {
+jsonObject* legacy_jsonParseStringFmt( const char* string, ... ) {
        VA_LIST_TO_STRING(string);
        return json_parse_string( VA_BUF );
 }
@@ -94,12 +94,13 @@ int _json_parse_string(char* string, unsigned long* index, jsonObject* obj, int
                /* json string */
                case '"': 
                        (*index)++;
-                       status = json_parse_json_string(string, index, obj, current_strlen); break;
+                       status = json_parse_json_string(string, index, obj, current_strlen);
+                       break;
 
                /* json array */
                case '[':
                        (*index)++;
-                       status = json_parse_json_array(string, index, obj, current_strlen);                     
+                       status = json_parse_json_array(string, index, obj, current_strlen);
                        break;
 
                /* json object */
@@ -125,7 +126,7 @@ int _json_parse_string(char* string, unsigned long* index, jsonObject* obj, int
 
                default:
                        if(isdigit(c) || c == '.' || c == '-') { /* are we a number? */
-                               status = json_parse_json_number(string, index, obj, current_strlen);    
+                               status = json_parse_json_number(string, index, obj, current_strlen);
                                if(status) return status;
                                break;
                        }
@@ -244,8 +245,7 @@ int json_parse_json_number(char* string, unsigned long* index, jsonObject* obj,
        }
 
        obj->type = JSON_NUMBER;
-       obj->value.n = strtod(buf->buf, NULL);
-       buffer_free(buf);
+       obj->value.s = buffer_release(buf);
        return 0;
 }
 
@@ -478,17 +478,11 @@ int json_parse_json_string(char* string, unsigned long* index, jsonObject* obj,
                                                return json_handle_error(string, index,
                                                        "json_parse_json_string(): truncated escaped unicode"); }
 
-                                       char buff[5];
-                                       memset(buff,0,5);
-                                       memcpy(buff, string + (*index), 4);
-
-
                                        /* ----------------------------------------------------------------------- */
                                        /* ----------------------------------------------------------------------- */
                                        /* The following chunk was borrowed with permission from 
                                                json-c http://oss.metaparadigm.com/json-c/ */
-                                       unsigned char utf_out[3];
-                                       memset(utf_out,0,3);
+                                       unsigned char utf_out[3] = { '\0', '\0', '\0' };
 
                                        #define hexdigit(x) ( ((x) <= '9') ? (x) - '0' : ((x) & 7) + 9)
 
@@ -692,12 +686,14 @@ int json_eat_comment(char* string, unsigned long* index, char** buffer, int pars
 int json_handle_error(char* string, unsigned long* index, char* err_msg) {
 
        char buf[60];
-       memset(buf, 0, 60);
+       osrf_clearbuf(buf, sizeof(buf));
 
        if(*index > 30)
-               strncpy( buf, string + (*index - 30), 59 );
+               strncpy( buf, string + (*index - 30), sizeof(buf) - 1 );
        else
-               strncpy( buf, string, 59 );
+               strncpy( buf, string, sizeof(buf) - 1 );
+
+       buf[ sizeof(buf) - 1 ] = '\0';
 
        fprintf(stderr, 
                        "\nError parsing json string at charracter %c "
@@ -743,18 +739,7 @@ char* legacy_jsonObjectToJSON( const jsonObject* obj ) {
                        break;
 
                case JSON_NUMBER: {
-                       double x = obj->value.n;
-
-                       /* if the number does not need to be a double,
-                               turn it into an int on the way out */
-                       if( x == (int) x ) {
-                               INT_TO_STRING((int)x);  
-                               buffer_add(buf, INTSTR);
-
-                       } else {
-                               DOUBLE_TO_STRING(x);
-                               buffer_add(buf, DOUBLESTR);
-                       }
+                       buffer_add(buf, obj->value.s);
                        break;
                }