#include #include #include #include #include "internal.h" enum json_parse_result parse_array(struct json **json_out, struct raw_json *raw, size_t depth) { assert(raw->data[raw->index] == '['); enum json_parse_result ret = JSON_PARSE_OK; struct json *json = NULL; struct json *obj = NULL; if (depth++ > 9) { ret = JSON_PARSE_MAX_DEPTH_ERR; goto err; } json = json_array(); if (!json) return JSON_OOM; do { raw->index++; skip_ws(raw); if (raw->data[raw->index] == ']') { if (json->children->nitems == 0) { break; } ret = JSON_PARSE_EXPECTED_VALUE_ERR; goto err; } ret = parse_value(&obj, raw, depth); if (ret != JSON_PARSE_OK) { goto err; } json_append(json, obj); skip_ws(raw); obj = NULL; } while (raw->index < raw->size && raw->data[raw->index] == ','); if (raw->data[raw->index] != ']') { ret = JSON_PARSE_INVALID_TOKEN_ERR; goto err; } raw->index++; *json_out = json; goto end; err: json_delete(json); json_delete(obj); end: return ret; } bool json_append(struct json *dest, struct json *src) { if (!dest || !src) return false; json_detach(src); add_children(dest, src); return true; } bool json_insert(struct json *dest, size_t index, struct json *src) { if (!dest || !src) return false; json_detach(src); if (index >= dest->children->nitems) { add_children(dest, src); return true; } if (!ensure_size(dest, 1)) return false; struct json_children *chld = dest->children; if (index > 0) { chld->items[index]->prev = src; chld->items[index - 1]->next = src; } for (size_t i = index; i < chld->nitems - 1; i++) chld->items[i + 1] = chld->items[i]; chld->items[index] = src; return true; } bool json_replace(struct json *dest, size_t index, struct json *src) { if (!dest || !src) return false; json_detach(src); struct json_children *chld = dest->children; if (index >= chld->nitems) { add_children(dest, src); return true; } struct json *old = json_swap_index(dest, index, src); json_clear(old); free(old); return true; } struct json *json_at_mut(struct json *json, size_t index) { if (!json || json->type != JSON_ARRAY || index > json->children->nitems) return NULL; return json->children->items[index]; } const struct json *json_at_const(const struct json *array, size_t index) { return json_at_mut((struct json *) array, index); } struct json *json_detach_index(struct json *json, size_t index) { if (!json_is_array(json) || index >= json->children->nitems) return NULL; struct json *tmp = json->children->items[index]; json_detach(tmp); return tmp; } struct json *json_swap_index(struct json *json, size_t index, struct json *src) { if (!json_is_array(json) || index >= json->children->nitems || !src) return NULL; json_detach(src); struct json_children *chld = json->children; if (index > 0) chld->items[index - 1]->next = src; if (index < chld->nitems - 1) chld->items[index + 1]->prev = src; struct json *old = chld->items[index]; chld->items[index] = src; return old; } void json_delete_index(struct json *json, size_t index) { struct json *tmp = json_detach_index(json, index); json_delete(tmp); }