# --------------------------------------------------------------------
+# --------------------------------------------------------------------
+#
+# -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
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;
}
}
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;
}
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) {
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);
/* 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;
}
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, ",");
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);
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);
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);