Detect attempts to attach an element to a non-container parent.

Maybe there is a better name for this. The typical case is adjacent
tokens not separated by a comma.
This commit is contained in:
Eric Fischer 2014-02-06 12:51:32 -08:00
parent ad2843d65b
commit 6449e817b6

View File

@ -23,9 +23,7 @@ static json_object *add_object(json_type type, json_object *parent, char **error
} }
parent->array[parent->length++] = o; parent->array[parent->length++] = o;
} } else if (parent->type == JSON_HASH) {
if (parent->type == JSON_HASH) {
if (parent->length > 0 && parent->values[parent->length - 1] == NULL) { if (parent->length > 0 && parent->values[parent->length - 1] == NULL) {
// Hash has key but no value, so this is the value // Hash has key but no value, so this is the value
@ -35,6 +33,8 @@ static json_object *add_object(json_type type, json_object *parent, char **error
if (type != JSON_STRING) { if (type != JSON_STRING) {
*error = "Hash key is not a string"; *error = "Hash key is not a string";
free(o);
return NULL;
} }
if (SIZE_FOR(parent->length + 1) != SIZE_FOR(parent->length)) { if (SIZE_FOR(parent->length + 1) != SIZE_FOR(parent->length)) {
@ -46,6 +46,10 @@ static json_object *add_object(json_type type, json_object *parent, char **error
parent->values[parent->length] = NULL; parent->values[parent->length] = NULL;
parent->length++; parent->length++;
} }
} else {
*error = "Parent is not a container";
free(o);
return NULL;
} }
} }
@ -124,6 +128,9 @@ again:
if (c == '[') { if (c == '[') {
current = add_object(JSON_ARRAY, current, error); current = add_object(JSON_ARRAY, current, error);
if (current == NULL) {
return NULL;
}
current_is = '['; current_is = '[';
goto again; goto again;
} else if (c == ']') { } else if (c == ']') {
@ -151,6 +158,9 @@ again:
if (c == '{') { if (c == '{') {
current = add_object(JSON_HASH, current, error); current = add_object(JSON_HASH, current, error);
if (current == NULL) {
return NULL;
}
current_is = '{'; current_is = '{';
goto again; goto again;
} else if (c == '}') { } else if (c == '}') {
@ -308,8 +318,10 @@ again:
} }
json_object *n = add_object(JSON_NUMBER, current, error); json_object *n = add_object(JSON_NUMBER, current, error);
if (n != NULL) {
n->number = atof(val.buf); n->number = atof(val.buf);
string_free(&val); string_free(&val);
}
return n; return n;
} }
@ -369,7 +381,9 @@ again:
} }
json_object *s = add_object(JSON_STRING, current, error); json_object *s = add_object(JSON_STRING, current, error);
if (s != NULL) {
s->string = val.buf; s->string = val.buf;
}
return s; return s;
} }