diff --git a/jsoncat.c b/jsoncat.c index 5e614ffc..67e80c31 100644 --- a/jsoncat.c +++ b/jsoncat.c @@ -42,16 +42,15 @@ void json_print(json_object *j, int depth) { printf("{\n"); indent(depth + 1); - json_hash *h = j->hash; - while (h != NULL) { - json_print(h->key, depth + 1); + int i; + for (i = 0; i < j->length; i++) { + json_print(j->keys[i], depth + 1); printf(" : "); - json_print(h->value, depth + 1); - if (h->next != NULL) { + json_print(j->values[i], depth + 1); + if (i + 1 < j->length) { printf(",\n"); indent(depth + 1); } - h = h->next; } printf("\n"); indent(depth); @@ -60,14 +59,13 @@ void json_print(json_object *j, int depth) { printf("[\n"); indent(depth + 1); - json_array *a = j->array; - while (a != NULL) { - json_print(a->object, depth + 1); - if (a->next != NULL) { + int i; + for (i = 0; i < j->length; i++) { + json_print(j->array[i], depth + 1); + if (i + 1 < j->length) { printf(",\n"); indent(depth + 1); } - a = a->next; } printf("\n"); indent(depth); diff --git a/jsonpull.c b/jsonpull.c index 87f10103..80433e22 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -18,20 +18,25 @@ static json_object *add_object(json_type type, json_object *parent) { o->type = type; o->parent = parent; o->array = NULL; - o->hash = NULL; + o->keys = NULL; + o->values = NULL; + o->length = 0; + o->__allocated = 0; if (parent != NULL) { if (parent->type == JSON_ARRAY) { - json_array *a = malloc(sizeof(json_array)); - a->next = parent->array; - a->object = o; - parent->array = a; + if (parent->length >= parent->__allocated) { + parent->__allocated += 20; + parent->array = realloc(parent->array, parent->__allocated * sizeof(json_object *)); + } + + parent->array[parent->length++] = o; } if (parent->type == JSON_HASH) { - if (parent->hash != NULL && parent->hash->value == NULL) { + if (parent->length > 0 && parent->values[parent->length - 1] == NULL) { // Hash has key but no value, so this is the value - parent->hash->value = o; + parent->values[parent->length - 1] = o; } else { // No current hash, so this is a key @@ -39,11 +44,15 @@ static json_object *add_object(json_type type, json_object *parent) { json_error("Hash key is not a string"); } - json_hash *h = malloc(sizeof(json_hash)); - h->next = parent->hash; - h->key = o; - h->value = NULL; - parent->hash = h; + if (parent->length >= parent->__allocated) { + parent->__allocated += 20; + parent->keys = realloc(parent->keys, parent->__allocated * sizeof(json_object *)); + parent->values = realloc(parent->values, parent->__allocated * sizeof(json_object *)); + } + + parent->keys[parent->length] = o; + parent->values[parent->length] = NULL; + parent->length++; } } } @@ -56,11 +65,11 @@ json_object *json_hash_get(json_object *o, char *s) { return NULL; } - json_hash *h; - for (h = o->hash; h != NULL; h = h->next) { - if (h->key != NULL && h->key->type == JSON_STRING) { - if (strcmp(h->key->string, s) == 0) { - return h->value; + int i; + for (i = 0; i < o->length; i++) { + if (o->keys[i] != NULL && o->keys[i]->type == JSON_STRING) { + if (strcmp(o->keys[i]->string, s) == 0) { + return o->values[i]; } } } @@ -125,7 +134,7 @@ json_object *json_parse(FILE *f, json_object *current) { json_error("] at top level"); } - if (current->type == JSON_ARRAY && current->array == NULL) { + if (current->type == JSON_ARRAY && current->length == 0) { return current; // Empty array } @@ -145,14 +154,14 @@ json_object *json_parse(FILE *f, json_object *current) { json_error("} at top level"); } - if (current->type == JSON_HASH && current->hash == NULL) { + if (current->type == JSON_HASH && current->length == 0) { return current; // Empty hash } if (current->parent == NULL || current->parent->type != JSON_HASH) { json_error("} without {\n"); } - if (current->parent->hash != NULL && current->parent->hash->value == NULL) { + if (current->parent->length != 0 && current->parent->values[current->parent->length - 1] == NULL) { json_error("} without hash value\n"); } @@ -215,7 +224,7 @@ json_object *json_parse(FILE *f, json_object *current) { if (current->parent == NULL || current->parent->type != JSON_HASH) { json_error(": not in hash\n"); } - if (current->parent->hash == NULL || current->parent->hash->key == NULL) { + if (current->parent->length == 0 || current->parent->keys[current->parent->length - 1] == NULL) { json_error(": without key\n"); } diff --git a/jsonpull.h b/jsonpull.h index ff15ff43..08f36503 100644 --- a/jsonpull.h +++ b/jsonpull.h @@ -8,25 +8,18 @@ typedef enum json_type { JSON_HASH, JSON_ARRAY, JSON_NUMBER, JSON_STRING, JSON_TRUE, JSON_FALSE, JSON_NULL, } json_type; -typedef struct json_array { - struct json_object *object; - struct json_array *next; -} json_array; - -typedef struct json_hash { - struct json_object *key; - struct json_object *value; - struct json_hash *next; -} json_hash; - typedef struct json_object { json_type type; struct json_object *parent; char *string; double number; - json_array *array; - json_hash *hash; + + struct json_object **array; + struct json_object **keys; + struct json_object **values; + int length; + int __allocated; } json_object; json_object *json_parse(FILE *f, json_object *current);