mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 08:25:40 +02:00
Attach child objects to the containers
This commit is contained in:
@@ -24,13 +24,36 @@ typedef struct json_object {
|
|||||||
char *string;
|
char *string;
|
||||||
double number;
|
double number;
|
||||||
json_array *array;
|
json_array *array;
|
||||||
json_hash *object;
|
json_hash *hash;
|
||||||
} json_object;
|
} json_object;
|
||||||
|
|
||||||
json_object *new_object(json_type type, json_object *parent) {
|
static json_object *add_object(json_type type, json_object *parent) {
|
||||||
json_object *o = malloc(sizeof(struct json_object));
|
json_object *o = malloc(sizeof(struct json_object));
|
||||||
o->type = type;
|
o->type = type;
|
||||||
o->parent = parent;
|
o->parent = parent;
|
||||||
|
o->array = NULL;
|
||||||
|
o->hash = NULL;
|
||||||
|
|
||||||
|
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->type == JSON_HASH) {
|
||||||
|
if (parent->hash != NULL && parent->hash->value == NULL) {
|
||||||
|
parent->hash->value = o;
|
||||||
|
} else {
|
||||||
|
json_hash *h = malloc(sizeof(json_hash));
|
||||||
|
h->next = parent->hash;
|
||||||
|
h->key = o;
|
||||||
|
parent->hash = h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +76,7 @@ json_object *parse(FILE *f, json_object *current) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (c == '[') {
|
if (c == '[') {
|
||||||
return parse(f, new_object(JSON_ARRAY, current));
|
return parse(f, add_object(JSON_ARRAY, current));
|
||||||
} else if (c == ']') {
|
} else if (c == ']') {
|
||||||
if (current->parent == NULL || current->parent->type != JSON_ARRAY) {
|
if (current->parent == NULL || current->parent->type != JSON_ARRAY) {
|
||||||
json_error("] without [");
|
json_error("] without [");
|
||||||
@@ -63,7 +86,7 @@ json_object *parse(FILE *f, json_object *current) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (c == '{') {
|
if (c == '{') {
|
||||||
return parse(f, new_object(JSON_HASH, current));
|
return parse(f, add_object(JSON_HASH, current));
|
||||||
} else if (c == '}') {
|
} else if (c == '}') {
|
||||||
if (current->parent == NULL || current->parent->type != JSON_HASH) {
|
if (current->parent == NULL || current->parent->type != JSON_HASH) {
|
||||||
json_error("} without {");
|
json_error("} without {");
|
||||||
@@ -77,7 +100,7 @@ json_object *parse(FILE *f, json_object *current) {
|
|||||||
json_error("misspelled null");
|
json_error("misspelled null");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_object(JSON_NULL, current);
|
return add_object(JSON_NULL, current);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == 't') {
|
if (c == 't') {
|
||||||
@@ -85,7 +108,7 @@ json_object *parse(FILE *f, json_object *current) {
|
|||||||
json_error("misspelled true");
|
json_error("misspelled true");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_object(JSON_TRUE, current);
|
return add_object(JSON_TRUE, current);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == 'f') {
|
if (c == 'f') {
|
||||||
@@ -93,7 +116,7 @@ json_object *parse(FILE *f, json_object *current) {
|
|||||||
json_error("misspelled false");
|
json_error("misspelled false");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_object(JSON_FALSE, current);
|
return add_object(JSON_FALSE, current);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == ',') {
|
if (c == ',') {
|
||||||
@@ -110,6 +133,9 @@ json_object *parse(FILE *f, json_object *current) {
|
|||||||
if (current->parent == NULL || current->parent->type != JSON_HASH) {
|
if (current->parent == NULL || current->parent->type != JSON_HASH) {
|
||||||
json_error(": not in hash");
|
json_error(": not in hash");
|
||||||
}
|
}
|
||||||
|
if (current->parent->hash == NULL || current->parent->hash->value != NULL) {
|
||||||
|
json_error(": without key");
|
||||||
|
}
|
||||||
|
|
||||||
return parse(f, current);
|
return parse(f, current);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user