objson now accepts empty array and object values if STRICT_JSON_READ is not defined
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 15 Jul 2005 19:04:49 +0000 (19:04 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 15 Jul 2005 19:04:49 +0000 (19:04 +0000)
and writes null array and object values as empty strings if STRICT_JSON_WRITE is not defined

git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@398 9efc2488-bf62-4759-914b-345cdb29e865

src/objson/Makefile
src/objson/json_parser.c
src/objson/object.c
src/objson/objson_test.c

index 0d1caaf..639500c 100644 (file)
 # --------------------------------------------------------------------
 
 
+# --------------------------------------------------------------------
+#
+# -DSTRICT_JSON_WRITE if not defined, 'null' objects will be written
+#      as empty strings. e.g. [null, null] => [,,]
+#
+# -DSTRICT_JSON_READ if not defiend, empty array and object values
+#      (not keys) will be considered null.
+#      e.g. [,,{"key":}] will be parsed as [null, null, {"key":null}]
+#
+# --------------------------------------------------------------------
+
 OBJS                   = object.o json_parser.o utils.o
 UTIL_DIR               = ../utils
 DEST_INCLUDE = objson
+CC_OPTS                += #-DSTRICT_JSON_WRITE #-DSTRICT_JSON_READ
 
 all:   test
 
index 5e4c80a..db47fb5 100644 (file)
@@ -249,16 +249,20 @@ int json_parse_json_number(char* string, unsigned long* index, object* obj) {
 int json_parse_json_array(char* string, unsigned long* index, object* obj) {
        assert(string && obj && index && *index < current_strlen);
 
-       int status;
+       int status = 0;
        int in_parse = 0; /* true if this array already contains one item */
        obj->is_array = 1;
        obj->is_null = 0;
+       int set = 0;
+       int done = 0;
+
        while(*index < current_strlen) {
 
                json_eat_ws(string, index, 1);
 
                if(string[*index] == ']') {
                        (*index)++;
+                       done = 1;
                        break;
                }
 
@@ -273,13 +277,31 @@ int json_parse_json_array(char* string, unsigned long* index, object* obj) {
                }
 
                object* item = new_object(NULL);
-               status = _json_parse_string(string, index, item);
+
+#ifndef STRICT_JSON_READ
+               if(*index < current_strlen) {
+                       if(string[*index] == ',' || string[*index] == ']') {
+                               status = 0;
+                               set = 1;
+                       }
+               }
+               if(!set)
+                       status = _json_parse_string(string, index, item);
+
+#else
+                status = _json_parse_string(string, index, item);
+#endif
 
                if(status) return status;
                obj->push(obj, item);
                in_parse = 1;
+               set = 0;
        }
 
+       if(!done)
+               return json_handle_error(string, index,
+                       "json_parse_json_array(): array not closed");
+
        return 0;
 }
 
@@ -294,6 +316,8 @@ int json_parse_json_object(char* string, unsigned long* index, object* obj) {
        obj->is_null = 0;
        int status;
        int in_parse = 0; /* true if we've already added one item to this object */
+       int set = 0;
+       int done = 0;
 
        while(*index < current_strlen) {
 
@@ -301,13 +325,14 @@ int json_parse_json_object(char* string, unsigned long* index, object* obj) {
 
                if(string[*index] == '}') {
                        (*index)++;
+                       done = 1;
                        break;
                }
 
                if(in_parse) {
                        if(string[*index] != ',') {
                                return json_handle_error(string, index,
-                                       "json_parse_json_object(): object missing ',' betweenn elements" );
+                                       "json_parse_json_object(): object missing ',' between elements" );
                        }
                        (*index)++;
                        json_eat_ws(string, index, 1);
@@ -337,15 +362,34 @@ int json_parse_json_object(char* string, unsigned long* index, object* obj) {
                /* now grab the value object */
                json_eat_ws(string, index, 1);
                object* value_obj = new_object(NULL);
-               status = _json_parse_string(string, index, value_obj);
+
+#ifndef STRICT_JSON_READ
+               if(*index < current_strlen) {
+                       if(string[*index] == ',' || string[*index] == '}') {
+                               status = 0;
+                               set = 1;
+                       }
+               }
+               if(!set)
+                       status = _json_parse_string(string, index, value_obj);
+
+#else
+                status = _json_parse_string(string, index, value_obj);
+#endif
+
                if(status) return status;
 
                /* put the data into the object and continue */
                obj->add_key(obj, key, value_obj);
                free_object(key_obj);
                in_parse = 1;
+               set = 0;
        }
 
+       if(!done)
+               return json_handle_error(string, index,
+                       "json_parse_json_object(): object not closed");
+
        return 0;
 }
 
index 39babe4..8a8babe 100644 (file)
@@ -461,7 +461,12 @@ 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, ",");
@@ -477,7 +482,14 @@ char* object_to_json(object* obj) {
                        buffer_add(buf, tmp->key);
                        buffer_add(buf, "\":");
                        char* data =  object_to_json(tmp->item);
+
+#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
+
                        if(itr->has_next(itr))
                                buffer_add(buf, ",");
                        free(data);
index 19fe108..f3a5c39 100644 (file)
@@ -26,12 +26,24 @@ GNU General Public License for more details.
 int main() {
 
 
+
+       object* o;
+
+
+       printf("------------------------------------------------------------------\n");
+       o = json_parse_string("[,,{\"key\":,\"key1\":\"a\"}]");
+       char* h = o->to_json(o);
+       printf("\nParsed number: %s\n", h);
+       free_object(o);
+
+
        /* number, double, and 'null' parsing... */
        printf("------------------------------------------------------------------\n");
-       object* o = json_parse_string("1");
+        o = json_parse_string("1");
        printf("\nParsed number: %ld\n", o->num_value);
        free_object(o);
 
+
        printf("------------------------------------------------------------------\n");
        o = json_parse_string("1.1");
        printf("\nDouble number: %lf\n", o->double_value);
@@ -75,6 +87,11 @@ int main() {
 
 
        printf("------------------------------------------------------------------\n");
+       o = json_parse_string("{ Null: trUe }");
+
+
+
+       printf("------------------------------------------------------------------\n");
        o = json_parse_string("\"Pin\\u0303ata\"");
        s = o->to_json(o);
        printf("UNICODE:: %s\n", o->string_data);