JSON_Status json_object_set_value()

in lib/parson.c [2044:2085]


JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) {
    unsigned long hash = 0;
    parson_bool_t found = PARSON_FALSE;
    size_t cell_ix = 0;
    size_t item_ix = 0;
    JSON_Value *old_value = NULL;
    char *key_copy = NULL;

    if (!object || !name || !value || value->parent) {
        return JSONFailure;
    }
    hash = hash_string(name, strlen(name));
    found = PARSON_FALSE;
    cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
    if (found) {
        item_ix = object->cells[cell_ix];
        old_value = object->values[item_ix];
        json_value_free(old_value);
        object->values[item_ix] = value;
        value->parent = json_object_get_wrapping_value(object);
        return JSONSuccess;
    }
    if (object->count >= object->item_capacity) {
        JSON_Status res = json_object_grow_and_rehash(object);
        if (res != JSONSuccess) {
            return JSONFailure;
        }
        cell_ix = json_object_get_cell_ix(object, name, strlen(name), hash, &found);
    }
    key_copy = parson_strdup(name);
    if (!key_copy) {
        return JSONFailure;
    }
    object->names[object->count] = key_copy;
    object->cells[cell_ix] = object->count;
    object->values[object->count] = value;
    object->cell_ixs[object->count] = cell_ix;
    object->hashes[object->count] = hash;
    object->count++;
    value->parent = json_object_get_wrapping_value(object);
    return JSONSuccess;
}